@echo off
if "%1" == "" goto helptopic
if "%2" == "" goto helptopic
if "%ORACLE_HOME%" == "" goto envhelp
if "%ORACLE_SID%" == "" goto envhelp

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

erase %2\USER.LOG
erase %2\AW.LOG
erase %2\ENABLE.LOG
erase %2\CWM.LOG
erase %2\PRIVS.LOG
erase %2\SH_CHECK.LOG
erase %2\SH_DROP.LOG
erase %2\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.
echo \*\*\* Next if user cs_olap is already installed and if 
echo \*\*\* it is currently connected
echo.

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

rem Is the user connected?
findstr "CS_OLAP" CHECK_USER.LOG > nul
if not errorlevel 1 goto USER_ERROR

rem Is the account locked
findstr "LOCKED" CHECK_USER.LOG > nul
if not errorlevel 1 goto OLAPSYS_ERROR

rem Does the user have OLAP_DBA privileges
findstr "OLAP_DBA" CHECK_USER.LOG > nul
if errorlevel 1 goto OLAP_DBA_ERROR

rem Is CS_OLAP Installed? If yes, then first remove it.
findstr "CS_OLAP" CS_OLAP.LOG > nul
if NOT ERRORLEVEL 1 goto cs_installed
goto no_cs

:cs_installed
echo Removing existing CS_OLAP installation
echo Connecting as SYS user
%ORACLE_HOME%\BIN\sqlplus "/ as sysdba" @1_drop_all.sql %1 %2


:no_cs
rem at this stage the cs_olap user would have been removed if it existed.
echo Connecting as SYS user
%ORACLE_HOME%\BIN\sqlplus "/ as sysdba" @2_create_user.sql %1 %2

echo Connecting as CS_OLAP and importing eif file from directory %2
%ORACLE_HOME%\BIN\sqlplus "cs_olap/cs_olap" @3_create_aw.sql

rem Check the DB Version
%ORACLE_HOME%\BIN\sqlplus -L -S "/ as sysdba" @check_db_version.sql
if ERRORLEVEL 10 goto no_cs_10g

rem Steps for 9i only
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

rem for 10g
:no_cs_10g
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
%ORACLE_HOME%\BIN\sqlplus "/ as sysdba" @7_sh_check_cwm.sql

copy nul SH_DROP.LOG

findstr "SH" SH_CHECK.LOG > nul
if NOT ERRORLEVEL 1 goto SH_INSTALLED
goto no_sh

:sh_installed
echo Removing demo user SH metadata from OLAP catalog
%ORACLE_HOME%\BIN\sqlplus "/ as sysdba" @8_sh_drop_cwm.sql

:no_sh
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 \*\*\*
Copy USER.LOG+AW.LOG+ENABLE.LOG+CWM.LOG+PRIVS.LOG+SH_DROP.LOG 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

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


:helptopic
echo.
echo.
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 install d:\Orahome1\oradata\my9iservice d:\beans
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


:user_error
echo
echo \*\*\*
echo \*\*\* The user CS_OLAP is currently connected and cannot be dropped
echo \*\*\* Disconnect all CS_OLAP sessions and try again
echo \*\*\*
goto end

:OLAPSYS_ERROR
echo
echo \*\*\*
echo \*\*\* The user OLAPSYS is currently locked.  
echo \*\*\* Unlock the account and try again
echo \*\*\*
goto end

:OLAP_DBA_ERROR
echo
echo \*\*\*
echo \*\*\* You do not have OLAP_DBA privileges.  
echo \*\*\* Assign the OLAP_DBA role to your account
echo \*\*\*
goto end

:envhelp
echo.
echo.
echo Please specify the following two parameters when running the install:
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

:end
