#!/bin/bash

# Copyright (c) 2000, 2005 IBM Corp. - All Rights Reserved.
# IBM makes no representations or warranties about the suitability of
# this program, either express or implied, including but not limited to
# the implied warranties of merchantability, fitness for a particular
# purpose, or non-infringement. 

# The purpose of this script is to migrate the database schema used by WPC 5.0.0.4 to the WPC 5.2.1.1 
# Please consult WPC support before attempting to use this script

# 
# # Copyright (c) 2000, 2004 IBM Corp. - All Rights Reserved.
# # IBM makes no representations or warranties about the suitability of
# # this program, either express or implied, including but not limited to
# # the implied warranties of merchantability, fitness for a particular
# # purpose, or non-infringement. 
# 
#


if [ -z "$TOP" -o ! -r "$TOP" ]; then
   ${ECHO} TOP variable not defined
   exit 1
fi

if [ -z "$MV" -o ! -r "$MV" ]; then
    export MV=mv
    export CUT=cut
fi


export LOG_FILE=${TOP}/bin/migrate500xto5211.log 


# Asking for Confirmation - just in case. 
${ECHO}
${ECHO} "This script will cause permanent changes to the database schema use by WPC."
${ECHO} "This change is irreversible. We urge you take a backup of the database schema before you continue."
${ECHO} "Do you want to continue ? (y/n) "
read res
if [ ! "$res" = "y" ]; then
    ${ECHO} "!! exiting"
    exit
fi
# Asking for re-confirmation - just in case. 
${ECHO} "Have you taken a back-up  of the database schema? (y/n) "
read res1
if [ ! "$res1" = "y" ]; then
    ${ECHO} "!! exiting"
    exit
fi


${ECHO} "+++ Schema upgrade has been initiated" | tee ${LOG_FILE} 


. ${TOP}/src/db/schema/util/set_db.sh --confirm_db="no"  >>  ${LOG_FILE}


${ECHO} "+++ Updating table structure ..." | tee -a ${LOG_FILE} 

#--${ECHO} "+++ Dropping and recreating table tutl_jvm_heartbeat"  | tee -a ${LOG_FILE} 

. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_alter_jvm_heartbeat.sql --errorfilter=yes | tee -a ${LOG_FILE} 

${ECHO} "+++ Dropping and recreating table tsec_scu_user"  | tee -a ${LOG_FILE} 
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_alter_scu.sql --errorfilter=yes | tee -a ${LOG_FILE} 

${ECHO} "+++ Dropping and recreating table twbs_wbs_webservice"  | tee -a ${LOG_FILE} 
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_alter_wbs.sql --errorfilter=yes | tee -a ${LOG_FILE} 

${ECHO} "+++ Creating sequence wbs_txn_id"  | tee -a ${LOG_FILE} 
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_create_sequence.sql --errorfilter=yes | tee -a ${LOG_FILE} 

${ECHO} "+++ Creating indexes on tevt_que_queue and tctg_icm_item_category_map tables"  | tee -a ${LOG_FILE} 
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_create_indexes.sql --errorfilter=yes | tee -a ${LOG_FILE} 


${ECHO} "+++ Generating default User authentication scripts ..." | tee -a ${LOG_FILE} 


GET_COMPANIES=`. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/get_cmpcode.sql --errorfilter=yes | ${GREP} 'cmpcode-' `
COMPANIES="${GET_COMPANIES}"

for CMP_CODE_COL in $COMPANIES; 
do
    CMP_CODE=`${ECHO} ${CMP_CODE_COL} | ${GREP} 'cmpcode-' | ${CUT} -f2 -d "-"`
    if [ ! $CMP_CODE = "" ] ; then
        ${ECHO} Updating login scripts for company $CMP_CODE | tee -a ${LOG_FILE}
        ${JAVA_RT_NOOUT} com.ibm.ccd.docstore.util.DocLoader $CMP_CODE Admin $SCRIPTS_DIR/common/ldap_scripts.xml Cp1252 | tee -a ${LOG_FILE} 
    fi
done


${ECHO} "+++ Updating ust table to contain script id" | tee -a ${LOG_FILE}
. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/mig500xto5211_update_ust.sql --errorfilter=yes | tee -a ${LOG_FILE}

#FIX For WPC00021931
${JAVA_RT} com.ibm.ccd.envexpimp.migration.InsertPrecision | tee -a ${LOG_FILE}

${ECHO} "+++ Schema Upgradation complete" | tee -a ${LOG_FILE}
