#!/bin/sh

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

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

bin=`grep "^bin:" /etc/group | wc -l`
groupToUse=bin
if [ $bin = "0" ]
then
	groupToUse=root
fi

if [ ! -d /opt ]
then
	mkdir /opt
	chmod 775 /opt
 	chown root /opt
	chgrp $groupToUse /opt
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 \> 32`
#if [ $lengthcheck = "1" ]
#then
	#echo ""
        #echo "Please enter a user name whose length is <= 32 characters."
	#echo ""
	#continue
#fi

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

# read the user name and validate it, with default user name option #
userHome="null"
userPasswd="null"

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 ..."
		echo "Enter the directory in which the eG agent should be installed [/opt]: "
		read userHome
		if [ -z "$userHome" ]
		then
			userHome="/opt"
		fi	
	fi
done

if [ "$createUser" = "true" ]
then
	echo "Enter the directory in which the eG agent should be installed [/opt]: "
	read userHome
	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

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

cd $userHome
# start Extracting files from eGmanager.tar #
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
tar -xvf ${currentDir}/eGagent_linux.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
		echo "Removing an existing link at /opt/egurkha"
		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


if [ -f /bin/ksh ]
then
	cp /bin/ksh ${userHome}/egurkha/bin/ksh
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 $groupToUse fping
#chgrp $groupToUse netprobe
# giving super user permissions to these files #
chmod +s fping
#chmod +s netprobe

chown root fping6
chgrp $groupToUse fping6
chmod +s fping6


# 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

chmod +x /opt/egurkha/lib/vmgfiles/*

# 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
# chmod +x /opt/egurkha/bin/auto_restart 
# if [ $val = "Y" -o $val = "y"  ]
# then
        # /opt/egurkha/bin/auto_restart install agent
# fi

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

if [ -d /opt/egurkha/syslog ]
then
chmod +x /opt/egurkha/syslog/bin/*
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
cp /bin/sh /opt/egurkha/bin/ksh
chmod +x /opt/egurkha/bin/ksh


