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

if test -z "$host" ; then
	host=`hostname`
fi
FLAG="y"
echo "Next, enter the port number associated with $SID: "
while [ "$FLAG" = "y" ]
do
        read portNo
        if [ -n "$portNo" ]
        then
                FLAG="n"
        else
                echo" Please Enter valid PortNo  : "
        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: "
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read dbUser
	if [ -n "$dbUser" ]
	then
		FLAG="n"
	else
		echo "Please enter valid userName : "
	fi
done
 
echo "Enter the desired password for $dbUser: "
stty -echo 
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read dbPass
        if [ -n "$dbPass" ]
        then
                FLAG="n"
        else
                echo "Password cannot be NULL : "
        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: "
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read tspace1
        if [ -n "$tspace1" ]
        then
                FLAG="n"
        else
                echo "Default tablespace cannot be NULL : "
        fi
done

echo "Enter the temporary tablespace for $dbUser: "
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read tspace2
        if [ -n "$tspace2" ]
        then
                FLAG="n"
        else
                echo "Temporary tablespace cannot be NULL :"
        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: "
FLAG="y"
while [ "$FLAG" = "y" ]
do
	read  dbaUser
        if [ -n "$dbaUser" ]
        then
                FLAG="n"
        else
                echo "Please Enter valid dba user name : "
        fi
done

echo "Enter the dba password for $dbauser@$SID : " 
stty -echo 
FLAG="y"
while [ "$FLAG" = "y" ]
do
        read dbaPass
        if [ -n "$dbaPass" ]
        then
                FLAG="n"
        else
                echo "Password cannot be NULL : "
        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`
if [ $isJRE17 -eq 0 ]
then
	isJRE17=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "1.8" | wc -l`
fi
isJRE10=`/opt/egurkha/jre/bin/java -version 2>&1 | grep "10." | wc -l`

export isJRE16
export isJRE17
export isJRE10

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
if [ "$isJRE10" -ge "1" ]
then
        javaCmd="java -XX:ErrorFile=/dev/null -XX:HeapDumpPath=/dev/null -XX:-CreateCoredumpOnCrash "
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 
rm -f $EG_HOME/bin/create_user.sql
echo "Completed database configuration for $SID."
exit
