#!/bin/ksh

if [ $# -ne 2 ]
then
	echo Please specify the following two parameters when running the install:
	echo " location of your Oracle9i cs_olap datafiles"
	echo " location of the shaw.eif file (source of the data for the analytic workspace)"
	echo ""
	echo For example:
	echo $0 /oracle/ora10g/oradata/orcl /tmp/scripts/samples/common_schema/olap
	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

PAR1=$1
PAR2=$2

echo \*\*\*
echo Removing previous log files
echo \*\*\*

rm -f $PAR2\USER.LOG
rm -f $PAR2\AW.LOG
rm -f $PAR2\ENABLE.LOG
rm -f $PAR2\CWM.LOG
rm -f $PAR2\PRIVS.LOG
rm -f $PAR2\SH_CHECK.LOG
rm -f $PAR2\SH_DROP.LOG
rm -f $PAR2\INSTALL.LOG

echo \*\*\*
echo \*\*\*
echo \*\*\* This script makes the following assumptions:
echo \*\*\* 1. ORACLE_HOME and ORACLE_SID environment variables are set
echo \*\*\* 2. Oracle OLAP has been installed in the target database instance
echo \*\*\* 3. SQL*Plus is in your path
echo \*\*\* 4. OLAPSYS account is unlocked
echo \*\*\* 5. The user running this script has the OLAP_DBA role assigned
echo \*\*\*
echo \*\*\*
echo \*\*\* Next if user cs_olap is already installed and if 
echo \*\*\* it is currently connected

echo Connecting as SYS user
$ORACLE_HOME/bin/sqlplus "/ as sysdba" @0_check_user.sql $1 $2

#Is the user connected?
grep "CS_OLAP" CHECK_USER.LOG > /dev/null
if [ $? -eq 0 ]
then
	echo \*\*\*
	echo \*\*\* The user CS_OLAP is currently connected and cannot be dropped
	echo \*\*\* Disconnect all CS_OLAP sessions and try again
	echo \*\*\*
	exit 1
fi

#Is the account locked
grep "LOCKED" CHECK_USER.LOG > /dev/null
if [ $? -eq 0 ]
then
	echo \*\*\*
	echo \*\*\* The user OLAPSYS is currently locked.  
	echo \*\*\* Unlock the account and try again
	echo \*\*\*
	exit 1
fi

#Does the user have OLAP_DBA privileges
grep "OLAP_DBA" CHECK_USER.LOG > /dev/null
if [ $? -ne 0 ]
then
	echo \*\*\*
	echo \*\*\* You do not have OLAP_DBA privileges.  
	echo \*\*\* Assign the OLAP_DBA role to your account
	echo \*\*\*
	exit 1
fi

#Is CS_OLAP Installed
grep "CS_OLAP" CS_OLAP.LOG > /dev/null
if [ $? -eq 0 ]
then
	echo "User CS_OLAP insalled"
	echo Removing existing CS_OLAP installation
	$ORACLE_HOME/bin/sqlplus "/ as sysdba" @1_drop_all.sql $1 $2
fi

#Create the CS_OLAP user
echo "Create the CS_OLAP user"
$ORACLE_HOME/bin/sqlplus "/ as sysdba" @2_create_user.sql $1 $2

echo Connecting as CS_OLAP and importing eif file from directory $PAR2
$ORACLE_HOME/bin/sqlplus "cs_olap/cs_olap" @3_create_aw.sql

#Check the DB version
echo Checking DB version ...
$ORACLE_HOME/bin/sqlplus -S "/ as sysdba" @check_db_version.sql

dbver=$?

if [ $dbver -eq 9 ]
then
	echo Enabling the SHAW analytic workspace for OLAP API and BI Beans
	$ORACLE_HOME/bin/sqlplus "cs_olap/cs_olap" @4_enable_shaw.sql

	echo Organizing measures and dimensions
	$ORACLE_HOME/bin/sqlplus "cs_olap/cs_olap" @5_create_cwm_folder.sql	
fi


echo Modifying user privileges on CS_OLAP
$ORACLE_HOME/bin/sqlplus "cs_olap/cs_olap" @6_alter_privileges.sql

echo Checking if SH schema metadata installed
echo please enter OLAPSYS password
$ORACLE_HOME/bin/sqlplus "/ as sysdba" @7_sh_check_cwm.sql

cp /dev/null SH_DROP.LOG

grep "SH" SH_CHECK.LOG > /dev/null
if [ $? -eq 0 ]
then
	echo Removing demo user SH metadata from OLAP catalog
	echo please enter OLAPSYS password
	$ORACLE_HOME/bin/sqlplus "/ as sysdba" @8_sh_drop_cwm.sql
fi

echo Resizing tablespaces
$ORACLE_HOME/bin/sqlplus "/ as sysdba" @9_reset_tablespaces.sql $1

echo
echo \*\*\*
echo \*\*\* Checking for errors in the log file
echo \*\*\*
cat USER.LOG AW.LOG ENABLE.LOG CWM.LOG PRIVS.LOG SH_DROP.LOG INSTALL.LOG > tmpinstall.log

grep -i "error" tmpinstall.log > /dev/null
if [ $? -eq 0 ]
then
	echo \*\*\*
	echo \*\*\* Errors in the log file, please review file tmpinstall.log
	echo \*\*\* and resolve the errors before re-running this script 
	echo \*\*\*
	exit 1
fi

echo
echo \*\*\*
echo \*\*\* Schema CS_OLAP password CS_OLAP successfully installed.  Have fun!
echo \*\*\*
