#!/bin/sh 

currentUser=`whoami`
if [ "$currentUser" != "root" ]
then
	echo "Your user name is '$currentUser'. Only the root user can install and execute this file!"
	exit
fi
curDir=`pwd`
if [ ! -f "$curDir/eGCLI.tar" ]
then
	if [ ! -f "$curDir/eGCLI.tar.gz" ]
	then
        	echo "Failed to find $curDir/eGCLI.tar!"
        	exit 1
        fi
    gunzip eGCLI.tar.gz
	if [ ! -f "$curDir/eGCLI.tar" ]
	then
        	echo "Failed to find $curDir/eGCLI.tar!"
        	exit 1
        fi
fi
clear

echo "This script will install the eG CLI."

currentDir=`pwd`
#TMPDIR check this enviroment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
fi
if [ ! -d /opt ]
then
	mkdir /opt
	chmod 777 /opt
	chown root /opt
	chgrp bin /opt
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

echo "The eG CLI must be executed by a separate user. If you have"
echo "already installed the eG manager/agent on this system, please"
echo "use the same user account for the eG CLI installation."
echo " "

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 \> 8`
if [ $lengthcheck = "1" ]
then
	echo ""
        echo "Please enter a user name whose length is <= 8 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"

createUser="true"
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 CLI 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 CLI should be installed [/opt]: "
	read userHome
	if [ -z "$userHome" ] 
	then
		userHome="/opt"
	fi
fi

echo " "
#checking whether group egurkha exists or Not #	
check1=true
createGroup="true"
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 dafault 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 CLI..."
echo "Press <Enter> to continue, or <CTRL-C> to stop the installation"
read jun01

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 ..." 
	mkdir -p $userHome	
	/usr/sbin/useradd -G $groupName -g $groupName -d $userHome/egurkha $userName 
	#echo "Enter new password for $userName "
	passwd $userName	
fi
if [ ! -d $userHome ]
then
        mkdir -p $userHome
fi

rm -f /tmp/eg_temp1
rm -f /tmp/eg_temp2 # temp files used in setup_manager
cd $userHome
# start Extracting files from eGCLI.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}/eGCLI.tar  egurkha
#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 CLI will not start until this change is made."
		echo "*********************************************************"
		else
			ln -s "$userHome/egurkha" /opt/egurkha
		fi
	fi
fi

#changing the egurkha directory Recursively #

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

chmod -R 755 ${userHome}/egurkha
chown -R $userName ${userHome}/egurkha
chgrp -R $groupName ${userHome}/egurkha 

# some special permissions given to the fping and netprobe files #

echo "**************************************************************"
echo "The eG CLI has been successfully installed!"
echo "**************************************************************"

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


