#!/bin/sh
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}
if [ -z "$XMX" ]
then
	XMX="-Xmx512m"
fi
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 "$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]: \c"
read accept 
if test -z "$accept" ; then
        exit
fi
fi
if test  "$accept" != "y" ; then
        exit
fi

ver=`/usr/bin/uname -a | cut -d ' ' -f3`
export ver
if [ $ver != "5.6" ]
then
        rm -f $EG_HOME/lib/librt.so*
        rm -f $EG_HOME/lib/libgen.so*
        rm -f $EG_HOME/lib/libpthread.so*
fi

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

if [ -z "$silentInstall" ]
then
	clear
fi
#TMPDIR check this enviroment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
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: \c"
while :
do
        read hostName
        if [ -z "$hostName" ]
        then
                echo "Enter the hostname (or IP address) of the eG Manager:\c"
        else
                break
        fi
done

echo "Enter the port number of the eG Manager [7077]: \c"
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] : \c"
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: \c"
        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): \c"
                fi
        done

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

        echo "Do you need authentication for the proxy? y/n [n] : \c"
        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 : \c"
                FLAG="y"
                while [ "$FLAG" = "y" ]
                do
                        read proxyUsername
                        if [ -n "$proxyUsername" ]
                        then
                                FLAG="n"
                        else
                                echo "Please enter a valid username for the proxy : \c"
                        fi
                done

                FLAG="y"
                while [ "$FLAG" = "y" ]
                do
                        stty -echo
                        echo "Please enter the password for $proxyUsername : \c"
                        read proxyPassword
                        echo " "
                        if [ -z "$proxyPassword" ]
                        then
                                echo "Sorry, Invalid password! Please try again ... "
                        else
                                echo "Please re-enter the password for $proxyUsername: \c"
                                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 "Please enter the location of your Java home directory [$java_home]: \c"
#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" != "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: \c"
#                fi
#        else
#                echo "Failed to locate $javaHome/bin/java ..."
#                echo "Please enter the location of your Java home directory: \c"
#	fi
#done
java_home=/opt/egurkha/jre
export java_home
javaHome=/opt/egurkha/jre
export javaHome
JAVA_HOME=$javaHome
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH

JAVA_LIB=$javaHome/lib
export JAVA_LIB

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] : \c"
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] : \c"
#	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]: \c"
#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: \c"
#		fi
#        else
#                echo "Failed to locate $javaHome/bin/java ..."
#                echo "Please enter the location of your Java home directory: \c"
#        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] : \c"
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: \c"
#	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
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 '/^#!\/bin\/sh/d'  > $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/sh" >  $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\/sh/d' > $EG_TEMP1
cat $EG_TEMP1  | sed '/^JAVA_HOME=/d'  > $EG_TEMP2
cat $EG_TEMP2 | sed '/^export JAVA_HOME/d' > $EG_TEMP1
echo "#!/bin/sh" >  $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`
case "$tempHostName" in
  *:*) tempHostName=`echo "["$tempHostName"]"`
esac
#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`
isJRE18=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.8" | wc -l`
#export isJRE16 
export isJRE18 
export isJRE17
javaCmd="java -Xrs -Deg.name=EgMainAgent "
#if [ "$isJRE16" -ge "1" ]
#then
	#javaCmd="java -Xrs -Deg.name=EgMainAgent -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null "
#fi
if [ "$isJRE17" -ge "1" ]
then
	javaCmd="java -Xrs -Deg.name=EgMainAgent -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateMinidumpOnCrash "
fi
if [ "$isJRE18" -ge "1" ]
then
	javaCmd="java -Xrs -Deg.name=EgMainAgent -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateMinidumpOnCrash "
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

PATH=/opt/egurkha/bin:$JAVA_HOME/bin:/usr/bin:/sbin:$PATH:/bin
export PATH
echo "#!/bin/sh" > $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 "#EXTSHM=ON" >> $EG_HOME/bin/start_agent
echo "#export EXTSHM" >> $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 "LIBPATH=$EG_HOME/lib:\$LIBPATH" >> $EG_HOME/bin/start_agent
echo "export LIBPATH" >> $EG_HOME/bin/start_agent
echo "ulimit -c 0" >> $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 "LANG=en_US" >> $EG_HOME/bin/start_agent
echo "export LANG" >> $EG_HOME/bin/start_agent
echo "CLASSPATH=" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $EG_HOME/bin/start_agent
echo "CLASSPATH=.:$EG_CLASSPATH:\$EG_HOME/lib/xercesImpl.jar:\$EG_HOME/lib/xmlParserAPIs.jar:\$EG_HOME/lib/jsse.jar:\$EG_HOME/lib/jcert.jar:\$EG_HOME/lib/jnet.jar:\$EG_HOME/lib/mail.jar:\$EG_HOME/lib/activation.jar:\$EG_HOME/lib/pop3.jar:\$EG_HOME/lib/ldapjdk.jar:\$EG_HOME/lib/eg_plus.jar:\$CLASSPATH" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $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:\$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 "PATH=.:\$JAVA_HOME/bin:\$EG_HOME/bin:\$EG_HOME/bin/ic:/sbin:/usr/bin:/bin:/usr/sbin:/usr/local/bin:\$PATH" >>$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 "/opt/egurkha/bin/stop_agent 0" >> $EG_HOME/bin/start_agent

echo "XMX=$XMX" >> $EG_HOME/bin/start_agent
echo "export XMX" >> $EG_HOME/bin/start_agent

echo "" >> $EG_HOME/bin/start_agent
echo "IBM_HEAPDUMP=false" >> $EG_HOME/bin/start_agent
echo "export IBM_HEAPDUMP" >> $EG_HOME/bin/start_agent
echo "IBM_HEAPDUMP_OUTOFMEMORY=false" >> $EG_HOME/bin/start_agent
echo "export IBM_HEAPDUMP_OUTOFMEMORY" >> $EG_HOME/bin/start_agent
echo "" >> $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

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=/opt/egurkha/lib/eg_agent.jar:/opt/egurkha/lib/eg_util.jar:/opt/egurkha/lib/xml-apis.jar:\$CLASSPATH" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH " >> $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
echo "cd /opt/egurkha" >> $EG_HOME/bin/start_agent
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 "export proxyUsername proxyPassword" >> $EG_HOME/bin/start_agent
                echo "nohup $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
		echo "proxyUsername=$proxyUsername" >> $sfile
		echo "proxyPassword=$proxyPassword" >> $sfile
        else
                echo "nohup $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
else
        echo "nohup $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
echo "ver=\"\`java com.eg.EgInstallInfo\`\"" >> $EG_HOME/bin/start_agent
echo "export ver" >> $EG_HOME/bin/start_agent
echo "sleep 5" >> $EG_HOME/bin/start_agent
echo "val=\`ps -aef | grep EgMainAgent | 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
chmod +x $EG_HOME/bin/start_agent

#echo "Do you want to configure the agent now to monitor any database servers? y/n [y]\c"
#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] \c"
	#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] \c"
#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] \c"
#	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] \c"
#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] \c"
#	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] \c"
#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] \c"
#	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] \c"
#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] \c"
#	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 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 "                                  /opt/egurkha/bin/setup_cf."
#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

