#!/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.
# To see usage information: 
# read $TOP/bin/diagnostic/pim_support/pimSupport.usage, or 
# pimSupport.sh  --help

. ${TOP}/bin/get_params.sh

WPCSUPPORTDIR=""
#TIMESTAMP=$(/bin/date +%a.%d.%b.%y_%S)
TIMESTAMP=`$JAVA_RT com.ibm.ccd.common.wpcsupport.util.SupportUtil --cmd=getTimeStamp`
WPCSUPPORT="pimSupport"
DBDIR="dblogs"
DB2SUPPORT="db2support"
APPSRVR="ApplicationServer"
APPSRVRLOGS="Logs"
SYSINFODIR="syslogs"
MONITORINFODIR="monitorlogs"
HIGHCPUDIR="highcpulogs"
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

. $TOP/bin/diagnostic/common/common.functions
. $TOP/bin/diagnostic/pim_support/pimSupport.functions

if [ $# == 1 ]; then
    usage
    exit 0
fi

for _VAR in "$@"; do
    case ${_VAR} in
        --help)
        usage
        exit 0
    esac
    case ${_VAR} in
        --version)
	version
        exit 0
    esac
done

echo COMMON_INFO_STARTED_SH `date`

# For making version correlation between ISA plugin and WPC release, we will try to 
# to report if a specific command-line options are available in pimSupport.sh or not.
# For example, if a user takes a newer version of ISA plugin to work with a older version
# of WPC release, and the ISA menu has new optoins that WPC pimSupport.sh does not know,
# we will report "Option not supported in this pimSupport.sh version".
OPTION_PROCESSED=false

if [ "X${_CCD_OUTPUTDIRECTORY}" != "X" ]; then
    if [ ! -e ${_CCD_OUTPUTDIRECTORY} ]; then
        ${ECHO} COMMON_ERROR_INVALID_DIR_SH ${_CCD_OUTPUTDIRECTORY}
        exit 5
    fi
    if [ ! -d ${_CCD_OUTPUTDIRECTORY} ]; then
        ${ECHO} COMMON_ERROR_NOT_DIR_SH ${_CCD_OUTPUTDIRECTORY}
        exit 5
    fi
    if [ ! -w ${_CCD_OUTPUTDIRECTORY} ]; then
        ${ECHO} COMMON_ERROR_NO_WRITE_PERM_SH ${_CCD_OUTPUTDIRECTORY}
        exit 5
    fi

      #convert all the relative paths to absolute path
      cd ${_CCD_OUTPUTDIRECTORY}
      _CCD_OUTPUTDIRECTORY=`pwd`
      cd -
        WPCSUPPORTDIR=${_CCD_OUTPUTDIRECTORY}/
fi

if [  "X${_CCD_OUTPUTDIRECTORY}" = "X" ]; then
    TMPDIR=`$JAVA_RT com.ibm.ccd.common.util.Configuration --cmd=tmpdir`
    if [ "$TMPDIR" = "" ]; then
        ${ECHO} COMMON_ERROR_INVALID_DIR_BOTH_SH
        exit 5
    else
        if [ ! -e ${TMPDIR} ]; then
            ${ECHO} COMMON_ERROR_INVALID_DIR_SH
            exit 5
        fi
        WPCSUPPORTDIR=${TMPDIR}

    fi
fi

# If we are here that means we have found out a directory where we can work  and service list is empty or well formed.
# create a directory with a timestamp within that.

WORKDIR=${WPCSUPPORTDIR}$WPCSUPPORT$UNDERSCORE$TIMESTAMP
mkdir -p $WORKDIR
# echo COMMON_INFO_SUPPORT_ARCHIVE_DIR_SH $WORKDIR.zip

# dump high-cpu process information
if [ "X${_CCD_DUMPPROCESS}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    # produce process dump in $TOP/logs/high_cpu
    echo COMMON_INFO_COLLECT_INFO_SH "High-CPU Process Info log files ...... wait about 2 minutes!"       
    $TOP/bin/diagnostic/common/dumpProcessInfo.sh ${_CCD_DUMPPROCESS}

    # copy dump files from $TOP/logs/high_cpu to $WORKDIR/$HIGHCPUDIR
    collectProcessInfo
fi


# collect system information
if [ "X${_CCD_COLLECTSYSINFO}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    if [ "$_CCD_COLLECTSYSINFO" = "yes" ]; then
        # produce system info output
        $TOP/bin/diagnostic/common/systemInfo.sh
 
        # copy dump files from $TOP/logs/diagnostic/env_info to $WORKDIR/$HIGHCPUDIR
        collectSysInfo
    fi
fi


# collect log files from background system monitoring
if [ "X${_CCD_COLLECTMONITORLOGS}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    if [ "$_CCD_COLLECTMONITORLOGS" = "yes" ]; then
        collectMonitorLogs
    fi
fi


#collect Database Infomation
if [ "X${_CCD_COLLECTDBINFO}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    if [ "$_CCD_COLLECTDBINFO" = "yes" ]; then
        collectDBLogs
    fi
fi

#Collect Application Server Logs ?
if [ "X${_CCD_COLLECTAPPSVRLOGS}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    if [ "$_CCD_COLLECTAPPSVRLOGS" = "yes" ]; then
        collectappsverlogs
    fi
fi



if [ "X${_CCD_COLLECTWPCLOGS}" != "X" ]; then
    # mark command line option handled
    OPTION_PROCESSED=true

    VALIDATE=`$JAVA_RT com.ibm.ccd.common.wpcsupport.util.SupportUtil --cmd=validateAdmin`
    if [ "$VALIDATE" = "fail" ]; then
	${ECHO} COMMON_ERROR_NO_ADMIN_FOUND_SH 
	exit 1
    fi

    VALIDATE=`$JAVA_RT com.ibm.ccd.common.wpcsupport.util.SupportUtil --cmd=validateServc --svclist="${_CCD_COLLECTWPCLOGS}"`
    if [ "$VALIDATE" = "false" ]; then
        ${ECHO} COMMON_ERROR_INVALID_SVCLIST_SH "\"[all|appsvr|scheduler|workflowengine|queuemanager|admin|eventprocessor]\""
        exit 1
    fi

    #  Also we zip all files collected under $WORDIR.
    $JAVA_RT com.ibm.ccd.common.wpcsupport.common.WPCSupport -tempdir="${WORKDIR}" \
                                                         -wpclogs="${_CCD_COLLECTWPCLOGS}" \
                                                         -wpcconf="${_CCD_COLLECTWPCCONF}"
fi

if [ "$OPTION_PROCESSED" == "false" ]; then
	echo "-------------------------------------------------------------"
	echo "The option is not supported in this pimSupport.sh version, for command line: $@"
	cat $TOP/bin/diagnostic/pim_support/pimSupport.version
	echo "-------------------------------------------------------------"
	exit 2
fi

# when all is done, remove the folder{we want the zip file not the folder}
if [ -d ${WORKDIR} ]; then
    cd ${CURRENTEXECDIR}

	`rm -rf  ${WORKDIR}`
    
fi

cd $SAVEDDIR

exit 0
