#!/bin/bash
# IBM Confidential
# OCO Source Materials
# 5724-V51
#
# (c) Copyright IBM Corp. 2000, 2008 All Rights Reserved.
#
# The source code for this program is not published or otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.

# This is the interface script between pimSuport.sh and actual integrityVerify
# scripts. Based on options, this script will figure out a proper corresponding
# integrity scripts to execute. It is assumed that integrityVerify package will
# keep the "entry" script command=line parameters intacted.
#
# To see usage information: 
# read $TOP/bin/diagnostic/integrity_check/integrityCheck.usage, or 
# integrityCheck.sh  --help

. ${TOP}/bin/get_params.sh

WPCSUPPORTDIR=""
TIMESTAMP=$(date +%a.%d.%b.%y_%S)
#TIMESTAMP=`$JAVA_RT com.ibm.ccd.common.wpcsupport.util.SupportUtil --cmd=getTimeStamp`
WPCSUPPORT="integrityCheck"
UNDERSCORE="_"
CURRENTEXECDIR=`pwd`
TOPBINDIR=$TOP/bin
SAVEDDIR=$CURRENTEXECDIR


#if [ "$CURRENTEXECDIR" != "$TOP/bin" ]; then
#    # echo "Please execute pimSupport.sh under $TOP/bin directory"
#    cd $TOPBINDIR
#    CURRENTEXECDIR=`pwd`
#fi

. $TOPBINDIR/diagnostic/common/common.functions

if [ $# == 0 ]; then
    cat $TOPBINDIR/diagnostic/integrity_check/integrityCheck.usage 
    exit 0
fi

for _VAR in "$@"; do
    case ${_VAR} in
        --help)
        cat $TOPBINDIR/diagnostic/integrity_check/integrityCheck.usage 
        exit
    esac
    case ${_VAR} in
        --version)
        cat $TOPBINDIR/diagnostic/integrity_check/integrityCheck.version
        exit
    esac
done

echo "[integrityCheck.sh] command line: $@"

# get database login
setupdbcredentials

if [ "X${_CCD_CODE}" == "X" ]; then
	echo "Company code is required, please specify --code=<comp_code> option"
	exit 1
fi


if [ "X${_CCD_INTEGRITY}" != "X" ]; then
    # first detect if having other options?
    TASK_AREA=oneCmp
    ADDITIOINAL_TASK_OPTION=

    if [ "X${_CCD_COLAREA}" != "X" ] && [ "X${OPT_COLAREA}" != "X" ]; then 
	ADDITIONAL_TASK_OPTION="--coaname=$OPT_COLAREA"
	# clear OPT_COLARA
	export OPT_COLAREA=
	TASK_AREA=oneCmpOneCA
    fi

    if [ "X${_CCD_CATALOG}" != "X" ] && [ "X${OPT_CATALOG}" != "X" ]; then
        ADDITIONAL_TASK_OPTION="--ctgname=$OPT_CATALOG"
	# clear OPT_CATALOG
	export OPT_CATALOG=
	TASK_AREA=oneCmpOneCat
    fi

    # basic mode
    # 
    if [ "$_CCD_INTEGRITY" == "basic" ]; then
	echo "Executing: $TOP/src/integrity/basic/${CCD_DB}/basic_integrity_verify.sh $@"
	${TOP}/src/integrity/basic/${CCD_DB}/basic_integrity_verify.sh $@ 
    fi 

    # workflow mode or serialization mode
    #
    if [ "$_CCD_INTEGRITY" == "workflow" ] || [ "$_CCD_INTEGRITY" == "serialization" ]; then
	if [ "$CCD_DB" == "db2" ]; then
	    echo "Executing: $TOP/src/integrity/${TASK_AREA}/${_CCD_INTEGRITY}/${CCD_DB}/${_CCD_INTEGRITY}_integrity_verify.sh --db=$DB_SERVICE_NAME --dbuser=$DB_USER_NAME --dbpassword=$DB_USER_PWD --code=${_CCD_CODE} $ADDITIONAL_TASK_OPTION"
            MY_TEMP_DIR=`pwd`
	    cd $TOP/src/integrity/${TASK_AREA}/${_CCD_INTEGRITY}/${CCD_DB}
	    ./${_CCD_INTEGRITY}_integrity_verify.sh --db=$DB_SERVICE_NAME --dbuser=$DB_USER_NAME --dbpassword=$DB_USER_PWD --code=${_CCD_CODE} ${ADDITIONAL_TASK_OPTION}
	    cd $MY_TEMP_DIR
        fi
	
	if [ "$CCD_DB" == "oracle" ]; then
	    echo "Executing: $TOP/src/integrity/${TASK_AREA}/${_CCD_INTEGRITY}/${CCD_DB}/${_CCD_INTEGRITY}_integrity_verify.sh --code=${_CCD_CODE} $ADDITIONAL_TASK_OPTION"
            MY_TEMP_DIR=`pwd`
	    cd $TOP/src/integrity/${TASK_AREA}/${_CCD_INTEGRITY}/${CCD_DB}
	    ./${_CCD_INTEGRITY}_integrity_verify.sh --code=${_CCD_CODE} ${ADDITIONAL_TASK_OPTION}
	    cd $MY_TEMP_DIR
        fi
    fi 


    # full - fullIntegrityCheck 
    #
    if [ "$_CCD_INTEGRITY" == "full" ]; then
	echo "Executing: $TOP/src/integrity/${TASK_AREA}/fullIntegrityVerify.sh --code=${_CCD_CODE} --basic=y --workflow=y --serialization=y $ADDITIONAL_TASK_OPTION"
        MY_TEMP_DIR=`pwd`
	cd $TOP/src/integrity/${TASK_AREA}/
	./fullIntegrityVerify.sh --code=${_CCD_CODE} --basic=y --workflow=y --serialization=y $ADDITIONAL_TASK_OPTION
	cd $MY_TEMP_DIR
    fi 
fi

# move any files generated to $TOP/logs/diagnostic/ directory
if [ -d $TOP/logs/badBlobsReports ]; then
	if [ -d $TOP/logs/diagnostic/badBlobsReports ]; then
		cp $TOP/logs/badBlobsReports/*.* $TOP/logs/diagnostic/badBlobsReports 2> /dev/null
		rm -rf $TOP/logs/badBlobsReports
	else
		mv $TOP/logs/badBlobsReports $TOP/logs/diagnostic
	fi	
fi
if [ -d $TOP/logs/ghost_items_reports ]; then
	if [ -d $TOP/logs/diagnostic/ghost_items_reports ]; then
		cp $TOP/logs/ghost_items_reports/*.* $TOP/logs/diagnostic/ghost_items_reports 2> /dev/null
		rm -rf $TOP/logs/ghost_items_reports
	else
		mv $TOP/logs/ghost_items_reports $TOP/logs/diagnostic
	fi	
fi

cd $SAVEDDIR

