#!/bin/sh
. ./fileproperties.prop

#### Check Oracle home #########################################
if [ ! -d "$ORACLE_HOME" ]
then
    echo "####"
    echo "The ORACLE_HOME setting ($ORACLE_HOME) points to a non-existing"
    echo "directory. Please check the configuration script."
    exit 1
fi

process_sql_and_log() {
   if [ $# -ne 2 ]
   then
      echo "Usage: $0 sqlfile logfile"
      return 1
   fi
   sqlplus ABSDATAWAREHOUSE_V10/ABSDATAWAREHOUSE_V10@ABS1 <<EOF
spool ${2} append
@${1}
set echo off
set heading off
spool off
exit;
EOF
}

touch $LOGFILENAME
echo "####"
echo 'start task' >> $LOGFILENAME
echo `date +%Y%b%d_%H%M%S` >> $LOGFILENAME
cd "${ABS_ROOTDIR}"
echo "==========Step 1.1 Create oracle directory CONVDIMKEY=========="
echo  "${ABS_ROOTDIR}"
sqlplus / as sysdba <<EOF
spool ${2} append
create or replace directory CONVDIMKEY as '${ABS_ROOTDIR}';
set echo off
set heading off
spool off
exit;
EOF

#### Creating the V10 schema ###################################
echo "==========Step 1.2 Export and import schema=========="
expdp ABSDATAWAREHOUSE/ABSDATAWAREHOUSE@ABS1 PARFILE=1.1.partial_absdatawarehouse_v10_subset4.par directory=CONVDIMKEY dumpfile=ABSDATAWAREHOUSE_V10.dmp logfile=ABSDATAWAREHOUSE_V10_exp.log
impdp ABSDATAWAREHOUSE/ABSDATAWAREHOUSE@ABS1 remap_schema=ABSDATAWAREHOUSE:ABSDATAWAREHOUSE_V10 directory=CONVDIMKEY dumpfile=ABSDATAWAREHOUSE_V10.dmp logfile=ABSDATAWAREHOUSE_V10_imp.log
#### SYS scripts ###############################################
echo "Step 2 SYS scripts"
sqlplus / as sysdba <<EOF
spool ${2} append
@2.sys_script_for_v10.sql
set echo off
set heading off
spool off
exit;
EOF
#### V10 scripts ###############################################
echo "==========Step 3 create databaselink original locations=========="
process_sql_and_log 3.create_database_link_org_locations.sql $LOGFILENAME &&
echo "==========Step 4 upgrade datawarehouse 1.10.00=========="
process_sql_and_log 4.upgrade_absdatawarehouse_1.10.00.sql $LOGFILENAME &&
echo "==========Step 4c install V10 database objects=========="
process_sql_and_log '${ABS_CODE}'/load_date_and_time_dims.pck $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/load_warehouse_v10.pck $LOGFILENAME &&
#process_sql_and_log '${ABS_CODE}'/load_globe4_date_and_time_dims.pck $LOGFILENAME &&
#process_sql_and_log '${ABS_CODE}'/load_globe4_facts_v10.pck $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/pack_absencrypt_wh.pck $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/update_dimkey_wh.pck $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/chk_warehouse_v10.prc $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/enable_constraintsandindexes.prc $LOGFILENAME &&
process_sql_and_log '${ABS_CODE}'/disable_constraintsandindexes.prc $LOGFILENAME &&
echo "==========Step 5 drop indexes / remove constraints=========="
process_sql_and_log 5.before_update_dim_key.sql $LOGFILENAME &&
echo "==========Step 6 create check views=========="
process_sql_and_log 6.create_check_views.sql $LOGFILENAME &&
echo "==========Recompile invalid objects=========="
process_sql_and_log recompile.sql $LOGFILENAME &&
echo "==========Step 7.1 Update facts=========="
process_sql_and_log 7.1.update_facts.sql $LOGFILENAME &&
#echo "==========Step 7.2 Update Globe4 facts=========="
#process_sql_and_log 7.2.update_globe4_facts.sql $LOGFILENAME &&
echo "==========Step 7.3 Update Dims=========="
echo "Press any key to continue with the dims after checking the fact results.."
read -n 1 -s
process_sql_and_log 7.3.update_dim.sql $LOGFILENAME &&
echo "==========Step 8 Recreate indexes=========="
echo "Press any key to continue with recreate indexes after checking the dim results.."
read -n 1 -s
process_sql_and_log 8.after_update_dim_key.sql $LOGFILENAME &&
echo "==========Step 9 Update ETL timestamp=========="
process_sql_and_log 9.update_etl_timestamp_dwh.sql $LOGFILENAME &&
echo "==========Step 10 Create scheduler jobs=========="
process_sql_and_log 10.create_scheduler_jobs.sql $LOGFILENAME &&
echo "==========Step 10 Unlock tables and gather stats=========="
process_sql_and_log 11.unlock_tables_and_gather_stats.sql $LOGFILENAME &&
echo "==========Final step Recompile Invalid objects=========="
process_sql_and_log recompile.sql $LOGFILENAME
echo 'end task' >> $LOGFILENAME
echo `date +%Y%b%d_%H%M%S` >> $LOGFILENAME
echo . >> $LOGFILENAME