#!/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 VM agent on a 32-bit operating system. Exiting..."
        exit;
	fi
fi


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

if [ -z "$silentInstall" ]
then
echo "This script will install the eG VM agent. The eG VM agent must be"
echo "installed and executed by a separate user."
echo " " 
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/egvmagent
if [ -e "$file" ]
then
	linkfile="/opt/egvmagent"
	if [ -d "$file" ]
	then
		if [ -L "$file" ]
		then
			linkfile=`readlink -f $file`
		fi
	fi
	linkfile=`echo "$linkfile" | sed 's/\/egvmagent$//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 VM agent should be installed [/opt]: "
			read userHome
			else
				userHome=$linkfile
				echo "The directory '$linkfile' exists. Installing the eG VM 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 VM agent should be installed [/opt]: "
		read userHome
		else
		userHome=$linkfile
		echo "The directory '$linkfile' exists. Installing the eG VM 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/egvmagent $userName 
	else
		/usr/sbin/useradd  -G $groupName -g $groupName -d $userHome/egvmagent $userName 
		echo "Enter new password for $userName "
		passwd $userName	
	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 eGvmagent.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}/eGvmagent_linux_x64.tar  ./egvmagent
#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/egvmagent" ]
	then
		if [ -z "$silentInstall" ]
		then
			echo "Removing an existing link at /opt/egvmagent"
		fi
		rm -f /opt/egvmagent
		ln -s "$userHome/egvmagent" /opt/egvmagent
	else 
		if [ -d "/opt/egvmagent" ] 
		then
		echo "*******************************************************"
		echo "ERROR: The directory /opt/egvmagent already exists!"
		echo "Please delete this directory and run the command ..."
		echo "		ln -s $userHome/egvmagent /opt/egvmagent "
		echo "The eG VM agent will not start until this change is made."
		echo "*******************************************************"
		else
			ln -s "$userHome/egvmagent" /opt/egvmagent
		fi
	fi
fi

cd ${userHome}/egvmagent
curDir=`pwd`
if [ "$curDir" != "$userHome/egvmagent" ]
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/egvmagent - found $dir/egvmagent"
			echo "Exiting ..."
			exit 0
		fi
	fi
fi

#if [ -f /bin/ksh ]
#then
#	cp /bin/ksh ${userHome}/egvmagent/bin/ksh
#fi
rm -f  ${userHome}/egvmagent/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

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

cd ${userHome}/egvmagent/bin

# Giving permissions to make  these directories  by other components
# such as webservers
chmod 750 ${userHome}/egvmagent/logs
chmod 750 ${userHome}/egvmagent/config

# installing  auto_restart  feature for egurkha agent
if [ -z "$silentInstall" ]
then
	echo "Would you like the eG VM 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/egvmagent/bin/egvmagent.service | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_01
	mv /tmp/eg_tmp_01 /opt/egvmagent/bin/egvmagent.service
	chmod 644 /opt/egvmagent/bin/egvmagent.service
	if [ $val = "Y" -o $val = "y"  ]
	then
        cp /opt/egvmagent/bin/egvmagent.service /etc/systemd/system/
		systemctl daemon-reload
		systemctl enable egvmagent.service
	fi
else
cat /opt/egvmagent/bin/rc.egvmagent | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_01
mv /tmp/eg_tmp_01 /opt/egvmagent/bin/rc.egvmagent
chmod +x /opt/egvmagent/bin/auto_restart 
if [ $val = "Y" -o $val = "y"  ]
then
        /opt/egvmagent/bin/auto_restart install agent
fi
fi

if [ -z "$silentInstall" ]
then
echo "********************************************************"
echo "The eG VM agent has been successfully installed!"
echo "Please login as $userName and run the script " 
echo "          /opt/egvmagent/bin/setup_vmagent "
echo "to configure the VM agent." 
echo "********************************************************"
fi

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


