@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 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 \*\*\*
echo.
echo.

:: ////////////////////////////////
:main
goto validateargs
:main0
goto validatedbversion
:main1
:: Determine whether the OLAP option is installed
goto validateolap
:main2
:: Check SH schema exists
echo Validating SH schema exists ...
goto shexists
:main3
echo Validating SH account is not locked ...
goto shaccountlocked
:main4
:: Check the OLAPSYS password
echo Validating OLAPSYS password ...
if %OLAP_INSTALLED%=="TRUE" goto validateolapsys
:main5
:: Check the location of DB files exists
echo Validating the location of DB files exists ...
goto validatedbfiles
:main6
:: Check the location of the script exists
echo Validating the location of the script exists ...
goto validatescriptlocation
:main7
:: Call the Disco specific DB script
cd %LOCATION_SCRIPT%\disco
call dbdisco.bat
:: Call the OLAP DB script
if %OLAP_INSTALLED%=="TRUE" goto callolapscript
if %OLAP_INSTALLED%=="FALSE" goto warningolapnotinstalled
:main8
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

:: ////////////////////////////////
:callolapscript
:: Call the OLAP DB script
cd %LOCATION_SCRIPT%\olap
call dbolap.bat %LOCATION_DB_DATAFILES% %LOCATION_SCRIPT%\olap
cd %LOCATION_SCRIPT%
goto main8

:: ////////////////////////////////
:validateolap
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @check_olap.sql
findstr "OLAP Analytic Workspace" %LOCATION_SCRIPT%\check_olap.log > nul
if ERRORLEVEL 1 goto errorolapaws
findstr "Oracle OLAP API" %LOCATION_SCRIPT%\check_olap.log > nul
if ERRORLEVEL 1 goto errorolapapi
findstr "OLAP Catalog" %LOCATION_SCRIPT%\check_olap.log > nul
if ERRORLEVEL 1 goto errorolapcatalog
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @role_exists.sql OLAP_USER
if ERRORLEVEL 1 goto errorolapuser
echo OLAP Components are present in the target database.
set OLAP_INSTALLED="TRUE"
goto main2

:: ////////////////////////////////
:validatedbfiles
if NOT EXIST %LOCATION_DB_DATAFILES% goto invaliddbfiles
goto main6

:: ////////////////////////////////
:validatescriptlocation
if NOT EXIST %LOCATION_SCRIPT%\install.bat goto invalidscriptlocation
goto main7

:: ////////////////////////////////
:shexists
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @user_exists.sql SH
if ERRORLEVEL 1 goto errorshdoesnotexist
goto main3

:: ////////////////////////////////
:shaccountlocked
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @account_locked.sql SH
if ERRORLEVEL 1 goto errorshaccountlocked
goto main4

:: ////////////////////////////////
:validateolapsys
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @logon.sql
if ERRORLEVEL 1 goto invalidolapsyspassword
goto main5

:: ////////////////////////////////
:validateargs
if "%1"=="" goto usage
if "%2"=="" goto usage
if "%ORACLE_HOME%" == "" goto envhelp
if "%ORACLE_SID%" == "" goto envhelp
goto setcommonvars
goto main0

:: ////////////////////////////////
:validatedbversion
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @check_db_version.sql
if ERRORLEVEL 8 goto errorWrongDBVersion
if ERRORLEVEL 7 goto errorWrongDBVersion
goto main1

:: ////////////////////////////////
:errorWrongDBVersion
echo \*\*\*
echo \*\*\* ERROR: Script not supported on a pre 9i database.
echo \*\*\*
goto end

:: ////////////////////////////////
:setcommonvars
set LOCATION_DB_DATAFILES=%1
set LOCATION_SCRIPT=%2
goto main1

:: ////////////////////////////////
:warningolapnotinstalled
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 \*\*\*
goto main8



:: ////////////////////////////////
:errorolapaws
echo \*\*\*
echo \*\*\* WARNING: OLAP Analytic Workspace component not installed.
echo \*\*\*
set OLAP_INSTALLED="FALSE"
goto main2

:: ////////////////////////////////
:errorolapuser
echo \*\*\*
echo \*\*\* WARNING: OLAP_USER role not present in the database.
echo \*\*\*
set OLAP_INSTALLED="FALSE"
goto main2

:: ////////////////////////////////
:errorolapapi
echo \*\*\*
echo \*\*\* WARNING: Oracle OLAP API component not installed.
echo \*\*\*
set OLAP_INSTALLED="FALSE"
goto main2

:: ////////////////////////////////
:errorolapcatalog
echo \*\*\*
echo \*\*\* WARNING: OLAP Catalog component not installed.
echo \*\*\*
set OLAP_INSTALLED="FALSE"
goto main2

:: ////////////////////////////////
:errorshdoesnotexist
echo \*\*\*
echo \*\*\* Error: SH schema does not exist.
echo \*\*\* Please install the Sales History demo schema.
echo \*\*\*
goto end

:: ////////////////////////////////
:errorshaccountlocked
echo \*\*\*
echo \*\*\* Error: The user SH is currently locked.  
echo \*\*\* Unlock the account and try again
echo \*\*\*
goto end

:: ////////////////////////////////
:invalidolapsyspassword
echo \*\*\*
echo \*\*\* Error: Cannot authenticate using supplied OLAPSYS credentials.
echo \*\*\* Please validate the supplied password, and try again.
echo \*\*\*
goto end

:: ////////////////////////////////
: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

:: ////////////////////////////////
: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

:: ////////////////////////////////
: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

:: ////////////////////////////////
: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
goto end


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