#!/bin/sh

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

systemuser=0
if [ $# -eq 1 ]
then
	if [ "$1" = "-s" ]
	then
		systemuser=1
	fi	
else
if [ "$sysuser" = "yes" ]
then
	systemuser=1
fi
fi
export systemuser

SUDOERS=/etc/sudoers
export SUDOERS

silentInstall=$autoRestart

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

if [ -f "/bin/arch" ]
then
	arch=`/bin/arch`
else 
if [ -f "/usr/bin/arch" ]
then
	arch=`/usr/bin/arch`
else
	arch=`arch`
fi
fi

if echo "$arch" | grep -q "_64"
then
        dummy=1
        #echo "64 bit"
else
	if echo "$arch" | grep -q "64le"
	then
		dummy=1
		#echo "ppc64le"
	else
        echo "Error: Cannot install the 64-bit agent on a 32-bit operating system. Exiting..."
        exit;
	fi
fi

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

if [ -z "$silentInstall" ]
then
# read the user name and validate it, with default user name option #
userHome="null"
userPasswd="null"
fi

userFile=${TMPDIR}/eg_install_user
groupFile=${TMPDIR}/eg_install_group

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

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 775 /opt
 	chown root /opt
	chgrp $groupToUse /opt
fi

createUser="true"

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

line=`echo $userName | grep "@" | wc -l` 
if [ $line -gt 0 ]
then
#domain user
	createUser=false
	userHome=/opt
	break
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
done
fi

file=/opt/egurkha
if [ -e "$file" ]
then
	linkfile="/opt/egurkha"
	if [ -d "$file" ]
	then
		if [ -L "$file" ]
		then
			linkfile=`readlink -f $file`
		fi
	fi
	linkfile=`echo "$linkfile" | sed 's/\/egurkha$//g'`
fi

# 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 ..."
			if [ -z "$linkfile" ]
			then
			echo "Enter the directory in which the eG agent should be installed [/opt]: "
			read userHome
			else
				userHome=$linkfile
				echo "The directory '$linkfile' exists. Installing the eG agent in this folder..."
			fi
		fi
		if [ -z "$userHome" ]
		then
			userHome="/opt"
		fi	
	fi
done

if [ "$createUser" = "true" ]
then
	if [ -z "$silentInstall" ]
	then
		if [ -z "$linkfile" ]
		then
		echo "Enter the directory in which the eG agent should be installed [/opt]: "
		read userHome
		else
		userHome=$linkfile
		echo "The directory '$linkfile' exists. Installing the eG agent in this folder..."
		fi
	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
	# KARTHIK HERE #

	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/sbin/groupadd ]
	then
	echo "/usr/sbin/groupadd does not exist. User creation will fail! Exiting ..."
	exit 0
	fi
	/usr/sbin/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
	if [ "$systemuser" -eq "1" ]
	then
		/usr/sbin/useradd -r -G $groupName -g $groupName -d $userHome/egurkha $userName 
	else
		/usr/sbin/useradd  -G $groupName -g $groupName -d $userHome/egurkha $userName 
		echo "Enter new password for $userName "
		passwd $userName	
	fi
fi

#Find the group ID of the Docker group on the Host
FILE=/var/run/docker.sock
if [ -e $FILE ]
then
	getgroupid=`stat -c %g /var/run/docker.sock`
	#echo $getgroupid
	usermod -aG $getgroupid $userName
fi


if [ -f "/usr/bin/esxtop" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/bin/esxtop" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/bin/esxtop" >> ${SUDOERS}
		echo "$userName ALL=NOPASSWD:/opt/egurkha/lib/vmgfiles/getinfo.pl" >> ${SUDOERS}
		echo "$userName ALL=NOPASSWD:/opt/egurkha/lib/vmgfiles/getinfo.sh" >> ${SUDOERS}
		echo "$userName ALL=NOPASSWD:/usr/sbin/vdf" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/sbin/vgdisplay" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/sbin/vgdisplay" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/sbin/vgdisplay" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/sbin/crm_mon" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/sbin/crm_mon" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/sbin/crm_mon" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/sbin/pcs" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/sbin/pcs" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/sbin/pcs" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/bin/docker" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/bin/docker" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/bin/docker" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/bin/iotop" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/bin/iotop" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/bin/iotop" >> ${SUDOERS}
	fi
fi

if [ -f "/usr/sbin/iotop" ]
then
	cnt=`grep "$userName ALL=NOPASSWD:/usr/sbin/iotop" ${SUDOERS} | wc -l`
	cnt=`echo $cnt | sed 's/ //g'`
	#echo "CNT=$cnt"
	if [ $cnt -eq 0 ]
	then
		echo "$userName ALL=NOPASSWD:/usr/sbin/iotop" >> ${SUDOERS}
	fi
fi

cd $userHome
# start Extracting files from eGmanager.tar #
curDir=`pwd`
if [ "$curDir" != "${userHome}" ]
then
	echo "ERROR: Encountered a problem changing to the eG directory!"
	echo "Exiting ..."
	exit 0
fi
tar -xvf ${currentDir}/eGagent_linux_x64.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

#if [ -f /bin/ksh ]
#then
#	cp /bin/ksh ${userHome}/egurkha/bin/ksh
#fi
rm -f ${userHome}/egurkha/bin/ksh

ia64=`uname -a | grep ia64 | wc -l`
if [ "$ia64" -ne "1" ]
then
	ia64=`uname -a | grep 86_64 | wc -l`
fi
export ia64

if [ "$ia64" -eq "1" ]
then
	if [ -f "/usr/bin/dig" ]
	then
	rm -f /opt/egurkha/bin/dig
	ln -s /usr/bin/dig /opt/egurkha/bin/dig 2>/dev/null >/dev/null
	fi	
	#cp /usr/bin/dig /opt/egurkha/bin 2>/dev/null > /dev/null
	cp /opt/egurkha/bin/bin64/* /opt/egurkha/bin 2>/dev/null > /dev/null
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
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

l=`which systemctl | wc -l`
export l
if [ "$l" -gt 0 ] && [ -d /etc/systemd/system ]
then
	cat /opt/egurkha/bin/egagent.service | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_01
	mv /tmp/eg_tmp_01 /opt/egurkha/bin/egagent.service
	chmod 644 /opt/egurkha/bin/egagent.service
	if [ $val = "Y" -o $val = "y"  ]
	then
        cp /opt/egurkha/bin/egagent.service /etc/systemd/system/
		systemctl daemon-reload
		systemctl enable egagent.service
	fi
else
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
fi

if [ -d /opt/egurkha/jre1.7 ]
then
isJRE17=`/opt/egurkha/jre1.7/bin/java -version 2>&1 | grep "12." | wc -l`
if [ $isJRE17 -ge 1 ]
then
	rm -rf /opt/egurkha/jre
	#rm -rf /opt/egurkha/jre1.3
	mv /opt/egurkha/jre1.7 /opt/egurkha/jre
else
	#echo "Would have removed ..."
	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

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


