#!/bin/sh
clear
FLAG="y"
echo "First, enter the name of the Oracle SID that eG should monitor: \c"
while [ "$FLAG" = "y" ]
do
	read SID
	if [ -n "$SID" ]
	then
		FLAG="n"
	else
		echo" Please Enter valid SID  : \c"
	fi
done
echo "Next, enter the hostname (or IP address) on which $SID is executing [`hostname`]: \c"
read host

if test -z "$host" ; then
	host=`hostname`
fi
FLAG="y"
echo "Next, enter the port number associated with $SID: \c"
while [ "$FLAG" = "y" ]
do
        read portNo
        if [ -n "$portNo" ]
        then
                FLAG="n"
        else
                echo" Please Enter valid PortNo  : \c"
        fi
done

echo "eG uses a special user account to monitor each Oracle instance (SID)."
echo "!!Note: Please remember the user name and password you give as you are needed "
echo "        provide this in the web admin console as parameter for the Oracle tests!"
echo "        "
echo "Enter the name of the eG user account for $SID: \c"
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read dbUser
	if [ -n "$dbUser" ]
	then
		FLAG="n"
	else
		echo "Please enter valid userName : \c"
	fi
done
 
echo "Enter the desired password for $dbUser: \c"
stty -echo 
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read dbPass
        if [ -n "$dbPass" ]
        then
                FLAG="n"
        else
                echo "Password cannot be NULL : \c"
        fi
done

stty echo
ORACLE_SID=$SID
export ORACLE_SID
echo ""
echo "Each Oracle user must be associated with a default and a temporary tablespace."
echo "Enter the default tablespace name for the user $dbUser: \c"
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read tspace1
        if [ -n "$tspace1" ]
        then
                FLAG="n"
        else
                echo "Default tablespace cannot be NULL : \c"
        fi
done

echo "Enter the temporary tablespace for $dbUser: \c"
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read tspace2
        if [ -n "$tspace2" ]
        then
                FLAG="n"
        else
                echo "Temporary tablespace cannot be NULL :\c"
        fi
done


echo ""
echo "A user account can only be created by an Oracle DBA user."
echo "Please enter the name of a DBA user for the SID $SID: \c"
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read  dbaUser
        if [ -n "$dbaUser" ]
        then
                FLAG="n"
        else
                echo "Please Enter valid dba user name : \c"
        fi
done

echo "Enter the dba password for $dbauser@$SID : \c" 
stty -echo 
FLAG="y"
while [ "$FLAG" = "y" ]
do
        read dbaPass
        if [ -n "$dbaPass" ]
        then
                FLAG="n"
        else
                echo "Password cannot be NULL : \c"
        fi
done

stty echo
echo "\n"

EG_HOME=/opt/egurkha
export EG_HOME
CLASSPATH=.:$EG_HOME/lib/classes111.zip:$EG_HOME/lib/eg_agent.jar:$CLASSPATH
export CLASSPATH
isJRE16=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.6" | wc -l`
isJRE17=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.7" | wc -l`
export isJRE16
export isJRE17

javaCmd="java "
if [ "$isJRE16" -ge "1" ]
then
        javaCmd="java -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null "
fi
if [ "$isJRE17" -ge "1" ]
then
        javaCmd="java -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateMinidumpOnCrash "
fi
echo ""
echo "create user $dbUser identified by $dbPass " > $EG_HOME/bin/create_user.sql
echo "default tablespace $tspace1 " >> $EG_HOME/bin/create_user.sql
echo "temporary tablespace $tspace2;" >>  $EG_HOME/bin/create_user.sql
#echo "quota 1m on $tspace1; " >> $EG_HOME/bin/create_user.sql 
echo "Grant connect, resource to $dbUser; " >> $EG_HOME/bin/create_user.sql
echo "Grant select_catalog_role to $dbUser; " >> $EG_HOME/bin/create_user.sql

#chmod 755 $EG_HOME/bin/create_user.sql
#sqlplus $USERNAME/$pass <<EOF
#@$EG_HOME/bin/create_user.sql
#EOF
$javaCmd EgUserCreation -host $host -port $portNo -sid $SID -dbauser $dbaUser -dbapassword $dbaPass -filepath $EG_HOME/bin/create_user.sql -username $dbUser
rm -f $EG_HOME/bin/create_user.sql
echo "Completed database configuration for $SID."
exit
