#!/bin/ksh 

# deletelogfiles 
echo \*\*\* deleting previous logs 
echo "deleting previous logs....."
rm -f fix4.log
rm -f fix3.log
rm -f fix2.log
rm -f fix1.log
rm -f profits.log
rm -f dropsh.log

#Check the DB version

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

dbver=$?

if [ $dbver -eq 10 ]
then
	echo Performing 10g and above steps ...
	echo Upgrading SH schema tables ...
	$ORACLE_HOME/bin/sqlplus -S sh/sh @FinalCustomerFix1.sql > install.log
	$ORACLE_HOME/bin/sqlplus -S sh/sh @FinalCustomerFix2.sql >> install.log
	$ORACLE_HOME/bin/sqlplus -S sh/sh @FinalCustomerFix3.sql >> install.log
	$ORACLE_HOME/bin/sqlplus -S sh/sh @FinalCustomerFix4.sql >> install.log
elif [ $dbver -eq 9 ]
then
	echo Performing 9i steps ...
	echo Dropping SH tables, MVs ...
	$ORACLE_HOME/bin/sqlplus -S sh/sh @drop_sh_db_objs.sql
	echo Importing SH schema data ...
	$ORACLE_HOME/bin/imp imp \"/ as sysdba\" touser=SH full=y ignore=y file=new10g.dmp
else
	echo "Wrong database version $dbver returned"
	exit 1
fi

#Create profits table
echo Creating profits_tab
$ORACLE_HOME/bin/sqlplus -S sh/sh @create_profits_tab.sql

echo Checking for errors in the log file ...
cat tmpcheckdbversion.sql 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 install.log
	echo \*\*\* and resolve the errors before re-running this script 
	echo \*\*\*
fi

