#!/bin/sh

EG_CLI_PATH="/opt/egurkha/egcli"
export EG_CLI_PATH

echo "Please provide the Manager ID of an account that you wish to modify :"
read oldMgrID

if [ ! -f /opt/egurkha/egcli/account/$oldMgrID.account ]
then
	echo "Manager ID does not exist"
	exit
fi
echo

while read tmpline
do	
	key="$(echo $tmpline | cut -d# -f1)"
	value="$(echo $tmpline | cut -d# -f2)"
	if [ "$key" = "mgrip" ]
	then
		oldMgrIP=$value
		export oldMgrIP
	fi
	if [ "$key" = "mgrport" ]
	then
		oldPort=$value
		export oldPort
	fi
	if [ "$key" = "user" ]
	then
		oldUser=$value
		export oldUser
	fi
	if [ "$key" = "pwd" ]
	then
		oldPwd=$value
		export oldPwd
	fi
	if [ "$key" = "ssl" ]
	then
		oldSsl=$value
		export oldSsl
	fi
done < /opt/egurkha/egcli/account/$oldMgrID.account
echo

clear
echo "Account details of the Manager ID : $oldMgrID"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

echo "eG Manager IP/Host name : $oldMgrIP"
echo "Please provide the new IP/Host name of the eG Manager :"
read mgrIP
if [ "$mgrIP" = "" ]
then
	mgrIP=$oldMgrIP
	export mgrIP
fi

echo "eG Manager port : $oldPort" 
echo "Please provide the new port of the eG Manager :"
read mgrPort
if [ "$mgrPort" = "" ]
then
	mgrPort=$oldPort
	export mgrPort
fi

echo "SSL enabled : $oldSsl"
echo "Is the eG Manager SSL enabled? (yes/no) :"
read ssl
if [ "$ssl" = "" ]
then
	ssl=$oldSsl
	export ssl
fi

echo "eG Manager admin user name : $oldUser"
echo "Please provide the new admin username for the eG Manager :"
read user
if [ "$user" = "" ]
then
	user=$oldUser
	export user
fi

echo "Please provide the new admin password for the eG Manager :"
stty -echo
read tmpencrypwd
stty echo

if [ "$tmpencrypwd" = "" ]
then
	encrypwd=$oldPwd
	export encrypwd
fi
if [ "$tmpencrypwd" != "" ]
then
	encrypwd=`/opt/egurkha/jre/bin/java -DEGCLI_INSTALL_DIR=/opt/egurkha/egcli -cp $EG_CLI_PATH/lib/eg_cli.jar:$CLASSPATH com.eg.cli.CLIConfigInfo $tmpencrypwd`
	export encrypwd
fi

echo "Manager ID : $oldMgrID"
echo "Please provide a new Manager ID for this profile :"
read mgrID
if [ "$mgrID" = "" ]
then
	mgrID=$oldMgrID
	export mgrID
fi


result="false"
export result

action="modify"
export action

result=`/opt/egurkha/jre/bin/java -DEGCLI_INSTALL_DIR=/opt/egurkha/egcli -cp $EG_CLI_PATH/lib/eg_cli.jar:$CLASSPATH com.eg.cli.AccountValidator $action $mgrIP $mgrPort $user $encrypwd $ssl $mgrID`
export result

if [ "$result" != "true" ]
then
	echo "$result"
	exit
fi

echo "mgrip#$mgrIP" > $EG_CLI_PATH/account/$mgrID.account
echo "mgrport#$mgrPort" >> $EG_CLI_PATH/account/$mgrID.account
echo "user#$user" >> $EG_CLI_PATH/account/$mgrID.account
echo "pwd#$encrypwd" >> $EG_CLI_PATH/account/$mgrID.account
echo "ssl#$ssl" >> $EG_CLI_PATH/account/$mgrID.account

if [ "$oldMgrID" != "$mgrID" ]
then
	rm -f $EG_CLI_PATH/account/$oldMgrID.account
fi

if [ "$result" = "true" ]
then
	echo "************************************************"
	echo "User account has been modified successfully!"
	echo "************************************************"
fi


