#!/bin/ksh

# Print the general help message.
echo \*\*\*
echo \*\*\*
echo \*\*\* This script makes the following assumptions:
echo \*\*\* 1. The script is being executed on the DB server.
echo \*\*\* 2. ORACLE_HOME and ORACLE_SID environment variables are set.
echo \*\*\* 3. Oracle OLAP has been installed in the target database instance.
echo \*\*\* 4. SQL*Plus and IMP are in your path.
echo \*\*\* 5. OLAPSYS account is unlocked.
echo \*\*\* 6. The SH schema is installed and the account is unlocked.
echo \*\*\*
echo \*\*\*

checkEnvVar()
{
        parameter=$1
        txt=`env | grep "^$parameter="`

        if [ "$txt" == "" ]
        then
                echo "env variable $parameter not set"
                exit 1
        fi
}

#Check that the Environment 
#Make sure your ORACLE_SID and ORACLE_HOME are correctly set
checkEnvVar ORACLE_HOME
checkEnvVar ORACLE_SID

#Check SH schema exists
echo "Validating SH schema exists ..."
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @user_exists.sql SH 2>/dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* Error: SH schema does not exist.
	echo \*\*\* Please install the Sales History demo schema.
	echo \*\*\*
	exit 1
fi	

#Validating SH account is not locked ...
echo "Validating SH account is not locked ..."
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @account_locked.sql SH
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* Error: The user SH is currently locked.  
	echo \*\*\* Unlock the account and try again
	echo \*\*\*
	exit 1
fi

#Call the Disco specific DB script
cd disco
dbdisco.sh

echo "Checking for errors in the log file ..."
cd $LOCATION_SCRIPT
cat tmpuserexists.sql tmpaccountlocked.sql > install.log
grep -i "error"  install.log > /dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* Errors in the log file, please review file INSTALL.LOG
	echo \*\*\* and resolve the errors before re-running this script 
	echo \*\*\*
	exit 1
fi
