#!/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_aix.tar" ]
then
	if [ ! -f "$curDir/eGagent_aix.tar.gz" ]
	then
        	echo "Failed to find $curDir/eGagent_aix.tar!"
        	exit 1
        fi
    gunzip eGagent_aix.tar.gz
	if [ ! -f "$curDir/eGagent_aix.tar" ]
	then
        	echo "Failed to find $curDir/eGagent_aix.tar!"
        	exit 1
        fi
fi
clear

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

max=`lsattr -El sys0 | grep max_logname |  awk '{ print $2}'`
if [ -z "$max" ]
then
	max=99999
fi
while :
do
echo "Enter the name of the eG user [egurkha]: "   
read userName 
if test  ! -n "$userName" ; then
	userName="egurkha"		
fi

wordcount=0
for words in $userName
do
        wordcount=`expr $wordcount + 1`
done

spacecheck=`expr $wordcount \> 1`
if [ $spacecheck = "1" ]
then
	echo ""
        echo "Please enter a user name with no space between characters."
	echo ""
	continue
fi

length=`expr length $userName`
lengthcheck=`expr $length \>= $max`
if [ $lengthcheck = "1" ]
then
	echo ""
        echo "Please enter a user name whose length is < $max characters."
	echo ""
	continue
fi

if [ "$spacecheck" -eq  "0" ]
then
	if [ "$lengthcheck" -eq "0" ]
	then
		break
	fi
fi
done

# read the user name and validate it, with default user name option #
userHome="null"
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 ..."
			echo "Enter the directory in which the eG agent should be installed [/opt]: "
			read userHome
		fi
		if [ -z "$userHome" ]
		then
			userHome="/opt"
		fi	
	fi
done

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

if [ ! -d "$userHome" ]
then
	mkdir $userHome
fi

if [ ! -d "$userHome" ]
then
	echo "Failed to create directory: $userHome!"
	echo "Please rerun this script with a different installation directory!"
	exit 0;
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 ..." 
	if [ ! -f /usr/bin/mkgroup ]
	then
	echo "/usr/bin/mkgroup does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	/usr/bin/mkgroup $groupName
fi
if test "$createUser" = "true" ; then
	echo "Creating user $userName belonging to group $groupName ..." 
	mkdir -p $userHome	
	if [ ! -f /usr/bin/mkuser ]
	then
	echo "/usr/bin/mkuser does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	if [ ! -f /usr/bin/chuser ]
	then
	echo "/usr/bin/chuser does not exist. User creation will fail! Exiting ..."
	exit 0
	fi

	/usr/bin/mkuser pgrp=$groupName $userName
	/usr/bin/chuser home=$userHome/egurkha $userName
	echo "Enter new password for $userName "
	passwd $userName	
fi

cd $userHome
curDir=`pwd`
if [ "$curDir" != "${userHome}" ]
then
	dir=$curDir/
	if [ "$dir" != "${userHome}" ]
	then
	echo "ERROR: Encountered a problem changing to the eG directory!"
	echo "Exiting ..."
	exit 0
	fi
fi


#currentDir=`pwd`
# start Extracting files from eGmanager.tar #
#mv ${currentDir}/eGagent ${currentDir}/eGagent.tar
tar -o -xvf ${currentDir}/eGagent_aix.tar ./egurkha 
#chown $userName *
#chgrp $groupName *
#chmod 755 *
#if the home is not /opt then creating link to /opt from  $userHome #
if test "$userHome" != "/opt" ; then
	if [ -L "/opt/egurkha" ]
	then
		if [ -z "$silentInstall" ]
		then
			echo "Removing an existing link at /opt/egurkha"
		fi
		rm -f /opt/egurkha
		ln -s "$userHome/egurkha" /opt/egurkha
	else 
		if [ -d "/opt/egurkha" ] 
		then
		echo "*******************************************************"
		echo "ERROR: The directory /opt/egurkha already exists!"
		echo "Please delete this directory and run the command ..."
		echo "		ln -s $userHome/egurkha /opt/egurkha "
		echo "The eG agent will not start until this change is made."
		echo "*******************************************************"
		else
			ln -s "$userHome/egurkha" /opt/egurkha
		fi
	fi
fi
cd ${userHome}/egurkha
curDir=`pwd`

if [ "$curDir" != "$userHome/egurkha" ]
then
	dir=`dirname $curDir`
	if [ "$dir" != "$userHome" ]
	then
		dir=$dir/
		if [ "$dir" != "$userHome" ]
		then
			echo "ERROR: Encountered a problem changing to the eG directory!"
			echo "Expected to be in $userHome/egurkha - found $dir/egurkha"
			echo "Exiting ..."
			exit 0
		fi
	fi
fi

#changing the egurkha directory #
chmod -R 755 ${userHome}/egurkha
chown -R $userName ${userHome}/egurkha
chgrp -R $groupName ${userHome}/egurkha

cd ${userHome}/egurkha/bin
chown root fping
#chown root netprobe
chgrp bin fping
#chgrp bin netprobe
# giving super user permissions to these files #
chmod +s fping
#chmod +s netprobe
# Giving permissions to make  these directories  by other components
# such as webservers
chmod 777 ${userHome}/egurkha/agent/logs
chmod 777 ${userHome}/egurkha/agent/config
chmod 777 ${userHome}/egurkha/agent/data

# installing  auto_restart  feature for egurkha agent
if [ -z "$silentInstall" ]
then
	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/xxxx/$userName/g" > /tmp/eg_tmp_01
mv /tmp/eg_tmp_01 /opt/egurkha/bin/rc.egurkha
chmod +x /opt/egurkha/bin/auto_restart 
if [ $val = "Y" -o $val = "y"  ]
then
        /opt/egurkha/bin/auto_restart install
fi 

if [ -d /opt/egurkha/jre1.6 ]
then
isJRE16=`/opt/egurkha/jre1.6/bin/java -version 2>&1 | grep "version" | wc -l`
if [ "$isJRE16" -eq "1" ]
then
	rm -rf /opt/egurkha/jre
	#rm -rf /opt/egurkha/jre1.3
	mv /opt/egurkha/jre1.6 /opt/egurkha/jre
#else
#isJRE15=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "version" | wc -l`
#if [ "$isJRE15" -eq "0" ]
#then
	#rm -rf /opt/egurkha/jre
	#rm -rf /opt/egurkha/jre1.6
	#mv /opt/egurkha/jre1.3 /opt/egurkha/jre
#fi
fi
fi

SUDOERS=/etc/sudoers
export SUDOERS

if [ -f $SUDOERS ]
then
	cnt=`grep "$userName ALL=(ALL) NOPASSWD:/usr/bin/kill" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=(ALL) NOPASSWD:/usr/bin/kill -3 *,/usr/bin/rm -rf /*/egi_heapdump*.phd" >> ${SUDOERS}
	fi
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


