#!/bin/sh


curDir=`pwd`
if [ ! -f "$curDir/eGAgent_hpux.depot" ]
then
        echo "Failed to find $curDir/eGAgent_hpux.depot!"
        exit 1
fi
clear

currentDir=`pwd`
export currentDir
swreg -l depot $currentDir/eGAgent_hpux.depot
swinstall -s $currentDir/eGAgent_hpux.depot  
#cp ./eGAgent.depot /var/spool/sw/#
#swreg -l depot /var/spool/sw/eGAgent.depot#
#swinstall -s /var/spool/sw/eGAgent.depot#

currentUser=`whoami`
if [ "$currentUser" != "root" ]
then
	echo "Your user name is '$currentUser'. Only root can install and execute this file!"
	exit
fi

echo "This script will install the eG agent. The eG agent must be"
echo "installed and executed by a separate user."
echo " " 
echo "If you have installed the eG manager on the same system, you must"
echo "use the same user and the same installation directory for the manager"
echo "and the agent."

currentDir=`pwd`
#TMPDIR check this environment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
fi

userFile=${TMPDIR}/eg_install_user
groupFile=${TMPDIR}/eg_install_group
if [ -f $userFile ]
then
        rm -f $dataFile
fi
if [ -f $groupFile ]
then
        rm -f $dataFile
fi
cat /etc/passwd | cut -f1 -d ':' > $userFile
cat /etc/group | cut -f1 -d ':' > $groupFile

if [ ! -d /opt/egurkha ]
then
	echo "The directory /opt/egurkha does not exist ..."
	echo "eG Agent Installation Failed ..."
	exit 

fi

echo "Enter the name of the eG user [egurkha]: "   
read userName 
# read the user name and validate it, with default user name option #
userHome="/opt/egurkha"
userPasswd="null"

if test  ! -n "$userName" ; then
	userName="egurkha"		
fi
createUser="true"
# Checking whether the user name is exist or not #
for name in `cat $userFile`
do
	if [ "$name" = "$userName" ]
	then
		createUser="false"
		echo "User $userName already exists; continuing ..."
	fi
done

#if [ "$createUser" = "true" ]
#then
#	echo "Enter the directory in which the eG agent is installed [/opt]: "
#	read userHome
#	if [ -z "$userHome" ] 
#	then
#		userHome="/opt"
#		
#	fi
#fi

echo " "
#checking whether group egurkha exists or Not #	
check1=true
createGroup="true"
groupName=" "
if [ "$createUser" = "true" ]
then

	echo "Enter the group to which the eG user is to be associated [egurkha]: " > /dev/tty
	read groupName < /dev/tty

	# if groupName doesn't have the valid data default value is assigned #
	if test ! -n "$groupName" ; then
		groupName="egurkha"
	fi

	for gname in `cat $groupFile`
	do
		if [ "$gname" = "$groupName" ]
		then
			echo "$groupName exists already; continuing ..."
			createGroup="false"
		fi
	done
else
	# if there is no need to create user then there is no need to create group also #
	createGroup="false"
	groupName=`id -n -g $userName`
	echo "Group  for the $userName = $groupName "
fi

#echo "Creating links and changing the file permissions ... "
#echo "userName = $userName"
#echo "groupName = $groupName"
#echo "createGroup = $createGroup"
#echo "createUser = $createUser"
#echo "userHome = $userHome"

echo "The installer will now install the eG agent..."
echo "Press <CTRL+C> to stop the installation or press <Enter> to continue "
read jun01

if test "$createGroup" = "true" ; then
	echo "Creating new group $groupName ..." 
	/usr/sbin/groupadd $groupName
fi
chgrp -R $groupName /opt/egurkha

if test "$createUser" = "true" ; then
	echo "Creating user $userName belonging to group $groupName ..." 
	if [ ! -f /usr/sbin/useradd ]
	then
	echo "/usr/bin/useradd does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	#mkdir -p $userHome	
	/usr/sbin/useradd -G $groupName -g $groupName -d /opt/egurkha -m $userName 
	echo "Enter new password for $userName "
	passwd $userName	
fi

cd /opt/egurkha
curDir=`pwd`
if [ "$curDir" != "/opt/egurkha" ]
then
	echo "ERROR: Encountered a problem changing to the eG directory!"
	echo "Exiting ..."
	exit 0
fi
     
#changing the egurkha directory #
chmod -R 755 /opt/egurkha
chown -R $userName /opt/egurkha
chgrp -R $groupName /opt/egurkha

cd /opt/egurkha/bin
chown root fping
chown root fping6
#chown root netprobe
chgrp bin fping
chgrp bin fping6
#chgrp bin netprobe
# giving super user permissions to these files #
chmod +s fping
chmod +s fping6
#chmod +s netprobe



# Giving permissions to make  these directories  by other components
# such as webservers
chmod 777 /opt/egurkha/agent/logs
chmod 777 /opt/egurkha/agent/config
chmod 777 /opt/egurkha/agent/data


# installing  auto_restart  feature for egurkha agent
echo "Would you like the eG agent to auto-restart on system boot-up? y/n [n]:"
read val
if [ -z "$val" ]
then
	val="N"
fi
cat /opt/egurkha/bin/rc.egurkha | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_01
mv /tmp/eg_tmp_01 /opt/egurkha/bin/rc.egurkha

if [ $val = "Y" -o $val = "y"  ]
then
	/opt/egurkha/bin/auto_restart install
fi

echo "********************************************************"
echo "The eG agent has been successfully installed!"
echo "Please login as $userName and run the script " 
echo "          /opt/egurkha/bin/setup_agent "
echo "to configure the agent." 
echo "********************************************************"

# Removing the temporary files in /tmp directory
rm -f $userFile $groupFile


