#!/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 \*\*\*

#Check for the number of arguments.
if [ $# -ne 2 ]
then	
	echo "Usage: $0 location_db_datafiles location_script"
	echo "   location_db_datafiles  Location of the DB datafiles."
	echo "   location_script        Fully qualified location of the script."
	echo ""
	echo "For example:"
	echo "$0 \$ORACLE_HOME/oradata/10DB /tmp/script_install"
	exit 1
fi

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

export LOCATION_DB_DATAFILES=$1
export LOCATION_SCRIPT=$2

OLAP_INSTALLED="TRUE"
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @check_olap.sql
grep "OLAP Analytic Workspace" check_olap.log > /dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* WARNING: OLAP Analytic Workspace component not installed.
	echo \*\*\*
	OLAP_INSTALLED="FALSE"
fi
grep "Oracle OLAP API" check_olap.log > /dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* WARNING: OLAP Analytic API not installed.
	echo \*\*\*
	OLAP_INSTALLED="FALSE"
fi
grep "OLAP Catalog" check_olap.log > /dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* WARNING: OLAP Analytic Catalog component not installed.
	echo \*\*\*
	OLAP_INSTALLED="FALSE"
fi

$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @role_exists.sql OLAP_USER
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* WARNING: OLAP_USER role not present in the database.
	echo \*\*\*
	OLAP_INSTALLED="FALSE"
fi

if [ $OLAP_INSTALLED == "TRUE" ]
then
	echo OLAP Components are present in the target database.
fi

#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

#Check the OLAPSYS password
echo "Validating OLAPSYS password ..."
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @logon.sql
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* Error: Cannot authenticate using supplied OLAPSYS credentials.
	echo \*\*\* Please validate the supplied password, and try again.
	echo \*\*\*
	exit 1
fi

#Check the location of DB files exists
echo "Validating the location of DB files exists ..."
if [ ! -a $LOCATION_DB_DATAFILES ]
then
	echo \*\*\*
	echo \*\*\* Error: Invalid location for DB files.
	echo \*\*\* Please ensure the location of the DB files is valid, and try again.
	echo \*\*\*
	exit 1
fi

#Check the location of the script exists
echo Validating the location of the script exists ...
if [ ! -a $LOCATION_SCRIPT/install.sh  ]
then
	echo \*\*\*
	echo \*\*\* Error: Invalid location for script. $LOCATION_SCRIPT/install.sh
	echo \*\*\* Cannot find the install.sh file within the specified script location.
	echo \*\*\* Please validate the supplied script directory is correct.
	echo \*\*\* The fully qualified path must be supplied, shortcuts such as . cannot be used.
	echo \*\*\* 
	exit 1
fi


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

#Call the OLAP DB script
if [ $OLAP_INSTALLED == "TRUE" ]
then
cd $LOCATION_SCRIPT/olap
dbolap.sh $LOCATION_DB_DATAFILES $LOCATION_SCRIPT/olap
cd $LOCATION_SCRIPT
fi

if [ $OLAP_INSTALLED == "FALSE" ]
then
echo \*\*\*
echo \*\*\* WARNING: OLAP not setup, some or all of the following
echo \*\*\* OLAP components are missing from the target database
echo \*\*\* OLAP Anaytic Workspace
echo \*\*\* Oracle OLAP API
echo \*\*\* Oracle Catalog.
echo \*\*\*
fi


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
