#!/bin/sh 
PRODUCT=ASPlite
export PRODUCT

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

echo "This script will install the eG manager."

currentDir=`pwd`
#TMPDIR check this enviroment variable value
if test -z "$TMPDIR" ; then
         TMPDIR="/tmp"
fi

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

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 manager must be executed by a separate user. If you have"
echo "already installed the eG agent, both the manager and agent must"
echo "use the same user accounts and must be installed in the same directory."
echo " "

createUser="true"
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

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

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 manager 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 manager 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 manager..."
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 eGmanager_linux.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}/eGmanager_linux.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 manager 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

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
	cp /usr/bin/dig /opt/egurkha/bin 2>/dev/null > /dev/null
	cp /opt/egurkha/bin/bin64/fping* /opt/egurkha/bin 2>/dev/null > /dev/null
fi

if [ ! -d ${userHome}/egurkha/manager/fonts ]
then
	mkdir ${userHome}/egurkha/manager/fonts 2>/dev/null > /dev/null
	chmod 755 ${userHome}/egurkha/manager/fonts 2>/dev/null > /dev/null
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 #

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

if [ -d ${userHome}/egurkha/agent/logs ]
then
	chmod 777 ${userHome}/egurkha/agent/logs
	chmod 777 ${userHome}/egurkha/agent/config
fi

# installing  auto_restart  feature for egurkha agent
echo "Would you like the eG manager to auto-restart on system boot-up? y/n [n] :"
read val
if [ -z "$val" ]
then
        val="n"
fi

l=`which systemctl | wc -l`
export l
if [ "$l" -gt 0 ] && [ -d /etc/systemd/system ]
then
	cat /opt/egurkha/bin/egmanager.service | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_01
	mv /tmp/eg_tmp_01 /opt/egurkha/bin/egmanager.service
	chmod 644 /opt/egurkha/bin/egmanager.service
	if [ $val = "Y" -o $val = "y"  ]
	then
        cp /opt/egurkha/bin/egmanager.service /etc/systemd/system/
		systemctl daemon-reload
		systemctl enable egmanager.service
	fi
else
cat /opt/egurkha/bin/rc.egurkha_manager | sed "s/xxxx/$userName/g" > /tmp/eg_tmp_02
mv /tmp/eg_tmp_02 /opt/egurkha/bin/rc.egurkha_manager
chmod +x /opt/egurkha/bin/auto_restart
if [ $val = "Y" -o $val = "y"  ]
then
        /opt/egurkha/bin/auto_restart install manager
fi
fi

if [ -d /opt/egurkha/jdk ]
then
	val=`/opt/egurkha/jdk/bin/java -version 2>&1 | grep "12." | wc -l`
	if [ $val -eq 0 ]
	then
		rm -rf /opt/egurkha/jdk 2>/dev/null
	fi
fi

#if test "$startupManager" = "true" ; then
#	echo "Moving files to /etc/init.d ..."
# 	move the initAgent script file into the /etc/init.d directory #
#	cp ${userHome}/egurkha/bin/initManager /etc/init.d
#
# 	#Creating link  to the file to /etc/rc2.d directory #
#	ln -s /etc/init.d/initManager /etc/rc2.d/S90initManager
#fi

echo "**************************************************************"
echo "The eG manager has been successfully installed!"
echo " "
echo "Please login as $userName and run the script "
echo "/opt/egurkha/bin/setup_manager to configure the manager."
echo " "
echo "The licensing terms for eG products are mentioned in the file "
echo "/opt/egurkha/license_agreement."
echo "PLEASE READ THIS FILE BEFORE PROCEEDING FURTHER."
echo " "
echo "Note that the eG manager requires JDK 1.8 or higher."
echo "**************************************************************"

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


