#!/bin/sh
accept=$1
if [ $# -ne 0 ]
then 
shift
fi
hostName=$1
if [ $# -ne 0 ]
then 
shift
fi
port=$1
if [ $# -ne 0 ]
then 
shift
fi
setProxy=$1
if [ $# -ne 0 ]
then 
shift
fi
proxyHost=$1
if [ $# -ne 0 ]
then 
shift
fi
proxyPort=$1
if [ $# -ne 0 ]
then 
shift
fi
setAuthentication=$1
if [ $# -ne 0 ]
then 
shift
fi
proxyUsername=$1
if [ $# -ne 0 ]
then 
shift
fi
proxyPassword=$1
if [ $# -ne 0 ]
then 
shift
fi
isSsl=$1
if [ $# -ne 0 ]
then 
shift
fi
trust=$1
if [ $# -ne 0 ]
then 
shift
fi
nick=$1
if [ $# -ne 0 ]
then 
shift
fi
nicks=`hostname`
cf=$1
if [ $# -ne 0 ]
then 
shift
fi
sybase=$1
if [ $# -ne 0 ]
then 
shift
fi
userName=$1
if [ $# -ne 0 ]
then 
shift
fi
groupName=$1
if [ $# -ne 0 ]
then 
	shift
	if [ -z "$1" ] 
	then
	    nicks=`hostname`
	else
	    nicks=$1
	fi
fi
if [ $# -ne 0 ]
then 
shift
fi
XMX=$1
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
export XMX

EG_TEMP1=/tmp/eg_mon1
EG_TEMP2=/tmp/eg_mon2
STARTA=/opt/egurkha/bin/starta
export EG_TEMP1 EG_TEMP2 STARTA

cat $STARTA | sed '/^#!\/bin\/sh/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/sleep 3/d' > $EG_TEMP2
cat $EG_TEMP2 | sed '/^XMX=/d' > $EG_TEMP1
cat $EG_TEMP1 | sed '/^export XMX/d' > $EG_TEMP2
echo "#!/bin/sh" > $STARTA
echo "sleep 3" >> $STARTA
echo "XMX=$XMX" >> $STARTA
echo "export XMX" >> $STARTA
cat $EG_TEMP2 >> $STARTA
rm -f $EG_TEMP1 $EG_TEMP2

chmod +x /opt/egurkha/bin/starta

#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]\c"
read accept
if test -z "$accept" ; then
        exit
fi
fi
if test  "$accept" != "y" ; then
        exit
fi

EG_HOME=/opt/egurkha
export EG_HOME

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

if [ -d /opt/egurkha/jre1.8 ]
then
isJRE18=`/opt/egurkha/jre1.8/bin/java -version 2>&1 | grep "version" | wc -l`
#echo "isJRE18 $isJRE18"
if [ "$isJRE18" -eq "1" ]
then
	mv /opt/egurkha/jre /opt/egurkha/jre1.5
	mv /opt/egurkha/jre1.8 /opt/egurkha/jre
	rm -rf /opt/egurkha/jre1.5
fi
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

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

#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
java_home="/opt/egurkha/jre"
export java_home
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

clear
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
        #echo "Creating tmp dir"
        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 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


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
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 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


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

# 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
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:/opt/sfw/bin
export PATH

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 -ne 1 ]
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`
export isJRE10
export isJRE16 
export isJRE17
javaCmd="java -client -Xrs -Deg.name=EgMainAgent "
if [ "$isJRE16" -ge "1" ]
then
	javaCmd="java -client -Xrs -Deg.name=EgMainAgent -Djdk.http.auth.tunneling.disabledSchemes= -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null "
fi
if [ "$isJRE17" -ge "1" ]
then
	javaCmd="java -client -Xrs -Deg.name=EgMainAgent -Djdk.http.auth.tunneling.disabledSchemes= -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateMinidumpOnCrash "
fi
if [ "$isJRE10" -ge "1" ]
then
	javaCmd="java -Xrs -Deg.name=EgMainAgent --add-modules=java.xml.ws -Djavax.xml.soap.SAAJMetaFactory=com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl -Djdk.http.auth.tunneling.disabledSchemes= -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateCoredumpOnCrash "
fi
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 "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 "LD_LIBRARY_PATH=$EG_HOME/lib:\$JAVA_LIB:\$JAVA_LIB/sparc:\$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 "CLASSPATH=" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $EG_HOME/bin/start_agent
#echo "#LANG=en_US > /dev/null 2>/dev/null" >> $EG_HOME/bin/start_agent
#echo "#export LANG > /dev/null 2>/dev/null" >> $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 "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 "               if [ \$jar != \"jconn4.jar\" ]" >> $EG_HOME/bin/start_agent
echo "               then" >> $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 "               fi" >> $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=\$CLASSPATH:\$EG_HOME/lib/jconn4.jar" >> $EG_HOME/bin/start_agent
echo "export CLASSPATH" >> $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:/bin:/usr/sbin:/usr/local/bin:\$PATH:/opt/sfw/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 "/opt/egurkha/bin/stop_agent 0" >> $EG_HOME/bin/start_agent
echo "echo \"Starting the eG Agent ...\"" >> $EG_HOME/bin/start_agent

echo "unset JAVA_TOOL_OPTIONS 2>/dev/null" >> $EG_HOME/bin/start_agent
echo "unset _JAVA_OPTIONS= 2>/dev/null" >> $EG_HOME/bin/start_agent
echo "unset IBM_JAVA_OPTIONS= 2>/dev/null" >> $EG_HOME/bin/start_agent

sfile=/opt/egurkha/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 "for ck in 1 2 3 " >> $EG_HOME/bin/start_agent
echo "do" >> $EG_HOME/bin/start_agent
newcheck="";
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 "proxyUsername=$proxyUsername" >> $sfile
		echo "proxyPassword=$proxyPassword" >> $sfile
                echo "nohup /opt/egurkha/bin/runa -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
                newcheck="-manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort -proxyUsername \$proxyUsername -proxyPassword \$proxyPassword"
        else
                echo "nohup /opt/egurkha/bin/runa -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
        	newcheck="-manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust -proxyHost \$proxyHost -proxyPort \$proxyPort"
        fi
else
        echo "nohup /opt/egurkha/bin/runa -manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust > /dev/null 2>/dev/null & " >> $EG_HOME/bin/start_agent
        newcheck="-manager \$host -port \$portNo -dir /opt/egurkha -ssl $isSsl $trust "

fi
echo "sleep 20" >> $EG_HOME/bin/start_agent
echo "ver=\"\`java -client com.eg.EgInstallInfo\`\"" >> $EG_HOME/bin/start_agent
echo "export ver" >> $EG_HOME/bin/start_agent
ps="ps -aef"
if [ -f /usr/ucb/ps ]
then
        ps="/usr/ucb/ps -gauxwww"
fi
echo "val=\`/opt/egurkha/bin/count_process \"EgMain\" \"java\"\`" >> $EG_HOME/bin/start_agent
echo "if [ \$val -eq 1 ] " >>  $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 "exit"  >>  $EG_HOME/bin/start_agent
echo "fi"  >>  $EG_HOME/bin/start_agent
echo "if [ \$val -gt 1 ] "  >>  $EG_HOME/bin/start_agent
echo "then "  >>  $EG_HOME/bin/start_agent
echo "/opt/egurkha/bin/stop_agent 0" >> $EG_HOME/bin/start_agent
echo "fi" >> $EG_HOME/bin/start_agent
	echo "done" >> $EG_HOME/bin/start_agent
	echo "/opt/egurkha/bin/stop_agent 0" >> $EG_HOME/bin/start_agent
	echo "nohup $javaCmd -Deg.logback.configurationFile=/opt/egurkha/lib/eg_logback.xml -Dsun.net.inetaddr.ttl=900 -Dhttp.keepAlive=false -Doracle.jdbc.timezoneAsRegion=false EgMainAgent $newcheck > /dev/null 2>&1 & " >> $EG_HOME/bin/start_agent
	echo "sleep 20" >> $EG_HOME/bin/start_agent
	echo "val=\`$ps | grep EgMain | 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 
# for tuxedo
var=`uname -r`
if [ $var = "5.8" ] ; then
	cdir=`pwd`
	cd /opt/egurkha/bin
 	rm egtux*7 > /dev/null 2>&1
 	fromfiles=`ls egtux*`
 	for files in $fromfiles
 	do
 		tofile=`echo $files | cut -f1 -d"8"`
 		if [ -f $files ]
		then
			if [ "$files" != "$tofile" ]
			then
				mv $files $tofile
			fi
		fi
 	done
	cd $cdir
fi
if [ $var = "5.7" ] ; then
	cdir=`pwd`
	cd /opt/egurkha/bin
 	rm egtux*8 > /dev/null 2>&1
 	fromfiles=`ls egtux*`
 	for files in $fromfiles
 	do
 		tofile=`echo $files | cut -f1 -d"7"`
 		if [ -f $files ]
		then
			if [ "$files" != "$tofile" ]
			then
				mv $files $tofile
			fi
		fi
 	done
	cd $cdir
fi

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

## Since v722 : Java BTM Profiler - Logback props file - read only permission for all the users
chmod 444 /opt/egurkha/lib/apm/Java/default/btmLogging.props
chmod 444 /opt/egurkha/lib/eg_logback.xml
chmod 444 /opt/egurkha/lib/eg_logback.properties

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

cat /opt/egurkha/bin/starta | sed -e '/nohup/s/\/dev\/null /\/opt\/egurkha\/agent\/logs\/agentout /g' -e '/nohup/s/\&1 /\/opt\/egurkha\/agent\/logs\/agenterr /g'> /opt/egurkha/bin/starta_debug
cat /opt/egurkha/bin/start_agent | sed 's/runa/runa_debug/g' > /opt/egurkha/bin/start_agent_debug
cat /opt/egurkha/bin/runa | sed 's/starta/starta_debug/g' > /opt/egurkha/bin/runa_debug
chmod +x /opt/egurkha/bin/start_agent_debug /opt/egurkha/bin/runa_debug /opt/egurkha/bin/starta_debug
chmod 700 /opt/egurkha/bin/start_agent_debug
chmod 700 /opt/egurkha/bin/runa_debug
chmod 700 /opt/egurkha/bin/starta_debug

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

