#!/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.
# usage: pimsupport.sh     --outputdirectory=path/to/store/the/result
#                          --collectdblogs=<yes/no> 
#                          --collectappsvrlogs=<yes/no>
#                          --collectwpcconf=<yes/no>
#                          --collectwpclogs = "all|appsvr|scheduler|workflowengine|queuemanager|admin|eventprocessor"
#                          --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"
UNDERSCORE="_"
CURRENTEXECDIR=`pwd`

function usage
{
echo "usage: pimsupport.sh     --outputdirectory=path/to/store/the/result"
echo "                         --collectdblogs=<yes/no>" 
echo "                         --collectappsvrlogs=<yes/no>"
echo "                         --collectwpcconf=<yes/no>"
echo "                         --collectwpclogs=\"all|appsvr|scheduler|workflowengine|queuemanager|admin|eventprocessor\""
echo "                         --help"
}

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


echo COMMON_INFO_STARTED_SH `date`



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

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

# find the temp directory from common.properties, if not passed in argument list
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
    else
        if [ ! -e ${TMPDIR} ]; then
            ${ECHO} COMMON_ERROR_INVALID_DIR_SH
            exit
        fi
        WPCSUPPORTDIR=${TMPDIR}

    fi
fi

#Validate that atleast one admin service is running, we wanna fail fast
VALIDATE=`$JAVA_RT com.ibm.ccd.common.wpcsupport.util.SupportUtil --cmd=validateAdmin`
if [ "$VALIDATE" = "fail" ]; then
    ${ECHO} COMMON_ERROR_NO_ADMIN_FOUND_SH 
    exit
fi


# Validate the Service List supplied , we wanna fail fast
if [ "X${_CCD_COLLECTWPCLOGS}" != "X" ]; then
    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
    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 $WORKDIR

echo COMMON_INFO_SUPPORT_ARCHIVE_DIR_SH $WORKDIR.zip



function setupdbcredentials
{

    if [ "${_CCD_DB_USER_NAME}" = "" ]; then
        DB_USER_NAME=`${GREP} '^ *db_userName' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
    else
        DB_USER_NAME=${_CCD_DB_USER_NAME}
    fi


    if [ "${_CCD_DB_USER_PWD}" = "" ]; then
        DB_USER_PWD=`${GREP} '^ *db_password' ${CCD_ETC_DIR}/default/common.properties | cut -f2 -d "="`
    else
        DB_USER_PWD=${_CCD_DB_USER_PWD}
    fi


    if [ "${_CCD_DB_SERVICE_NAME}" = "" ]; then
        DB_SERVICE_NAME=`${GREP} '^ *db_url' ${CCD_ETC_DIR}/default/common.properties | ${SED} -e 's/^.*:\([^:]*\) *$/\1/'  -e 's/[0-9]*//' -e s_/__ -e 's/@//'`

    else
        DB_SERVICE_NAME=${_CCD_DB_SERVICE_NAME}
    fi
}

function collectDB2logs
{
    echo COMMON_INFO_COLLECT_INFO_SH "DB2"
    setupdbcredentials
    mkdir $WORKDIR/$DBDIR
    echo COMMON_INFO_INVOKE_EXTRN_SH "db2support"  
    ${DB2SUPPORT} $WORKDIR/$DBDIR  -f -d $DB_SERVICE_NAME -m  -c -u $DB_USER_NAME -p $DB_USER_PWD > /dev/null
}

function collectOraclelogs
{
    echo COMMON_INFO_COLLECT_INFO_SH "ORACLE"
    mkdir $WORKDIR/$DBDIR
    $JAVA_RT com.ibm.ccd.common.db.OracleInfoCollector --tempdir=$WORKDIR/$DBDIR
}

function collectDBLogs
{
    if [ "$CCD_DB" = "db2" ]; then
        collectDB2logs
    fi
    if [ "$CCD_DB" = "oracle" ]; then
        collectOraclelogs
    fi
}

#Do we have to collect Database Logs/Infomation ?
if [ "X${_CCD_COLLECTDBLOGS}" != "X" ]; then
    if [ "$_CCD_COLLECTDBLOGS" = "yes" ]; then
        collectDBLogs
    fi
fi

function collectappsverlogs
{
    case "${CCD_APPSVR}" in
          websphere6|websphere61)
	          if [  "X${WAS_HOME}" != "X" ]; then
	              echo COMMON_INFO_COLLECT_INFO_SH "WebSphere Application Server"
	              if [ ! -d $WORKDIR/$APPSRVR ]; then
	                  mkdir $WORKDIR/$APPSRVR
	              fi
	              ${WAS_HOME}/bin/versionInfo.sh >> $WORKDIR/$APPSRVR/WebsphereVersionInfo.txt
	              cd $WORKDIR/$APPSRVR
	              mkdir $APPSRVRLOGS
	              cd $APPSRVRLOGS
	              ${WAS_HOME}/bin/collector.sh > /dev/null
	          else
	                  ${ECHO} COMMON_ERROR_ENV_VAR_NOT_SET_SH "WAS_HOME"
	                  ${ECHO} COMMON_ERROR_ENV_NOT_SET_IGNORE_SH "WebSphere Application Server"
	          fi
              ;;
          weblogic92)
              SHOULDEXECUTE="true"
              if [ "X${WL_HOME}" = "X" ]; then
                  SHOULDEXECUTE="false"
                  ${ECHO} COMMON_ERROR_ENV_VAR_NOT_SET_SH "WL_HOME"
              fi

              if [ "X${CCD_WL_USER}" = "X" ]; then
                  SHOULDEXECUTE="false"
                  ${ECHO} COMMON_ERROR_ENV_VAR_NOT_SET_SH "CCD_WL_USER"
              fi

              if [ "X${CCD_WL_PW}" = "X" ]; then
                  SHOULDEXECUTE="false"
                  ${ECHO} COMMON_ERROR_ENV_VAR_NOT_SET_SH "CCD_WL_PW"
              fi

              if [ "X${WL_ADMIN_PORT}" = "X" ]; then
                  SHOULDEXECUTE="false"
                  ${ECHO} COMMON_ERROR_ENV_VAR_NOT_SET_SH "WL_ADMIN_PORT"
              fi
                
              if [ "$SHOULDEXECUTE" = "true" ]; then
                  echo  COMMON_INFO_COLLECT_INFO_SH "Weblogic Application Server"
                  if [ ! -d $WORKDIR/$APPSRVR ]; then
                      mkdir $WORKDIR/$APPSRVR
                  fi

                  # Call commEnv here AFTER setting the java_vendor to get common environmental settings.
                  . ${WL_HOME}/common/bin/commEnv.sh

                  # set the class path
                  CLASSPATH="${WEBLOGIC_CLASSPATH}:${POINTBASE_CLASSPATH}:${WL_HOME}/server/lib/xqrl.jar:${JAVA_HOME}/jre/lib/rt.jar:${WL_HOME}/server/lib/webservices.jar:${CLASSPATH}:${CCD_CLASSPATH}"
                  $JAVA_HOME/bin/java weblogic.version -verbose >> $WORKDIR/$APPSRVR/WeblogicVersionInfo.txt
                  cd $WORKDIR/$APPSRVR
                  mkdir $APPSRVRLOGS
                  cd $APPSRVRLOGS
                  ADMIN_URL=http://localhost:${WL_ADMIN_PORT}
                  $JAVA_HOME/bin/java -classpath $CLASSPATH weblogic.Admin -url $ADMIN_URL -username ${CCD_WL_USER}  -password ${CCD_WL_PW} SERVERLOG >> weblogiclogs.txt
              else
                  ${ECHO} COMMON_ERROR_ENV_NOT_SET_IGNORE_SH "Weblogic Application Server"

              fi
              ;;
       esac 

    
}

#Collect Application Server Logs ?
if [ "X${_CCD_COLLECTAPPSVRLOGS}" != "X" ]; then

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

# Finally we are done with appsvr and db logs, lets hit JAVA now.

$JAVA_RT com.ibm.ccd.common.wpcsupport.common.WPCSupport -tempdir="${WORKDIR}" \
                                                         -wpclogs="${_CCD_COLLECTWPCLOGS}" \
                                                         -wpcconf="${_CCD_COLLECTWPCCONF}"

# 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
