#!/bin/sh

currentUser=`whoami`
if [ "$currentUser" != "root" ]
then
        echo "******************************************************************************"
        echo "This script can only be run by the root user. Exiting ..."
        echo "******************************************************************************"
        exit
fi

XMX="-Xmx512m"
#Xmx to use for the agent

eGuser=egurkha
#eG user - please make sure that the user account exists

eGgroup=egurkha
#eG user's group

serviceaccount=no
#whether the user we are going to create is a service account or a normal account

eGInstallDir=/opt
#eG install directory

autoRestart=y
#whether the agent should auto-restart

licenseAcceptance=y
#licenseAcceptance the license

eGManager=192.168.10.54
#eG manager host

eGManagerPort=7077
#eG manager port

useProxy=n
#use Proxy?

proxyHost=xxx
#proxy server

proxtPort=8080
#proxy port

setAuthentication=n
#use auhentication for proxy?

proxyUsername=none
#user name for proxy access - none

proxyPassword=none
#password for proxy access

useSSL=n
#use SSL for communication with the manager? y/n

trustedCertificates=n
#use trusted certificates for SSL communication with the manager? y/n

setNickName=n
#set the nickname for the agent - y/n

nickNameToUse=`hostname`
#the nick name to set for this agent

count=`grep "^$eGgroup:" /etc/group | wc -l`
if [ $count -eq 0 ]
then
	if [ ! -f /usr/sbin/groupadd ]
	then
	echo "/usr/sbin/groupadd does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	/usr/sbin/groupadd $eGgroup
fi

count=`grep "^$eGuser:" /etc/passwd | wc -l`
if [ $count -eq 0 ]
then
	if [ ! -f /usr/sbin/useradd ]
	then
	echo "/usr/sbin/useradd does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	if [ "$serviceaccount" = "yes" ]
	then
		/usr/sbin/useradd -r -G $eGgroup -g $eGgroup -d $eGInstallDir/egurkha $eGuser 
	else
		/usr/sbin/useradd -G $eGgroup -g $eGgroup -d $eGInstallDir/egurkha $eGuser 
	fi
fi

#run iAgent first
if [ ! -f "iAgent_linux" ]
then
        if [ -f "iAgent_linux.sh" ]
        then
                cp -f iAgent_linux.sh iAgent_linux
                chmod 755 iAgent_linux
        fi
        if [ -f "iAgent_linux_x64.sh" ]
        then
                cp -f iAgent_linux_x64.sh iAgent_linux_x64
                chmod 755 iAgent_linux_x64
        fi
fi
if [ ! -f "iAgent_linux_x64.sh" ]
then
        if [ -f "iAgent_linux.sh" ]
        then
                cp -f iAgent_linux.sh iAgent_linux
                chmod 755 iAgent_linux
        fi
        if [ -f "iAgent_linux_x64.sh" ]
        then
                cp -f iAgent_linux_x64.sh iAgent_linux_x64
                chmod 755 iAgent_linux_x64
        fi
fi

script=
if [ -f "iAgent_linux" ]
then
	script="iAgent_linux"
elif [ -f "iAgent_linux_x64" ]
then
	script="iAgent_linux_x64"
fi

if [ -z "$script" ]
then
	echo "Script file iAgent_linux not found! ..."
	exit 0
fi

chmod +x $script
`pwd`/$script $eGuser $eGgroup $eGInstallDir $autoRestart $serviceaccount >/dev/null 

monitorColdfusion=n
#monitor coldfusion servers?
monitorSybase=n
#monitor sybase servers?
# silent install for coldfusion and sybase is not handled here - pls manually configure these servers

#run setup_agent next
chmod +x $eGInstallDir/egurkha/bin/setup_agent
su - $eGuser -c "$eGInstallDir/egurkha/bin/setup_agent $licenseAcceptance $eGManager $eGManagerPort $useProxy $proxyHost $proxtPort $setAuthentication $proxyUsername $proxyPassword $useSSL $trustedCertificates $setNickName $monitorColdfusion $monitorSybase $eGuser $eGgroup $nickNameToUse $XMX" > /dev/null

#now start the agent
su - $eGuser -c $eGInstallDir/egurkha/bin/start_agent

echo "eG Agent installation and start-up completed."

