#!/bin/bash

EG_HOME=/opt/egurkha
export EG_HOME
CLASSPATH=.:$EG_HOME/lib/eg_manager.jar:$EG_HOME/lib/jnet.jar:$EG_HOME/lib/jsse.jar:$EG_HOME/lib/eg_util.jar:$EG_HOME/lib/jtds.jar:$EG_HOME/lib/classes111.zip:$EG_HOME/lib/ThinTds.jar:$CLASSPATH
export CLASSPATH
EG_DATABASE=$EG_HOME/bin/database
export EG_DATABASE

if [ -z "$JAVA_HOME" ]
then
echo "Please enter the location of your Java home directory [$java_home]: "
FLAG="y"
while [ "$FLAG" = "y" ]
do
        read javaHome
        if [ -z "$javaHome" ]
        then
                javaHome=$java_home
        fi
        if [ -f $javaHome/bin/java ]
        then
		version=`$javaHome/bin/java com.eg.EgJavaVersion $javaHome`
		if test "$version" != "true" ;
		then
			echo "The eG Manager requires JDK version 1.6 or higher ..."
			echo "Please enter the location of your Java home directory: "
		else
                	FLAG="n"
		fi
        else
                echo "Failed to locate $javaHome/bin/java ..."
                echo "Please enter the location of your Java home directory [$java_home]: "
        fi
done
JAVA_HOME=$javaHome
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
JAVA_LIB=$javaHome/lib
while [ ! -f $JAVA_LIB/tools.jar ]
do
        echo "The setup process failed to locate $JAVA_LIB/tools.jar ..."
        echo "Please enter the location of the Java lib directory: "
        FLAG="y"
        while [ "$FLAG" = "y" ]
        do
                read javaLib
                FLAG="n"
                if [ -z "$javaLib" ]
                then
                        JAVA_LIB=$java_home/lib
                        export JAVA_LIB
                else
                        JAVA_LIB=$javaLib
                        export JAVA_LIB
                fi
        done
done

curDir=`pwd`
cd $JAVA_LIB
cd ..
JAVA_FONT_HOME=`pwd`
cd $curDir
export JAVA_FONT_HOME
while [ ! -d $JAVA_FONT_HOME/jre/lib/fonts ]
do
 echo "The setup process failed to locate $JAVA_FONT_HOME/jre/lib/fonts ..."
 echo "Please enter the location of the Java directory where "
 echo "jre/lib/fonts subdirectory is available: "
 FLAG="y"
 while [ "$FLAG" = "y" ]
        do
                read javaFont
                FLAG="n"
                if [ -z "$javaFont" ]
                then
                        JAVA_FONT_HOME=$javaFont
                        export JAVA_FONT_HOME
                else
                        JAVA_FONT_HOME=$javaFont
                        export JAVA_FONT_HOME
                fi
        done
done

CLASSPATH=$CLASSPATH:$JAVA_LIB/tools.jar:$JAVA_LIB/dt.jar
export CLASSPATH
fi
JAVA_CMD=$JAVA_HOME/bin/java
export JAVA_CMD

clear

thisIP=$1
thisPort=$2
SSL=$3
proxyIP="none"
proxyPort="none"
proxyUser="null"
proxyPwd="none"
ipOfPM="none"
portOfPM="none"
pmSSL="false"
NAT="none"
PAT="none"

echo ""
echo "Would you like to enable eG manager redundancy y/n [n]? "
read clusterAware
if [ -z "$clusterAware" ]
then 
	clusterAware="n"
fi

if [ "$clusterAware" = "n" ] || [ "$clusterAware" = "N" ]
then
	exit
fi

if [ "$clusterAware" = "y" ] || [ "$clusterAware" = "Y" ]
then

	if [ -z "$1" ]
	then
		echo "Please enter the hostname (or IP address) of this host: "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read thisIP
			if [ -n "$thisIP" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid hostname (or IP address): "
			fi
		done
	else
		thisIP=$1
	fi

 	# this code converts thisIP to lower case
        if echo "$thisIP"|grep [A-Z] > /dev/null;
        then
                thisIP=`echo "$thisIP"|tr "[A-Z]" "[a-z]"`
        fi

	if [ -z "$2" ]
	then
		echo "Please enter the port at which this eG manager listens : "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read thisPort
			if [ -n "$thisPort" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid port : "
			fi
		done
	else
		thisPort=$2
	fi

	if [ -z "$3" ]
	then
		echo "Please indicate if your eG manager uses SSL y/n :[n] "
		read SSL
		if [ -z "$SSL" ]
		then
			SSL="n"
		fi
	else
		SSL=$3
	fi

	echo "Please specify if you use Network Address Translation(NAT) y/n :[n] "
	read isNATed
	if [ "$isNATed" = "y" ] || [ "$isNATed" = "Y" ]
	then
		isNATed="y"
	else
		isNATed="n"
	fi 

	if [ "$isNATed" = "y" ]
	then
		echo "Please enter the NAT ip/hostname: "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read NAT
			if [ -n "$NAT" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid NAT ip/hostname : "
			fi
		done
	fi	

	echo "Please specify if you use Port Address Translation(PAT) y/n :[n] "
	read isPATed
	if [ "$isPATed" = "y" ] || [ "isPATed" = "Y" ]
	then
		isPATed="y"
	else
		isPATed="n"
	fi 

	if [ "$isPATed" = "y" ]
	then
		echo "Please enter the PAT port: "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read PAT
			if [ -n "$PAT" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid PAT port : "
			fi
		done
	fi
	
	echo "Please indicate if you would use proxy server for communications y/n :[n] "
	read proxySupport
	if [ "$proxySupport" = "y" ] || [ "$proxySupport" = "Y" ]
	then  
		proxySupport="y"
	else
		proxySupport="n"
	fi

	if [ "$proxySupport" = "y" ]
	then
		echo "Please enter the hostname (or IP address) of the proxy: "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read proxyIP
			if [ -n "$proxyIP" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid hostname (or IP address): "
			fi
		done
		
		echo "Please enter the port of the proxy: "
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read proxyPort
			if [ -n "$proxyPort" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid port: "
			fi
		done

		echo "Do you need authentication for the proxy? y/n [n]: "
		read authenticate
		if [ "$authenticate" = "y" ] || [ "$authenticate" = "Y" ]
		then
			authenticate="y"
		else
			authenticate="n"
		fi
		if [ "$authenticate" = "y" ]
		then
			echo "Please enter the username to be used for the proxy: "
			FLAG="y"
			while [ "$FLAG" = "y" ]
			do
				read proxyUser
				if [ -n "$proxyUser" ]
				then
					FLAG="n"
				else
					echo "Please enter a valid username: "
				fi
			done
			
			FLAG="y"
			while [ "$FLAG" = "y" ]
			do
				stty -echo
				echo "Please enter the password for $proxyUser : "
				read proxyPwd
				echo " "
				if [ -z "$proxyPwd" ]
				then
					echo "Sorry, Invalid password! Please try again."
				else
					echo "Please re-enter the password for $proxyUser : "
					read proxyPwd2
					echo " "
					if [ "$proxyPwd" = "$proxyPwd2" ]
					then
						FLAG="n"
					else
						echo "Sorry, the passwords supplied do not match! Please try again."
					fi
				fi
				stty echo
			done
		fi
	fi	

	echo ""
	echo "Is this a primary manager y/n [n]? "
	read isPrimary
	if [ "$isPrimary" = "y" ] || [ "$isPrimary" = "Y" ]
	then
		isPrimary="y"
	else
		isPrimary="n"
	fi

	if [ "$isPrimary" = "n" ]
	then
		FLAG="y"
		echo "Please enter the hostname of the primary manager: "
		while [ "$FLAG" = "y" ]
		do
			read ipOfPM
			if [ -n "$ipOfPM" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid hostname : "
			fi
		done

		FLAG="y"
		echo "Please enter the port of the primary manager: "
		while [ "$FLAG" = "y" ]
		do
			read portOfPM
			if [ -n "$portOfPM" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid port : "
			fi
		done


		echo "Please indicate if your primary manager uses SSL y/n [n]: "
		read pmSSL
		if [ "$pmSSL" = "y" ] || [ "$pmSSL" = "Y" ]
		then
			pmSSL="true"
		else
			pmSSL="false"
		fi
		
	fi
fi

$JAVA_CMD com.eg.configureCluster $thisIP $thisPort $SSL $proxyIP $proxyPort $proxyUser "$proxyPwd" $ipOfPM $portOfPM $pmSSL $NAT $PAT






