#!/bin/bash
accept=$1
hostName=$2
port=$3
setProxy=$4
proxyHost=$5
proxyPort=$6
setAuthentication=$7
proxyUsername=$8
proxyPassword=$9
isSsl=${10}
trust=${11}
nick=${12}
cf=${13}
sybase=${14}
userName=${15}
groupName=${16}
if [ -z "${17}" ] 
then
    nicks=`hostname`
else
    nicks=${17}
fi
XMX=${18}
silentInstall=$accept

Check=`/opt/egurkha/bin/xvt setup_agent`
export Check
if [ $Check = "FALSE" ]
then
echo "The eG agent must be setup by the eG user. Exiting ..."
exit
fi

if [ -z "$XMX" ]
then
	XMX="-Xmx512m"
fi

#XMX=""
#if [ -d "/opt/egurkha/manager" ]
#then
#	XMX="-Xmx256m"
#fi

if [ -z "$silentInstall" ]
then
echo "Configuring the eG Agent ...	     "
echo " "
echo "*************************************************************************"
echo "  The licensing terms for the eG software are mentioned in the file            "
echo "   /opt/egurkha/license_agreement. PLEASE READ THIS FILE BEFORE       "
echo "   PROCEEDING FURTHER.						     "
echo "*************************************************************************"
echo " "
echo "Please indicate if you accept the eG licensing terms y/n [n]: "
read accept 
if test -z "$accept" ; then
        exit
fi
fi

if test  "$accept" != "y" ; then
        exit
fi

#tmp1=`which java 2>&1`
#cnt=`echo $tmp1 | grep "no java" | wc -l`
#if [ $cnt -eq 1 ]
#then
#	java_home=
#else
#	d1=`dirname $tmp1`
#	java_home=`dirname $d1`
#fi

PATH=$PATH:/usr/bin
export PATH

if [ -z "$silentInstall" ]
then
	clear
fi
#TMPDIR check this enviroment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
fi

EG_HOME=/opt/egurkha
export EG_HOME
#default JTDS is 1.7 - copy over 1.6 or 1.5 if needed
AgentJREversion=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.6" | wc -l`
export AgentJREversion
if [ "$AgentJREversion" -ge "1" ]
then
	cp $EG_HOME/lib/jtds/jtds1.6.jar $EG_HOME/lib/jtds.jar
fi
AgentJREversion=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.5" | wc -l`
export AgentJREversion
if [ "$AgentJREversion" -ge "1" ]
then
	cp $EG_HOME/lib/jtds/jtds1.5.jar $EG_HOME/lib/jtds.jar
fi
#default is jtds1.7

# special check for linux to check the winexe version
val=`/opt/egurkha/lib/vmgfiles/winexe 2>&1 | grep "FATAL" | wc -l`
if [ $val -eq 1 ]
then
	val=`/opt/egurkha/lib/vmgfiles/winexe.1 2>&1 | grep "FATAL" | wc -l`
	if [ $val -eq 0 ]
	then
		cp /opt/egurkha/lib/vmgfiles/winexe.1 /opt/egurkha/lib/vmgfiles/winexe
		chmod +x /opt/egurkha/lib/vmgfiles/winexe
	else
		val=`/opt/egurkha/lib/vmgfiles/winexe.2 2>&1 | grep "FATAL" | wc -l`
		if [ $val -eq 0 ]
		then
			cp /opt/egurkha/lib/vmgfiles/winexe.2 /opt/egurkha/lib/vmgfiles/winexe
			chmod +x /opt/egurkha/lib/vmgfiles/winexe
		fi
	fi
fi

# special check for linux to check the snmpwalk version
val=`/opt/egurkha/bin/snmpwalk.9 2>&1 | grep "error while loading" | wc -l`
if [ "$val" -eq "0" ]
then 
val=`/opt/egurkha/bin/snmpwalk.9 2>&1 | grep "not found" | wc -l`
fi
if [ "$val" -eq "0" ]
then
	cp /opt/egurkha/bin/snmpwalk.9 /opt/egurkha/bin/snmpwalk
else 
	val=`/opt/egurkha/bin/snmpwalk.8 2>&1 | grep "error while loading" | wc -l`
	if [ "$val" -eq "0" ]
	then
		val=`/opt/egurkha/bin/snmpwalk.8 2>&1 | grep "not found" | wc -l`
	fi
	if [ "$val" -eq "0" ]
 	then	
		cp /opt/egurkha/bin/snmpwalk.8 /opt/egurkha/bin/snmpwalk
	else
		val=`/opt/egurkha/bin/snmpwalk.7.2 2>&1 | grep "error while loading" | wc -l`
		if [ "$val" -eq "0" ]
		then
		val=`/opt/egurkha/bin/snmpwalk.7.2 2>&1 | grep "not found" | wc -l`
		fi
		if [ "$val" -eq "0" ]
		then
			cp /opt/egurkha/bin/snmpwalk.7.2 /opt/egurkha/bin/snmpwalk
		else
			val=`/opt/egurkha/bin/snmpwalk.7 2>&1 | grep "error while loading" | wc -l`
			if [ "$val" -eq "0" ]
			then
			val=`/opt/egurkha/bin/snmpwalk.7 2>&1 | grep "not found" | wc -l`
			fi
			if [ "$val" -eq "0" ]
			then
				cp /opt/egurkha/bin/snmpwalk.7 /opt/egurkha/bin/snmpwalk
			else
				val=`/opt/egurkha/bin/snmpwalk.6 2>&1 | grep "error while loading" | wc -l`
				if [ "$val" -eq "0" ]
				then
				val=`/opt/egurkha/bin/snmpwalk.6 2>&1 | grep "not found" | wc -l`
				fi
				if [ "$val" -eq "0" ]
				then
					cp /opt/egurkha/bin/snmpwalk.6 /opt/egurkha/bin/snmpwalk
				fi
			fi	
		fi	
	fi
fi

val=`/opt/egurkha/bin/snmpwalk 2>&1 | grep "error while loading" | wc -l`
if [ "$val" -eq "1" ]
then
	if [ -f /usr/bin/snmpwalk ]
	then
		cp /usr/bin/snmpwalk /opt/egurkha/bin
		val=`/opt/egurkha/bin/snmpwalk 2>&1 | grep "error while loading" | wc -l`
		if [ "$val" -eq "1" ]
		then
			echo "ERROR detected when loading the 'snmpwalk' command"
			echo "Some of the monitoring capabilities of the eG Aagent "
			echo "may not be functional ..."
			echo "Proceeding with the rest of the installation ..."
		fi
	else
			echo "ERROR detected when loading the 'snmpwalk' command"
			echo "Some of the monitoring capabilities of the eG Agent "
			echo "may not be functional ..."
			echo "Proceeding with the rest of the installation ..."
	fi
fi
if [ -z "$silentInstall" ]
then
	clear
fi
EG_HOME=/opt/egurkha
export EG_HOME
EG_CLASSPATH=$EG_HOME/lib/eg_agent.jar:$EG_HOME/lib/classes111.zip
export EG_CLASSPATH
CLASSPATH=$EG_CLASSPATH
export CLASSPATH

if [ ! -d $EG_HOME/tmp ]
then
        mkdir -p $EG_HOME/tmp
fi

if [ ! -d $EG_HOME/lib/com/eg ]
then
        mkdir -p $EG_HOME/lib/com/eg
fi

if [ -z "$silentInstall" ]
then
echo "Setup of the Manager/Agent communication path"
echo "**********************************************"
echo "Enter the hostname (or IP address) of the eG Manager: "  
while :
do
	read hostName
	if [ -z "$hostName" ]
	then
		echo "Enter the hostname (or IP address) of the eG Manager: "  
	else
		break
	fi
done

echo "Enter the port number of the eG Manager [7077]: " 
read port

if test -z "$port" ; then
        port="7077"
fi

echo "Please indicate if you want to use a proxy for the eG Agent"
echo "to communicate with the eG Manager? y/n [n] : "
read setProxy
if test -z "$setProxy" ; then
	setProxy="n"
fi

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

	echo "Please enter the port on which the proxy is listening [80]: "  
	read proxyPort
	if [ -z "$proxyPort" ]
	then
		proxyPort="80"
	fi

	echo "Do you need authentication for the proxy? y/n [n] : "
	read setAuthentication
	if test -z "$setAuthentication" ; then
		setAuthentication="n"
	fi

	if [ "$setAuthentication" = "y" ] 
	then
		echo "Please enter the username to be used for the proxy : "  
		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			read proxyUsername
			if [ -n "$proxyUsername" ]
			then
				FLAG="n"
			else
				echo "Please enter a valid username for the proxy : "
			fi
		done

		FLAG="y"
		while [ "$FLAG" = "y" ]
		do
			stty -echo
			echo "Please enter the password for $proxyUsername : "
			read proxyPassword
			echo " "
			if [ -z "$proxyPassword" ]
			then
				echo "Sorry, Invalid password! Please try again ... "
			else
				echo "Please re-enter the password for $proxyUsername: "
				read proxyPassword2
				echo " "
				if [ "$proxyPassword" = "$proxyPassword2" ]
				then
					FLAG="n"
				else
					echo "Sorry, the passwords supplied do not match ! Please try again ..."
				fi
			fi
			stty echo
		done
		echo " "
	fi
fi

echo "The eG Agent can use HTTP or HTTP/SSL to communicate with "
echo "the eG Manager. In order to use HTTP/SSL, please make sure "
echo "that the eG Manager has been configured to support SSL."
echo " "
echo "Do you want to configure the eG Agent to use SSL "
echo "for communication with the eG Manager? y/n [n] : "

read isSsl
fi
if test -z "$isSsl" ; then
	isSsl="false"
fi
if [ -z "$silentInstall" ]
then
trust="n"
if [ "$isSsl" = "y" ]
then
    isSsl="true";
	echo "Do you want to allow trusted certificates only? y/n [n] : "
	read trust
	if [ -z "$trust" ]
	then
		trust="n"
	fi

fi
else
if [ "$isSsl" = "y" ]
then
	isSsl="true";
fi
fi
tflag="false"
if [ "$trust" = "y" ]
then
	tflag="true"
	trust="-highSecurity true"
else
	tflag="false"
	trust="-highSecurity false"
fi

java_home="/opt/egurkha/jre/"
export java_home
#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 EgJavaVersion $javaHome`
#		if test "$version" != "false" ;
#		then
#			FLAG="n"
#		else
#			echo "eG $PRODUCT requires JRE version 1.3.1 or higher ..."
#			echo "Please enter the location of your Java home directory: "
#		fi
#        else
#                echo "Failed to locate $javaHome/bin/java ..."
#                echo "Please enter the location of your Java home directory: "
#        fi
#done
javaHome=$java_home
export javaHome
JAVA_HOME=$javaHome
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

JAVA_LIB=$javaHome/lib
export JAVA_LIB
if [ -z "$silentInstall" ]
then
echo "Please indicate if you want to assign a nick name(s) for this host? y/n [n] : "
read nick
if [ -z "$nick" ]
then
	nick="n"
fi
fi
if [ "$nick" = "y" ]
then
	FLAG="n"
	while [ "$FLAG" = "n" ]
	do
		if [ -z "$silentInstall" ]
		then
		echo "Please enter the nick name(s)(seperated by colon) to be used for this host: \c"
		read nicks
		fi
		if [ ! -z "$nicks" ]
		then
			TMPFLAG="n"
			if [ "$TMPFLAG" = "n" ]
			then
				idx=`$JAVA_HOME/bin/java com.eg.NicknameValidator "$nicks"`
				export idx
				if [ "$idx" = "0" ]
				then
					TMPFLAG=y
					FLAG=y
					$JAVA_HOME/bin/java com.eg.EgNick -set $nicks
				else
					echo "$idx"
				fi
			fi
		else
			echo "Nick name should not be empty"
		fi
	done
fi
#while [ ! -f $JAVA_LIB/tools.jar ]
#do
#	echo "The setup process could not locate the Java lib directory."
#	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

PATH=/opt/egurkha/bin:$JAVA_HOME/bin:/usr/bin:/bin:$PATH:/etc
export PATH

# this code added for DB2 support
EG_TEMP1=/tmp/db2_temp1
EG_TEMP2=/tmp/db2_temp2
export EG_TEMP1 EG_TEMP2
DB2_FILE=/opt/egurkha/bin/runEgPerfmonitor.sh
export DB2_FILE
cat $DB2_FILE | sed '/^#!\/bin\/sh/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/^PATH=/d' > $EG_TEMP2
cat $EG_TEMP2 | sed '/^export PATH/d' > $EG_TEMP1
echo "#!/bin/sh" > $DB2_FILE
echo "PATH=$JAVA_HOME/bin:\$PATH" >> $DB2_FILE
echo "export PATH" >> $DB2_FILE
cat $EG_TEMP1 >> $DB2_FILE
rm -f $EG_TEMP1 $EG_TEMP2

#This code is added for conf_webadapter

EG_TEMP1=/tmp/conf1
EG_TEMP2=/tmp/conf2
export EG_TEMP1 EG_TEMP2
CONF_FILE=/opt/egurkha/bin/conf_webadapter
export CONF_FILE
cat $CONF_FILE  | sed '/^#!\/opt\/egurkha\/bin\/ksh/d'  > $EG_TEMP1
cat $EG_TEMP1  | sed '/^#!\/bin\/bash/d'  > $EG_TEMP2
mv $EG_TEMP2 $EG_TEMP1
cat $EG_TEMP1  | sed '/^PATH=/d'  > $EG_TEMP2
cat $EG_TEMP2  | sed '/^CLASSPATH=/d'  > $EG_TEMP1
cat $EG_TEMP1  | sed '/^JAVA_HOME=/d'  > $EG_TEMP2
echo "#!/bin/bash" >  $CONF_FILE
echo "JAVA_HOME=$JAVA_HOME/bin" >> $CONF_FILE
echo "PATH=$JAVA_HOME/bin:\$PATH" >> $CONF_FILE
echo "CLASSPATH=$EG_CLASSPATH" >> $CONF_FILE
cat $EG_TEMP2 >> $CONF_FILE
rm -f $EG_TEMP1 $EG_TEMP2

#This code is added for cvt

EG_TEMP1=/tmp/cvt1
EG_TEMP2=/tmp/cvt2
export EG_TEMP1 EG_TEMP2
CVT_FILE=/opt/egurkha/bin/cvt
export CVT_FILE
cat $CVT_FILE | sed '/^#!\/bin\/bash/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/^JAVA_HOME=/d' > $EG_TEMP2
cat $EG_TEMP2 | sed '/^export JAVA_HOME/d' > $EG_TEMP1
echo "#!/bin/bash" > $CVT_FILE
echo "JAVA_HOME=$JAVA_HOME" >> $CVT_FILE
echo "export JAVA_HOME" >> $CVT_FILE
cat $EG_TEMP1 >> $CVT_FILE
rm -f $EG_TEMP1 $EG_TEMP2


# To update the eGAgentmon script for java_path 

EG_TEMP1=/tmp/eg_mon1
EG_TEMP2=/tmp/eg_mon2
AGENT_MON=/opt/egurkha/bin/eGAgentmon
export EG_TEMP1 EG_TEMP2 AGENT_MON
cat $AGENT_MON | sed '/^#!\/bin\/sh/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/^JAVA_HOME=/d' > $EG_TEMP2 
cat $EG_TEMP2 | sed '/^JAVA_LIB=/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/^export JAVA_HOME JAVA_LIB/d' > $EG_TEMP2

tempHostName=`echo $hostName`
indexOfColon=`expr index "$tempHostName" ":"`
if [ $indexOfColon -ne 0 ]
then
     tempHostName=`echo "["$tempHostName"]"`
fi

isJRE16=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.6" | wc -l`
isJRE17=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.7" | wc -l`
if [ $isJRE17 -eq 0 ]
then
	isJRE17=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.8" | wc -l`
fi
isJRE10=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "10." | wc -l`
isJRE12=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "12." | wc -l`
export isJRE16 
export isJRE17
export isJRE10
export isJRE12
javaCmd=" "
if [ "$isJRE16" -ge "1" ]
then
	javaCmd=" -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null "
fi
if [ "$isJRE17" -ge "1" ]
then
	javaCmd=" -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateMinidumpOnCrash "
fi
if [ "$isJRE12" -ge "1" ]
then
	javaCmd=" -Djavax.xml.soap.SAAJMetaFactory=com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateCoredumpOnCrash --add-opens java.management/sun.management=ALL-UNNAMED "
elif [ "$isJRE10" -ge "1" ]
then
	javaCmd=" --add-opens java.management/sun.management=ALL-UNNAMED --add-modules=java.xml.ws -Djavax.xml.soap.SAAJMetaFactory=com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateCoredumpOnCrash "
fi

echo "#!/bin/sh" > $AGENT_MON
echo "JAVA_HOME=$JAVA_HOME" >> $AGENT_MON
echo "JAVA_LIB=$JAVA_LIB" >> $AGENT_MON
echo "export JAVA_HOME JAVA_LIB" >> $AGENT_MON
cat $EG_TEMP2 >> $AGENT_MON
rm -f $EG_TEMP1 $EG_TEMP2
echo "#!/bin/bash" > $EG_HOME/bin/start_agent
echo "Check=\`/opt/egurkha/bin/xvt start_agent\`" >> $EG_HOME/bin/start_agent
echo "export Check" >> $EG_HOME/bin/start_agent
echo "if [ \$Check = \"FALSE\" ]" >> $EG_HOME/bin/start_agent
echo "then" >> $EG_HOME/bin/start_agent
echo "echo \"The eG agent must be started by the eG user. Exiting ...\"" >> $EG_HOME/bin/start_agent
echo "exit" >> $EG_HOME/bin/start_agent
echo "fi" >> $EG_HOME/bin/start_agent
echo "" >> $EG_HOME/bin/start_agent
echo "JAVA_HOME=$JAVA_HOME" >> $EG_HOME/bin/start_agent
echo "export JAVA_HOME" >> $EG_HOME/bin/start_agent
echo "JAVA_LIB=$JAVA_LIB" >> $EG_HOME/bin/start_agent
echo "export JAVA_LIB" >> $EG_HOME/bin/start_agent
echo " " >> $EG_HOME/bin/start_agent
echo "ps1=" >> $EG_HOME/bin/start_agent
echo "export ps1" >> $EG_HOME/bin/start_agent
echo "PS1=" >> $EG_HOME/bin/start_agent
echo "export PS1" >> $EG_HOME/bin/start_agent
echo " " >> $EG_HOME/bin/start_agent
echo "CLASSPATH=" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $EG_HOME/bin/start_agent
echo "LD_LIBRARY_PATH=$EG_HOME/lib:$LDP\$LD_LIBRARY_PATH" >> $EG_HOME/bin/start_agent
echo "export LD_LIBRARY_PATH" >> $EG_HOME/bin/start_agent
echo "ulimit -c 0" >> $EG_HOME/bin/start_agent
echo "ulimit -s unlimited 2>/dev/null " >> $EG_HOME/bin/start_agent
echo "EG_HOME=/opt/egurkha" >> $EG_HOME/bin/start_agent
echo "export EG_HOME" >> $EG_HOME/bin/start_agent
echo "if [ ! -z \"\$JAVA_HOME\" ] " >> $EG_HOME/bin/start_agent
echo "then" >> $EG_HOME/bin/start_agent
echo "CLASSPATH=\$CLASSPATH:\$EG_HOME/lib/vmgfiles:\$JAVA_LIB/tools.jar:\$JAVA_LIB/dt.jar:\$JAVA_LIB/rt.jar" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $EG_HOME/bin/start_agent
echo "fi" >> $EG_HOME/bin/start_agent
echo "LANG=en_US" >> $EG_HOME/bin/start_agent
echo "export LANG" >> $EG_HOME/bin/start_agent
echo "jar_list=\`ls \$EG_HOME/lib | grep \".jar\" | awk '{print \$1}'\`" >> $EG_HOME/bin/start_agent
echo "if [ \"\$jar_list\" ]" >> $EG_HOME/bin/start_agent
echo "then" >> $EG_HOME/bin/start_agent
echo "        for jar in \$jar_list"  >> $EG_HOME/bin/start_agent
echo "        do" >> $EG_HOME/bin/start_agent
echo "                CLASSPATH=\$CLASSPATH:\$EG_HOME/lib/\$jar" >> $EG_HOME/bin/start_agent
echo "                export CLASSPATH" >> $EG_HOME/bin/start_agent
echo "        done" >> $EG_HOME/bin/start_agent
echo "fi" >> $EG_HOME/bin/start_agent
echo "zip_list=\`ls \$EG_HOME/lib | grep \".zip\" | awk '{print \$1}'\`" >> $EG_HOME/bin/start_agent
echo "if [ \"\$zip_list\" ]" >> $EG_HOME/bin/start_agent
echo "then" >> $EG_HOME/bin/start_agent
echo "        for zip in \$zip_list" >> $EG_HOME/bin/start_agent
echo "        do" >> $EG_HOME/bin/start_agent
echo "                CLASSPATH=\$CLASSPATH:\$EG_HOME/lib/\$zip" >> $EG_HOME/bin/start_agent
echo "                export CLASSPATH" >> $EG_HOME/bin/start_agent
echo "        done" >> $EG_HOME/bin/start_agent
echo "fi" >> $EG_HOME/bin/start_agent

echo "CLASSPATH=\$EG_HOME/lib/httpclient.jar:\$EG_HOME/lib/httpcore.jar:\$EG_HOME/lib/eg_agent.jar:\$EG_HOME/lib/eg_util.jar:\$EG_HOME/lib/vim25.jar:\$EG_HOME/lib/vim.jar:\$EG_HOME/lib/xml-apis.jar:\$EG_HOME/lib/xercesImpl.jar:\$EG_HOME/lib/xmlParserAPIs.jar:\$CLASSPATH" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $EG_HOME/bin/start_agent

echo "PATH=.:\$JAVA_HOME/bin:\$EG_HOME/bin:\$EG_HOME/bin/ic:/sbin:/usr/bin:/usr/sbin:/bin:\$PATH:/usr/local/bin:/bin" >> $EG_HOME/bin/start_agent
echo "export PATH" >> $EG_HOME/bin/start_agent
echo "host=$tempHostName" >> $EG_HOME/bin/start_agent
echo "portNo=$port" >> $EG_HOME/bin/start_agent 
echo "export host portNo" >> $EG_HOME/bin/start_agent
echo "XMX=$XMX" >> $EG_HOME/bin/start_agent
echo "export XMX" >> $EG_HOME/bin/start_agent
echo "/opt/egurkha/bin/stop_agent 0" >> $EG_HOME/bin/start_agent

echo "unset JAVA_TOOL_OPTIONS" >> $EG_HOME/bin/start_agent
echo "unset _JAVA_OPTIONS" >> $EG_HOME/bin/start_agent
echo "unset IBM_JAVA_OPTIONS" >> $EG_HOME/bin/start_agent
sfile=$EG_HOME/agent/config/eg_setup.ini
export sfile

echo "[$hostName:$port]" > $sfile
echo "manager=$hostName" >> $sfile
echo "port=$port" >> $sfile
echo "ssl=$isSsl" >> $sfile
echo "highSecurity=$tflag" >> $sfile

tru=`uname -a | grep "^OSF1" | wc -l`
if [ "$setProxy" = "y" ]
then
        echo "proxyHost=$proxyHost" >> $EG_HOME/bin/start_agent
        echo "proxyPort=$proxyPort" >> $EG_HOME/bin/start_agent
        echo "export proxyHost proxyPort" >> $EG_HOME/bin/start_agent
	echo "proxyHost=$proxyHost" >> $sfile
	echo "proxyPort=$proxyPort" >> $sfile
        if [ "$setAuthentication" = "y" ]
        then
			proxyPassword=`$JAVA_HOME/bin/java com.eg.setPass "$proxyPassword" true`
                echo "proxyUsername=$proxyUsername" >> $EG_HOME/bin/start_agent
                echo "proxyPassword=$proxyPassword" >> $EG_HOME/bin/start_agent
		echo "proxyUsername=$proxyUsername" >> $sfile
		echo "proxyPassword=$proxyPassword" >> $sfile
                echo "export proxyUsername proxyPassword" >> $EG_HOME/bin/start_agent
                if [ $tru -eq 0 ]
                then
                	echo "nohup /opt/egurkha/jre/bin/java -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort -proxyUsername \$proxyUsername -proxyPassword \$proxyPassword > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
        	else
			echo "/opt/egurkha/jre/bin/java -fast32 -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort -proxyUsername \$proxyUsername -proxyPassword \$proxyPassword > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
        	fi
        else
                if [ $tru -eq 0 ]
                then
               		 echo "nohup /opt/egurkha/jre/bin/java -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
               	else
			echo "/opt/egurkha/jre/bin/java -fast32 -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
               	fi
        fi
else
        if [ $tru -eq 0 ]
        then
        	echo "nohup /opt/egurkha/jre/bin/java -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
	else
		echo "/opt/egurkha/jre/bin/java -fast32 -Xrs -Deg.name=EgMainAgent $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml \$XMX -Dsun.net.inetaddr.ttl=900 EgMainAgent -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
	fi
fi

echo "sleep 5" >> $EG_HOME/bin/start_agent
echo "ver=\"\`java com.eg.EgInstallInfo\`\"" >> $EG_HOME/bin/start_agent
echo "export ver" >> $EG_HOME/bin/start_agent
echo "val=\`ps -e -o pid,args | grep egurkha/jre | grep -v grep | wc -l\`" >> $EG_HOME/bin/start_agent
echo "if [ \$val -gt 0 ] " >>  $EG_HOME/bin/start_agent
echo "then " >>  $EG_HOME/bin/start_agent
echo   "echo \"***************************************************************\""  >>  $EG_HOME/bin/start_agent
echo   "echo \"The eG Agent \$ver has been started ...\"" >>  $EG_HOME/bin/start_agent
echo   "echo \"Please check the file: \"/opt/egurkha/agent/logs/error_log\"\"" >>  $EG_HOME/bin/start_agent
echo "echo \"for any errors while executing the agent.\""  >>  $EG_HOME/bin/start_agent
echo "echo \"***************************************************************\"">>  $EG_HOME/bin/start_agent
echo "else"  >>  $EG_HOME/bin/start_agent
	echo "val=\`ps -e -o pid,args | grep \"java -Xrs\" | grep -v grep | wc -l\`" >> $EG_HOME/bin/start_agent
	echo "if [ \$val -gt 0 ] " >>  $EG_HOME/bin/start_agent
	echo "then " >>  $EG_HOME/bin/start_agent
	echo	"echo \"***************************************************************\""  >>  $EG_HOME/bin/start_agent
	echo	"echo \"The eG Agent \$ver has been started ...\"" >>  $EG_HOME/bin/start_agent
	echo	"echo \"Please check the file: \"/opt/egurkha/agent/logs/error_log\"\"" >>  $EG_HOME/bin/start_agent
	echo	"echo \"for any errors while executing the agent.\""  >>  $EG_HOME/bin/start_agent
	echo	"echo \"***************************************************************\"">>  $EG_HOME/bin/start_agent
	echo "else" >> $EG_HOME/bin/start_agent

		echo "val=\`ps -e -o pid,args | grep \"java -fast32\" | grep -v grep | wc -l\`" >> $EG_HOME/bin/start_agent
		echo "if [ \$val -gt 0 ] " >>  $EG_HOME/bin/start_agent
		echo "then " >>  $EG_HOME/bin/start_agent
		echo	"echo \"***************************************************************\""  >>  $EG_HOME/bin/start_agent
		echo	"echo \"The eG Agent \$ver has been started ...\"" >>  $EG_HOME/bin/start_agent
		echo	"echo \"Please check the file: \"/opt/egurkha/agent/logs/error_log\"\"" >>  $EG_HOME/bin/start_agent
		echo	"echo \"for any errors while executing the agent.\""  >>  $EG_HOME/bin/start_agent
		echo	"echo \"***************************************************************\"">>  $EG_HOME/bin/start_agent
		echo "else" >> $EG_HOME/bin/start_agent

	
		echo	"echo \"***********************************************************************\""  >>  $EG_HOME/bin/start_agent
		echo	"echo \"The eG Agent \$ver failed to start ...\""  >>  $EG_HOME/bin/start_agent
		echo	"echo \"Please check the file: /opt/egurkha/agent/logs/error_log for details.\"" >> $EG_HOME/bin/start_agent
		echo	"echo \"***********************************************************************\""  >>  $EG_HOME/bin/start_agent
		echo "fi"  >>  $EG_HOME/bin/start_agent	
	echo "fi"  >>  $EG_HOME/bin/start_agent
echo "fi"  >>  $EG_HOME/bin/start_agent
chmod +x $EG_HOME/bin/start_agent

#echo "Do you want to configure the agent now to monitor any database servers? y/n [y]"
#read dbread
#if test -z "$dbread" ; then
	#dbread="y"
#fi
#
#while [ "$dbread" = "y" ]
#do
	#setup_dbagent
	#echo "Would like to configure another database server for monitoring? y/n [n] "
	#read dbread
#done
#clear
CLASSPATH=$EG_CLASSPATH
export CLASSPATH
#webserver="n"
#echo "Do you want to configure any web servers running on `hostname` for monitoring by the eG Agent? y/n [n] "
#read webserver
#if test -z "$webserver" ; then
#	webserver="n"
#fi
#
#while [ "$webserver" = "y" ]
#do
#	setup_webadapter
#	echo "Would like to configure another web server running on `hostname` for monitoring by the eG Agent? y/n [n] "
#	read webserver
#	if test -z "$webserver"; then
#		webserver="n"
#	fi
#	clear
#done
#if [ -z "$silentInstall" ]
#then
#echo "Do you want to configure any Coldfusion servers running on `hostname` for monitoring by the eG Agent? y/n [n] "
#read webserver
#else
#	webserver=$cf
#fi
#if test -z "$webserver" ; then
#	webserver="n"
#fi
#while [ "$webserver" = "y" ]
#do
#	setup_cf
#	echo "Would like to configure another Coldfusion server running on `hostname` for monitoring by the eG Agent? y/n [n] "
#	read webserver
#	if test -z "$webserver"; then
#		webserver="n"
#	fi
#	clear
#done
#echo "Do you want to configure any WebLogic servers (version 6.0 or higher) running on `hostname` for monitoring by the eG Agent? y/n [n] "
#read webserver
#if test -z "$webserver" ; then
#	webserver="n"
#fi
#while [ "$webserver" = "y" ]
#do
#	setup_wl
#	echo "Would like to configure another WebLogic server (version 6.0 or higher) running on `hostname` for monitoring by the eG Agent? y/n [n] "
#	read webserver
#	if test -z "$webserver"; then
#		webserver="n"
#	fi
#	clear
#done
#
##########    setup for Sybase Adaptive Server
#webserver="n"
#if [ -z "$silentInstall" ]
#then
#echo "Do you want to configure any Sybase Adaptive servers running on `hostname` for monitoring by the eG Agent? y/n [n] "
#read webserver
#else
#webserver=$sybase
#fi
#if test -z "$webserver" ; then
#	webserver="n"
#fi
#while [ "$webserver" = "y" ]
#do
#	setup_sybase.sh
#	echo "Would like to configure another Sybase Adaptive server running on `hostname` for monitoring by the eG Agent? y/n [n] "
#	read webserver
#	if test -z "$webserver"; then
#		webserver="n"
#	fi
#	clear
#done
##########    sybase ends here

# To change permissions for the following scripts to enable only the eG user to 
# execute them
chmod 700 /opt/egurkha/bin/start_agent /opt/egurkha/bin/stop_agent /opt/egurkha/bin/eGAgentmon 
chmod 755 /opt/egurkha/agent/snmptrapd/*trapd
chmod 766 /opt/egurkha/agent/snmptrapd/log
chmod 600 /opt/egurkha/lib/id_rsa > /dev/null 2>/dev/null
chmod 600 /opt/egurkha/lib/authorized_keys > /dev/null 2>/dev/null

isJRE18=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.8" | wc -l`
if [ "$isJRE18" -ge "1" ]
then
	cp /opt/egurkha/lib/db2jcc.8.jar /opt/egurkha/lib/db2jcc.jar
fi

if [ -z "$silentInstall" ]
then
clear
echo "****************************************************************************"
echo " "
#echo " To configure any applications at a later time for monitoring by the "
#echo " eG Agent, please follow the steps below: "
#echo " " 
#echo " - Database Servers: Configure database servers for monitoring by "
#echo "                     the eG Agent through the eG user interface."
#echo " " 
#echo " - Web Servers:      To configure any web servers running on `hostname` "
#echo "                     for monitoring by the eG Agent, please have each of"
#echo "                     the web server's administrators execute the script "
#echo "			 	/opt/egurkha/bin/setup_webadapter"
#echo "                     This script will augment the web server's startup "
#echo "                     files with the eG monitoring capability."
#echo " "
#echo " - Coldfusion Servers: To configure a Coldfusion server for monitoring, "
#echo "                       please have the Coldfusion server's administrator "
#echo "                       execute the script "
#echo " " 
#echo " - WebLogic Servers: To configure a WebLogic server (version 6.0 or higher)"
#echo "                     for monitoring, please have the WebLogic server's  "
#echo "                     administrator execute the script "
#echo "                                  /opt/egurkha/bin/setup_wl."
#echo " "
#echo " - Sybase Adaptive Servers: To configure a Sybase Adaptive server "
#echo "                     for monitoring,please have the Sybase server's  "
#echo "                     administrator execute the script "
#echo "                     /opt/egurkha/bin/setup_sybase.sh"
#echo " "
echo " The eG Agent has been configured successfully."
echo " Please use the commands /opt/egurkha/bin/start_agent and"
echo " /opt/egurkha/bin/stop_agent to start and stop the agent."
echo " "
echo " To provide feedback and report errors, please contact "
echo " support@eginnovations.com."
echo "*************************************************************************"
echo " "
fi

