#!/bin/sh

userName=$1
#userPass=$2
groupName=$2
userHome=$3
autoRestart=$4

silentInstall=$autoRestart

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


curDir=`pwd`
if [ ! -f "$curDir/eGagent_hpux_itanium.tar" ]
then
	if [ ! -f "$curDir/eGagent_hpux_itanium.tar.gz" ]
	then
        	echo "Failed to find $curDir/eGagent_hpux_itanium.tar!"
        	exit 1
        fi
    	gunzip eGagent_hpux_itanium.tar.gz
	if [ ! -f "$curDir/eGagent_hpux_itanium.tar" ]
	then
        	echo "Failed to find $curDir/eGagent_hpux_itanium.tar!"
        	exit 1
        fi
fi
clear

currentDir=`pwd`
export currentDir
if [ -z "$silentInstall" ]
then
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."
fi

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 ]
then
	mkdir /opt
	chmod 777 /opt
	chown root /opt
	chgrp bin /opt
fi

if [ -z "$silentInstall" ]
then
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
fi
createUser="true"
# Checking whether the user name is exist or not #
for name in `cat $userFile`
do
	if [ "$name" = "$userName" ]
	then
		createUser="false"
		if [ -z "$silentInstall" ]
		then
		echo "User $userName already exists; continuing ..."
		fi
	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
	if [ -z "$silentInstall" ]
	then
	echo "Enter the group to which the eG user is to be associated [egurkha]: " > /dev/tty
	read groupName < /dev/tty
	fi

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

if [ -z "$silentInstall" ]
then
echo "The installer will now install the eG agent..."
echo "Press <CTRL+C> to stop the installation or press <Enter> to continue "
read jun01
fi

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

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 $userHome/egurkha -m $userName 
	echo "Enter new password for $userName "
	passwd $userName	
fi

curDir=`pwd`
cd $userHome
#cd /opt
#echo "tar -xvf ${curDir}/eGagent_hpux_itanium.tar ./egurkha"
tar -xvf ${curDir}/eGagent_hpux_itanium.tar egurkha
if [ "$userHome" != "/opt" ]
then
	rm -f /opt/egurkha 2>/dev/null
	ln -s $userHome/egurkha /opt/egurkha
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 $userHome/egurkha
chown -R $userName $userHome/egurkha
chgrp -R $groupName $userHome/egurkha

chown $userName $userHome/egurkha
chgrp $groupName $userHome/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

if [ -z "$silentInstall" ]
then
# 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
else
	val=$autoRestart
fi

cat /opt/egurkha/bin/rc.egurkha | sed "s/^.*start_agent.*\$/su - $userName -c \/opt\/egurkha\/bin\/start_agent \&/" > /tmp/eg_tmp_01
cat /tmp/eg_tmp_01 | sed "s/^.*stop_agent.*\$/su - $userName -c \/opt\/egurkha\/bin\/stop_agent \&/" > /tmp/eg_tmp_02
mv /tmp/eg_tmp_02 /opt/egurkha/bin/rc.egurkha
rm -f /tmp/eg_tmp_01
chmod +x /opt/egurkha/bin/auto_restart
if [ $val = "Y" -o $val = "y"  ]
then
	/opt/egurkha/bin/auto_restart install
fi

if [ -z "$silentInstall" ]
then
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 "********************************************************"
fi

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


