#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
# 
# # 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. 
# 
#


# The purpose of this script is to migrate the database schema used by WPC from 5.3.0 or later releases to 5.3.2
# Please consult WPC support before attempting to use this script


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

# Set the 5.3.0.1 database to be migrated to Fresno

${ECHO}
${ECHO} "********************************************************"
${ECHO} "********************************************************"
${ECHO} "**"
${ECHO} MIGRATION_INFO_MIGRATE_SCHEMA_SH
${ECHO} MIGRATION_INFO_BACKUP_DB_WARNING_SH
${ECHO} "**"
${ECHO} "********************************************************"
${ECHO} "********************************************************"
${ECHO}

. ${TOP}/src/db/schema/util/set_db_localized.sh "$@"
_ERROR_LOGS=$TOP/logs/errfile.log

init()
{

${ECHO}  "MIGRATION_INFO_MIGRATE_ACG_SH"                     
${ECHO}  "MIGRATION_INFO_MIGRATE_CAT_ITA_ATTRIBUTES_SH"      
${ECHO}  "MIGRATION_INFO_MIGRATE_SCHEMA_SYNCHRONIZATION_SH"  
${ECHO}  "MIGRATION_INFO_MIGRATE_SCRIPT_CACHE_PERSISTENCE_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_CEH_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_WORKFLOW_CACHE_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_BLOB_PERFORMANCE_ENHANCEMENTS_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_WBS_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_TUTL_LCK_LOCK_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_SYN_SYNDICATION_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_ALL_SH"                     

${ECHO}  " ------------------------------------------------------"
${ECHO} "MIGRATION_INFO_MIGRATE_FP12FRESNO_SH"
${ECHO}  " ------------------------------------------------------"

# Removed the 1st option migrateRichSearch so that it cannot be run independently.
case $1 in 

    "2") migrateACG;;
    "3") migrateCategoryItemAttributes;;
    "4") migrateSchemaSynchronization;;
    "5") migrateScriptCachePersistence;;
    "6") migrateCeh;;
    "7") migrateWorkflowCache;;
    "8") migrateBlobPerformanceEnhancements;;
    "9") migrateWbs;;
    "10") migrateTutlLckLock;;
    "11") migrateSynSyndication;;    
    "12") migrateACG;migrateCategoryItemAttributes;migrateSchemaSynchronization;migrateScriptCachePersistence;migrateCeh;migrateWorkflowCache;migrateBlobPerformanceEnhancements;migrateWbs;migrateTutlLckLock;migrateSynSyndication;migrateRichSearch;;

esac 

    # invariably run the script for migrating sequence batch and wfe index ( To avoid PII messages )
    migrateSequenceBatchSize
    migrateWfeIndex
    migrateIndexes

}

getErrorCountInLogFile()
{
    if [ ! -f $_ERROR_LOGS ]; then
        errorCount=0
    else
        errorCount=`cat $_ERROR_LOGS | wc -c`
    fi
}

DoStartAndPrepare()
{
    ${ECHO} 
    ${ECHO} $1
    ${ECHO} 

    getErrorCountInLogFile
    initial=$errorCount
}

DoEndAndReportError()
{

    getErrorCountInLogFile
    final=$errorCount

    if [ ! $initial -eq $final ]; then

        ${ECHO} "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        ${ECHO} $1
        ${ECHO} "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        ${ECHO} "MIGRATION_INFO_CHECK_ERROR_LOG_FILE_SH" $_ERROR_LOGS
        
        exit -1

    fi
        ${ECHO} "-----------------------------------------"
        ${ECHO} $2
        ${ECHO} "-----------------------------------------"

}


migrateRichSearch()
{

    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Rich Search Results"


        GET_COMPANIES=`. ${DB_DIR}/util/run_db_cmd.sh --file=$DB_CMN_SCRIPTS/get_cmpcode.sql --errorfilter=yes 2>> $_ERROR_LOGS | ${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} "MIGRATION_INFO_UPDATING_WPC_SCRIPTS_SH" $CMP_CODE
            	${JAVA_RT_NOOUT} com.ibm.ccd.docstore.util.DocLoader $CMP_CODE Admin $SCRIPTS_DIR/common/report_scripts.xml Cp1252 2>> $_ERROR_LOGS
            fi
        done

    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Rich Search Results." "MIGRATION_INFO_SUCCEEDED_SH Rich Search Results Export Script"

}

migrateACG()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH ACG"

    $JAVA_RT_CMD com.ibm.ccd.common.migration.MigrateACGStepOne 2>> $_ERROR_LOGS
    $JAVA_RT_CMD com.ibm.ccd.common.migration.MigrateACGStepTwo 2>> $_ERROR_LOGS
    
    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH ACG." "MIGRATION_INFO_SUCCEEDED_SH ACG"

}
migrateCategoryItemAttributes()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Category Item Attributes"
 
    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_cab_category_attributes_table.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_cab_category_attributes_table2.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    
    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_ita_item_attributes_table.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_ita_item_attributes_table2.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    $JAVA_RT_CMD com.ibm.ccd.common.migration.MigrateValueStringForSearch $CCD_DB 10000 

    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Category Item Attributes." "MIGRATION_INFO_SUCCEEDED_SH Category Item Attributes"
}

migrateSchemaSynchronization()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Schema Synchronization"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/schemaSynchMigrate.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"



        ${JAVA_RT} com.ibm.ccd.synchronize.DBSchemaVersion --autoupd 2>> $_ERROR_LOGS

    
    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Schema Synchronization." "MIGRATION_INFO_SUCCEEDED_SH Schema Synchronizaiton"
}

migrateScriptCachePersistence()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Script Cache Persistence"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/ScriptCachePersistentMapCreate.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Script Cache Persistence." "MIGRATION_INFO_SUCCEEDED_SH Script Cache Persistence"
}

migrateCeh()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH CEH"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_ceh_coll_entry_history.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH CEH." "MIGRATION_INFO_SUCCEEDED_SH CEH "
}
migrateWorkflowCache()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Workflow Cache"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/WorkflowCache.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Workflow Cache." "MIGRATION_INFO_SUCCEEDED_SH WorkflowCache"
}

migrateBlobPerformanceEnhancements()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Blob Performance Enhancements"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/BlobPerformance.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Blob Performance Enhancements." "MIGRATION_INFO_SUCCEEDED_SH Blob Performance Enhancements"
}

migrateWbs()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH wbs "



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_wbs_table.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH wbs ." "MIGRATION_INFO_SUCCEEDED_SH wbs "
}

migrateTutlLckLock()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH tutl_lck_lock "



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_tutl_lck_lock.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH tutl_lck_lock." "MIGRATION_INFO_SUCCEEDED_SH tutl_lck_lock "
}

migrateSynSyndication()
{
   DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH tctg_syn_syndication"



        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/alter_syn_syndication.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"


        DoEndAndReportError  "MIGRATION_INFO_FAILED_SH tctg_syn_syndication." "MIGRATION_INFO_SUCCEEDED_SH tctg_syn_syndication"
}

migrateSequenceBatchSize()
{
   DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH sequence batch size"


   . ${DB_DIR}/util/update_sequence_batch_size.sh -newbatchsize=200 --errorfilter=yes 2>> $_ERROR_LOGS

    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH sequence batch size." "MIGRATION_INFO_SUCCEEDED_SH sequence batch size"

}


migrateWfeIndex()
{
   DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH WFE Index"

        . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/create_wfe_index.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH WFE Index." "MIGRATION_INFO_SUCCEEDED_SH WFE Index"

}

migrateIndexes()
{
    DoStartAndPrepare "MIGRATION_INFO_UPDATING_SH Indexes"

    . ${DB_DIR}/util/run_db_cmd.sh --file=$DB_SCRIPTS/add_new_indexes.sql --errorfilter=yes 2>> $_ERROR_LOGS| ${GREP} -Ev "ORA-01418:|ERROR|complete.|dropped.|created.|altered.|^$"

    DoEndAndReportError  "MIGRATION_INFO_FAILED_SH Indexes." "MIGRATION_INFO_SUCCEEDED_SH Indexes"
}


#Removed the message for migrateRichSearch. Remaining messages are not altered so as to avoid PII impact.
if [ $1 -lt 1 ]
then
${ECHO} "MIGRATION_INFO_SCRIPT_USAGE_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_ACG_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_CAT_ITA_ATTRIBUTES_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_SCHEMA_SYNCHRONIZATION_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_SCRIPT_CACHE_PERSISTENCE_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_CEH_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_WORKFLOW_CACHE_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_BLOB_PERFORMANCE_ENHANCEMENTS_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_WBS_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_TUTL_LCK_LOCK_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_SYN_SYNDICATION_SH"
${ECHO}  "MIGRATION_INFO_MIGRATE_ALL_SH"
exit 1
else
     init $1
fi
