#!/bin/bash

if [ -f /etc/init.d/transition-service ]; then
    echo
    echo "Checking whether the transition-service (NetApp 7-Mode Transition Tool server) is already present on the system."
    echo "Service already exists."
    echo "Run the './unconfigure' command to remove the existing transition-service, then rerun the './configure' command."
    echo
    exit 1
fi

echo
echo "Checking java version."

if type -p java; then
    echo "Found java executable in PATH."
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo "Found java executable in JAVA_HOME."
    _java="$JAVA_HOME/bin/java"
else
    echo "Could not detect Java Runtime Environment on this system."
    echo "Set JAVA_HOME and append to PATH after installing JRE 1.7."
    echo "Ensure that the default JRE version is 1.7."
    echo 
    exit 1
fi

if [[ "$_java" ]]; then
    javaVersion=$(java -version 2>&1 | head -n 1 | cut -d\" -f 2)
    jGeneration=$(java -version 2>&1 | head -n 1 | cut -d\" -f 2 | cut -d"." -f1)
    jMajor=$(java -version 2>&1 | head -n 1 | cut -d\" -f 2 | cut -d"." -f2)
    echo "Java version:" $javaVersion

    if [[ "$jGeneration" = "1" ]]; then 
        if [[ "$jMajor" = "8" ]]; then 
            echo "Status: JRE check passed."
        elif [[ "$jMajor" = "7" ]]; then 
            echo "Recommended JRE version: 1.8u45"
        else
            echo "Required JRE version: 1.8u45"
            echo "Status: Check failed." 
            echo "Set JAVA_HOME and append to PATH after installing JRE 1.8u45."
            echo "Ensure that the default JRE version is 1.8u45."
            echo
            exit 1
        fi
    else
        echo "Required JRE version:" 1.8u45
            echo "Status: Check failed." 
            echo "Set JAVA_HOME and append to PATH after installing JRE 1.8u45."
            echo "Ensure that the default JRE version is 1.8u45."
            echo
            exit 1
    fi
fi

echo
echo "Preparing the transition-service (NetApp 7-Mode Transition Tool server) script." 

templateFile=bin/transition-service-template
outFile=bin/transition-service
Install_Dir_Path="`pwd`"
sed "s+INSTALL_DIR=.*+INSTALL_DIR=$Install_Dir_Path+" $templateFile > $outFile

echo
echo "Copying the transition-service (NetApp 7-Mode Transition Tool server) script to the /etc/init.d/ directory."
cp $outFile /etc/init.d/

echo
echo "Setting execute permission for the transition-service (NetApp 7-Mode Transition Tool server) script."
chmod +x /etc/init.d/transition-service

echo
echo "Registering the transition-service (NetApp 7-Mode Transition Tool server) script as a Linux service."
chkconfig --add transition-service

echo
echo "Setting runlevel for the transition-service (NetApp 7-Mode Transition Tool server)."
chkconfig --level 2345 transition-service on

echo
echo "Starting the transition-service (NetApp 7-Mode Transition Tool server)."
service transition-service start

echo
echo "Updating the NetApp 7-Mode Transition Tool server PATH in ~/.bashrc file."
echo "export PATH=\$PATH:$Install_Dir_Path/bin" > ~/.bashrc

echo
echo "Reloading bashrc."
source ~/.bashrc
echo
