@echo off

:main

goto deletelogfiles

:main1
:: Check the DB version
echo Checking DB version ...
goto checkdbversion

:main2
:: Perform9steps imports the entire dmp file into the 9i datbase.
if ERRORLEVEL 10 goto perform10steps
goto perform9steps

:main3
:: this is called both in the case of 9i and 10g.
goto createprofitstab

:main4
echo Checking for errors in the log file ...
copy check.log+fix4.log+fix3.log+fix2.log+fix1.log+profits.log+dropsh.log+tmpcheckdbversion.sql INSTALL.LOG > nul

:: look for 'error' in different cases
findstr "error" INSTALL.LOG > nul
if NOT ERRORLEVEL 1 goto LOG_ERROR
findstr "ERROR" INSTALL.LOG > nul
if NOT ERRORLEVEL 1 goto LOG_ERROR
findstr "Error" INSTALL.LOG > nul
if NOT ERRORLEVEL 1 goto LOG_ERROR

goto end
:endmain

:: ////////////////////////////////
:: C R E A T E   P R O F I T S   T A B L E 
:createprofitstab
echo Creating profits_tab table
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @create_profits_tab.sql
goto main4

:: ////////////////////////////////
:: P E R F O R M   1 0 g   S T E P S
:perform10steps
echo Performing 10g and above steps ...
echo Upgrading SH schema tables ...
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @FinalCustomerFix1.sql
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @FinalCustomerFix2.sql
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @FinalCustomerFix3.sql
echo Updating COSTS table; this operation may take a long time.
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @FinalCustomerFix4.sql
goto main3

:: ////////////////////////////////
:: P E R F O R M   9 i   S T E P S
:perform9steps
echo Performing 9i steps ...
echo Upgrading SH schema tables ...
echo Dropping SH tables, MVs first ...
%ORACLE_HOME%\BIN\sqlplus -L -S SH/SH @drop_sh_db_objs.sql
echo Importing SH schema data; this operation may take a long time ...
%ORACLE_HOME%\BIN\imp '/ as sysdba' touser=SH full=y ignore=y file=new10g.dmp
goto main3

:: ////////////////////////////////
:log_error
echo
echo \*\*\*
echo \*\*\* Errors in the log file, please review file INSTALL.LOG
echo \*\*\* and resolve the errors and re-run this script if required.
echo \*\*\*
goto end

:: ////////////////////////////////
:deletelogfiles
echo
echo \*\*\*
echo \*\*\* Deleting previous log files
echo \*\*\* You may get error messages if these log files do not exist. 
echo \*\*\* You can ignore these errors.
echo
erase check.log 
erase fix4.log
erase fix3.log 
erase fix2.log 
erase fix1.log 
erase profits.log 
erase dropsh.log 
goto main1

:: ////////////////////////////////
:checkdbversion
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @check_db_version.sql
goto main2

:end
