#!/bin/sh

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

clear

echo "Add New Account"
echo "~~~~~~~~~~~~~~~"

echo "Please provide the eG Manager IP / Host name :"
read mgrIP

echo "Please provide the port number for the eG Manager [7077]:"
read mgrPort

echo "Is the eG Manager SSL enabled? (yes/no) [no]:"
read ssl

echo "Please provide an admin user name for the eG Manager :"
read user

echo "Please provide the password for the admin user :"

stty -echo
read password
stty echo

if [ "$ssl" = "" ]
then
	ssl="no"
	export ssl
fi

if [ "$mgrPort" = "" ]
then
	mgrPort="7077"
	export mgrPort
fi

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 $password`
export encrypwd


echo "The details you just provided will be saved in a profile on this system."
echo "Please provide a Manager ID for this profile :"
read mgrID

result="false"
export result

action="add"
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 [ "$result" = "true" ]
then
	echo "************************************************"
	echo "User account has been registered successfully!"
	echo "************************************************"
fi
