@echo off

rem arguments: location_db_datafiles and location_script
echo \*\*\*
echo \*\*\*
echo \*\*\* This script makes the following assumptions:
echo \*\*\* 1. The script is being executed on the Database server.
echo \*\*\* 2. The ORACLE_HOME and ORACLE_SID environment variables are set.
echo \*\*\* 3. SQL*Plus and IMP are in your path.
echo \*\*\* 4. 
echo \*\*\* 5. The SH sample schema is installed and the account is unlocked.
echo \*\*\*
echo \*\*\*
echo.
echo.

:: ////////////////////////////////
:main

:main0
goto validatedbversion

:main1
:: Check SH schema exists
echo Validating SH schema exists ...
goto shexists

:main3
echo Validating SH account is not locked ...
goto shaccountlocked

:main7
:: Call the Disco specific DB script
cd disco
call dbdisco.bat
:main8

cd ..
echo Checking for errors in the log file ...
Copy tmpuserexists.sql+tmpaccountlocked.sql install.log > nul
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

:: ////////////////////////////////
:: V A L I D A T E   D B   F I L E S   L O C A T I O N
:validatedbfiles
echo %LOCATION_DB_DATAFILES% 
if EXIST %LOCATION_DB_DATAFILES% goto main6
goto invaliddbfiles

:: ////////////////////////////////
:: V A L I D A T E   S C R I P T   L O C A T I O N
:validatescriptlocation
if NOT EXIST %LOCATION_SCRIPT%\install_rel.bat goto invalidscriptlocation
goto main7

:: ////////////////////////////////
:: D O E S   S H   E X I S T
:shexists
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @user_exists.sql SH
if ERRORLEVEL 1 goto errorshdoesnotexist
goto main3

:: ////////////////////////////////
:: C H E C K   I F   S H   A C C O U N T   U N L O C K E D
:shaccountlocked
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @account_locked.sql SH
if ERRORLEVEL 1 goto errorshaccountlocked
goto main7


:: ////////////////////////////////
:: V A L I D A T E   A R G U M E N T S
:validateargs
if "%1"=="" goto usage
if "%2"=="" goto usage
if "%ORACLE_HOME%" == "" goto envhelp
if "%ORACLE_SID%" == "" goto envhelp
goto setcommonvars
goto main0

:: ////////////////////////////////
:: V A L I D A T E   D A T A B A S E   V E R S I O N
:validatedbversion
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @check_db_version.sql
echo %ERRORLEVEL%
if ERRORLEVEL 10 goto main1
if ERRORLEVEL 9 goto main1

if ERRORLEVEL 8 goto errorWrongDBVersion
if ERRORLEVEL 7 goto errorWrongDBVersion
goto main1

:: ////////////////////////////////
:: W R O N G   D A T A B A S E   V E R S I O N
:errorWrongDBVersion
echo \*\*\*
echo \*\*\* ERROR: Script not supported on a database older than Oracle9i.
echo \*\*\*
goto end

:: ////////////////////////////////
:: S E T   V A R I A B L E S
:setcommonvars
set LOCATION_DB_DATAFILES=%1
set LOCATION_SCRIPT=%2
goto main0

:: ////////////////////////////////
:: E R R O R   S H   D O E S   N O T   E X I S T
:errorshdoesnotexist
echo \*\*\*
echo \*\*\* Error: SH schema does not exist.
echo \*\*\* Please install the Sales History sample schema and then retry this installation.
echo \*\*\*
goto end

:: ////////////////////////////////
:: E R R O R   S H   A C C O U N T   L O C K E D
:errorshaccountlocked
echo \*\*\*
echo \*\*\* Error: The user SH is currently locked.  
echo \*\*\* Unlock the account and try again
echo \*\*\*
goto end

:: ////////////////////////////////
:: I N V A L I D   L O C A T I O N   F O R   D B   F I L E S
:invaliddbfiles
echo \*\*\*
echo \*\*\* Error: Invalid location for DB files.
echo \*\*\* Please ensure the location of the DB files is valid, and try again.
echo \*\*\*
goto end

:: ////////////////////////////////
:: I N V A L I D   L O C A T I O N   S P E C I F I E D   F O R   T H E   S C R I P T
:invalidscriptlocation
echo \*\*\*
echo \*\*\* Error: Invalid location for script.
echo \*\*\* Cannot find the install.bat 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 \*\*\* 
goto end

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

:: ////////////////////////////////
:: E N V   V A R I A B L E S   N O T   S E T 
:envhelp
echo.
echo.
echo - Make sure your ORACLE_SID and ORACLE_HOME are correctly set
echo -
echo - for example SET ORACLE_SID=BEANS
echo -             SET ORACLE_HOME=D:\ORACLE\ORA92
echo.
goto end

:: ////////////////////////////////
:: E C H O   C O R R E C T   U S A G E
:usage
echo Usage: install 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 install c:\orahome1\oradata\myservice c:\samples\common_schema
echo
echo Be sure NOT to insert a slash at the end of the location.
goto end


:: ////////////////////////////////
:end
