#!/bin/bash
# 
# # 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. 
# 
#


#--------------------------------------------------------------------------------
# Prerequisites:
#  - environment variable TOP pointing to the root WPC directory   
#       export TOP=/opt/IBM/WPC
#  - environment variable JDK_HOME, pointing to the root of the java directory
#       export JDK_HOME=/usr/local/java1.4.2
#  - this looks for a java compiler in $JAVAC, or jikes at $JDK_HOME/bin/jikes or uses javac
#  - your jars should be in JARDIR
#  - you may export CUSTOM_JAVA_RT_OPTIONS=Xmx512M to set a larger heap
#
# Features:
#  - sets up JAVA_HOME for use
#  - sets up JAVAC for use
#  - sets up APPSVR_OPTS for you
#  - sets up SCHEDULER_MEMORY_FLAG for you
#
# NOTE: If you add any prereq env vars in the section above, please
#       check for the empty conditions below and alert according.
#
#
#
#--------------------------------------------------------------------------------



#--------------------------------------------------------------------------------
# utils
#--------------------------------------------------------------------------------

#AddJar <jar name>
AddJar() {
    JARNAME=$1
    if [ ! -r "$JARNAME" ]; then
        ${ECHO} "Error!: $JARNAME not found."
    fi

    if [ -z "$CCD_CLASSPATH" ]; then
        CCD_CLASSPATH="$JARNAME"
    else
        CCD_CLASSPATH="$CCD_CLASSPATH$PATH_SEPARATOR$JARNAME"
    fi
    export CCD_CLASSPATH
}

# ---> Added to deal with user jar files
#AddUserJar <jar name>
AddUserJar() {
    JARNAME=$1
     NUMBER_OF_VALID_USER_JAR_FILES=`expr $NUMBER_OF_VALID_USER_JAR_FILES + 1`
     if [ -z "$CCD_USER_CLASSPATH" ]; then
        CCD_USER_CLASSPATH="$JARNAME"
     else
        CCD_USER_CLASSPATH="$CCD_USER_CLASSPATH$PATH_SEPARATOR$JARNAME"
     fi
    export CCD_USER_CLASSPATH
}
# <--- Added to deal with user jar files

#VerifyVar <name> <value> [defaultvalue]
VerifyVar() {
    NAME="$1"
    VALUE="$2"
    DEFAULT="$3"
    if [ -z "${VALUE}" ]; then
        ${ECHO} "Error!: ${NAME} environment variable is not set."
        if [ ! -z "${DEFAULT}" ]; then
            eval "${NAME}='${DEFAULT}'"; export $NAME
        fi
    fi
}

#VerifyFile <filename> [text]
VerifyFile() {
    FILENAME="$1"
    TEXT="$2"
    if [ ! -r "${FILENAME}" ]; then
        ${ECHO} "Error!: file ${FILENAME} not found. $TEXT"
    fi
}

#--------------------------------------------------------------------------------
# WPC specific variables
#--------------------------------------------------------------------------------

CCDVars() {
    VerifyVar "TOP" "$TOP"
    ${ECHO} "  Using TOP=$TOP"
    VerifyVar "JDK_HOME" "$JDK_HOME"
    VerifyVar "JARDIR" "$JARDIR"
    VerifyVar "SHELL" "$SHELL"


    # use ccd_etc_dir=$TOP/etc
    CCD_ETC_DIR=$TOP/etc; export CCD_ETC_DIR

    if [ -z "${CCD_ETC_DIR}" ]; then
        CCD_ETC_DIR=$TOP/etc; export CCD_ETC_DIR
    fi
    # jdk stream variable
    JDK_STREAM=$($JDK_HOME/bin/java -version 2>&1 | awk -F\" '/^java /{print $2}' | cut -d_ -f1)



    VerifyVar "CCD_ETC_DIR" "$CCD_ETC_DIR" 

    DB_ROOT_DIR=$TOP/src/db; export DB_ROOT_DIR
    DB_DIR=$TOP/src/db/schema; export DB_DIR
    SCRIPTS_DIR=$TOP/src/db/scripts; export SCRIPTS_DIR
    JAVA_CLASSDIR=$TOP/classes; export JAVA_CLASSDIR
    JAVAAPI_CLASSDIR=$TOP/javaapi_classes; export JAVAAPI_CLASSDIR
    OLDJAVAAPI_CLASSDIR=$TOP/oldjavaapi_classes; export OLDJAVAAPI_CLASSDIR    
    JUNIT_CLASSDIR=$TOP/junit_classes; export JUNIT_CLASSDIR
    TOMCAT_CLASSDIR=$TOP/jspclasses/jsp_tomcat60; export TOMCAT_CLASSDIR
    PUBLIC_HTML=$TOP/public_html; export PUBLIC_HTML
    CENTERSTAGE=/opt/xmlconnect; export CENTERSTAGE
    ADMIN_DIR=$TOP/bin/go; export ADMIN_DIR
    SVC_LOG_DIR=$TOP/logs; export SVC_LOG_DIR

    #get short hostname (baby.ibm.com -> baby)
    SHORT_HOSTNAME=`hostname|${SED} -e 's/^\([^.]*\)\..*$/\1/'|${TR} a-z A-Z`; export SHORT_HOSTNAME
    FULL_HOSTNAME=$(host $HOSTNAME | cut -f1 -d' ')

    . $TOP/bin/read_config_file.sh ${CCD_ETC_DIR}/default/common.properties
}


#--------------------------------------------------------------------------------
# database
#--------------------------------------------------------------------------------

DatabaseSetup() {

    VerifyVar "CCD_DB" "$CCD_DB" "oracle"

# * Set PATH for Database Specific SQL Command Line Processor
# * Set CCD_SQLPLUS to Database Specific SQL Command
#   Line Processor Executable
# * Set New Variable DB_SQLFILE_EXECUTE to set the
#   Database Specific SQL file processor command
# * Set New Variable DB_DISCONNECT to set the
#   Database Specific Connection Terminator
# * Set a New Variable DDL_ERROR_FILTER which stores
#   Database specific Error Codes to filter output during
#   execution of the DDLs

    case ${CCD_DB} in
        oracle)
            CCD_SQLPLUS="sqlplus -s"; export CCD_SQLPLUS
            CCD_SQLPLUS_LOGIN="sqlplus"; export CCD_SQLPLUS_LOGIN
            DB_SQLFILE_EXECUTE="start "; export DB_SQLFILE_EXECUTE
            DB_SQLLINE_EXECUTE=""; export DB_SQLLINE_EXECUTE
            DB_DISCONNECT="quit;"; export DB_DISCONNECT
            DDL_ERROR_FILTER="ORA-01418: |ERROR at line |ORA-00942: |select|SELECT|*"; export DDL_ERROR_FILTER
            ;;
        db2)
                CCD_SQLPLUS="db2 -t +c "; export CCD_SQLPLUS
            CCD_SQLPLUS_LOGIN=$CCD_SQLPLUS; export CCD_SQLPLUS_LOGIN
            DB_SQLFILE_EXECUTE="db2 -tf "; export DB_SQLFILE_EXECUTE
            DB_SQLLINE_EXECUTE="db2 -t +c "; export DB_SQLLINE_EXECUTE
            DB_DISCONNECT="db2 -t terminate;"; export DB_DISCONNECT
            DDL_ERROR_FILTER="SQL2205N |ERROR at line |SQL0204N |DB21034E |valid Command Line |SQLSTATE=42704"; export DDL_ERROR_FILTER
            VerifyVar "DB2_HOME" "$DB2_HOME"
            ;;
        *)
            ${ECHO} "Error! CCD_DB has not been set! Build Processes will fail"
    esac

    DB_SCRIPTS="$TOP/src/db/schema/dbscripts/$CCD_DB"; export DB_SCRIPTS
    DB_CMN_SCRIPTS="${TOP}/src/db/schema/dbscripts/common"; export DB_CMN_SCRIPTS

    ${ECHO} "  Setting up for ${CCD_DB}"
}


#--------------------------------------------------------------------------------
# app server
#--------------------------------------------------------------------------------

AppSvrCheck() {
    VerifyVar "CCD_APPSVR" "$CCD_APPSVR" "websphere6"

    case "${CCD_APPSVR}" in
            
        tomcat60)
            VerifyVar "TOMCAT_HOME" "$TOMCAT_HOME"
            ;; 
            
        weblogic92)
                    export WL_HOME_UNIX_STYLE="$WL_HOME"
            
            VerifyVar "WL_HOME" "$WL_HOME"
            VerifyVar "CCD_WL_DOMAIN" "$CCD_WL_DOMAIN"
            VerifyVar "WL_HOME_UNIX_STYLE" "$WL_HOME_UNIX_STYLE"
            VerifyVar "CCD_WL_DOMAIN_DIR" "$CCD_WL_DOMAIN_DIR"
            VerifyVar "CCD_WL_SERVER" "$CCD_WL_SERVER"
            VerifyVar "CCD_WL_USER" "$CCD_WL_USER"
            VerifyVar "CCD_WL_PW" "$CCD_WL_PW"
            VerifyFile "$CCD_WL_DOMAIN_DIR/security/SerializedSystemIni.dat" "Please copy it from your weblogic installation directory"
            VerifyFile "$CCD_WL_DOMAIN_DIR/fileRealm.properties" "Please copy it from your weblogic installation directory"
            VerifyFile "$CCD_WL_DOMAIN_DIR/security/DefaultAuthenticatorInit.ldift" "Please copy it from your weblogic installation directory"
            VerifyFile "$CCD_WL_DOMAIN_DIR/security/DefaultAuthorizerInit.ldift" "Please copy it from your weblogic installation directory"
            VerifyFile "$CCD_WL_DOMAIN_DIR/security/DefaultRoleMapperInit.ldift" "Please copy it from your weblogic installation directory"

            ;;
        websphere6)
            VerifyVar "WAS_HOME" "$WAS_HOME"
            VerifyVar "WAS_APPLICATION_SERVER_PROFILE" "$WAS_APPLICATION_SERVER_PROFILE"
            VerifyVar "WAS_CELLNAME" "$WAS_CELLNAME"
            VerifyVar "WAS_NODENAME" "$WAS_NODENAME"
            VerifyVar "WAS_APPSERVERNAME" "$WAS_APPSERVERNAME" "TrigoProductCenter"
            ;;
        *)
            ${ECHO} "Error!: CCD_APPSVR set to \"$CCD_APPSVR\". Unknown value"
            ;;
    esac

    ${ECHO} "  Setting up for $CCD_APPSVR"

}

#--------------------------------------------------------------------------------
# app variables (scheduler, appsvc, javac)
#--------------------------------------------------------------------------------

AppVars() {

    #soap
    export CCD_URL="http://${HOSTNAME}:${_CCD_CONF_APPSVR_PORT}"
}

#--------------------------------------------------------------------------------
# os
#--------------------------------------------------------------------------------

OSInit() {
    if [ `uname` = "Linux" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=":"
        export PLATFORM=LINUX
        ${ECHO} "  Setting up for linux"
    elif [ `uname` = "SunOS" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=":"
        export PLATFORM=SUN
        ${ECHO} "  Setting up for solaris"
    elif [ `uname` = "AIX" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=":"
        export PLATFORM=AIX
        ${ECHO} "  Setting up for AIX"
    elif [ `uname` = "HP-UX" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=":"
        export PLATFORM=HP
        ${ECHO} "  Setting up for HP"
    fi
}

# ---> Added to deal with user jar files
#--------------------------------------------------------------------------------
# User classpath
#--------------------------------------------------------------------------------

InitUserClasspath() {
    CCD_USER_CLASSPATH=
    NUMBER_OF_VALID_USER_JAR_FILES=0
    if [ -r $CCD_ETC_DIR/default/user_jars.list ]; then
       ${ECHO} "Adding user jar files."
       TMP_FILE=tester
       #strip dos linefeeds, and comment lines from input.
       ${CAT} $CCD_ETC_DIR/default/user_jars.list | ${SED} -e 'sx$xx' |${SED} -e 'sx^//.*$xx' > $TMP_FILE
       while read LINE; do
                # need to get unix to interpret variables
                ALINE=`eval echo $LINE`
                if [ ! -z "$ALINE" ]; then
                  AddUserJar "$ALINE"
                fi
       done < $TMP_FILE

       ${RM} $TMP_FILE
       ${ECHO} "Added $NUMBER_OF_VALID_USER_JAR_FILES user jar files"
    fi
}
# <--- Added to deal with user jar files

#--------------------------------------------------------------------------------
# classpath
#--------------------------------------------------------------------------------

InitClasspath() {
    export CCD_CLASSPATH=

    AddJar $TOP/jars/ccd_svr.jar
    AddJar $TOP/jars/ccd_javaapi.jar
    AddJar $TOP/jars/ccd_javaapi2.jar
    
    AddJar $TOP/jars/ccd_trace_aspects.jar
    
    export CCD_CLASSPATH=$CCD_CLASSPATH$PATH_SEPARATOR$JAVAAPI_CLASSDIR$PATH_SEPARATOR$OLDJAVAAPI_CLASSDIR
    export CCD_CLASSPATH=$CCD_CLASSPATH$PATH_SEPARATOR$JUNIT_CLASSDIR


    # JAVA JVM

    AddJar $JDK_HOME/lib/tools.jar

    # Add in the aspectj runtime
    AddJar $JARDIR/aspectjrt-1.5.3.jar

    # Logging
    AddJar $JARDIR/log4j-1.2.8.jar
    #xerces (this has to come before weblogic.jar)
    #Manjunath Subramanian IBM Corporation, 18-Nov-2004

    AddJar $JARDIR/xercesImpl_4_4_6.jar
    AddJar $JARDIR/xml-apis_4_4_6.jar

    AddJar $JARDIR/xalan_2_7_5.jar


    # To Use Sentry Spell Checking Engine From WinterTree Software
    # Make a SymLink to the installed WinterTree jar file named ssce.jar in the <WINTERTREE_INSTALL_DIR>/runtime/lib from
    # <WPC_INSTALL_DIR>/jars/ssce.jar
    # Add this line AddJar <WPC_INSTALL_DIR>/jars/ssce.jar to this file as shown in the bracket.

    # for our thread pool
    # compiled for source available at http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
    AddJar $JARDIR/concurrent-utils-1.3.2.jar

    #xalan

    #javamail
    AddJar $JARDIR/mail_ibm_1.3.jar

    #I18N
    #ICU4J v3.8.1
    AddJar $JARDIR/icu4j-3_8_1.jar

    #BiDi transformation
    AddJar $JARDIR/bdlayout.jar

    #WPC LTA adapter plug-in
    AddJar $JARDIR/com.ibm.ccd.logging.parsers_1.0.2.jar
 
    #JDBC driver setup
    if [ "$CCD_DB" = "oracle" ]; then
      # oracle
        AddJar $ORACLE_JDBC_DRIVER
    elif [ "$CCD_DB" = "db2" ]; then
        # DB2
        AddJar $DB2_HOME/java/db2jcc.jar
        AddJar $DB2_HOME/java/db2jcc_license_cu.jar
    else
        ${ECHO} "Error! CCD_DB has not been set correctly! Build Processes will fail"
    fi

    #IBM Eclipse SDK jars. Used in CommomBaseEvent, SymptomCatalogGeneration etc
    AddJar $JARDIR/hexcore_4.4.1.jar
    AddJar $JARDIR/hexr_4.4.1.jar
    AddJar $JARDIR/hlcbe101_4.4.1.jar
    AddJar $JARDIR/hlcore_4.4.1.jar
    AddJar $JARDIR/hllog4j_4.4.1.jar
    AddJar $JARDIR/tlcore_4.4.1.jar
    AddJar $JARDIR/tptp-models_4.4.1.jar
    AddJar $JARDIR/org.eclipse.emf.common_2.3.2.v200802051830.jar
    AddJar $JARDIR/org.eclipse.emf.ecore_2.3.2.v200802051830.jar
    AddJar $JARDIR/org.eclipse.emf.ecore.xmi_2.3.2.v200802051830.jar



    # servlet + app server classes
    case "${CCD_APPSVR}" in
        tomcat60)
            AddJar $TOMCAT_HOME/bin/bootstrap.jar
            AddJar $TOMCAT_HOME/lib/annotations-api.jar
            AddJar $TOMCAT_HOME/lib/catalina.jar
            AddJar $TOMCAT_HOME/lib/catalina-ant.jar
            AddJar $TOMCAT_HOME/lib/el-api.jar
            AddJar $TOMCAT_HOME/lib/jasper.jar
            AddJar $TOMCAT_HOME/lib/jasper-el.jar
            AddJar $TOMCAT_HOME/lib/jasper-jdt.jar
            AddJar $TOMCAT_HOME/lib/jsp-api.jar
            AddJar $TOMCAT_HOME/lib/servlet-api.jar
            AddJar $TOMCAT_HOME/lib/tomcat-coyote.jar
            ;;

        weblogic*)
            AddJar $WL_HOME/server/lib/weblogic.jar
            ;;
        websphere6)
            AddJar $WAS_HOME/plugins/com.ibm.ws.runtime_6.1.0.jar
            AddJar $WAS_HOME/plugins/com.ibm.ws.webcontainer_2.0.0.jar
            AddJar $WAS_HOME/lib/bootstrap.jar
            AddJar $WAS_HOME/plugins/com.ibm.ws.emf_2.1.0.jar
            AddJar $WAS_HOME/lib/j2ee.jar
            ;;
        *)
            ${ECHO} "Error!: CCD_APPSVR set to \"$CCD_APPSVR\". Unknown value"
            ;;
    esac

    #java activation framework
    AddJar $JARDIR/activation_ibm_1.0.2.jar

    # Apache Axis (SOAP)
    AddJar $JARDIR/axis_1.4.jar
    # WPC Web Services Providers for Apache Axis.
    AddJar $JARDIR/ccd-axis_1.1.jar
    AddJar $JARDIR/ccd-axis.jar
    AddJar $JARDIR/axis_1.4_jaxrpc.jar
    AddJar $JARDIR/axis_1.4_saaj.jar
    AddJar $JARDIR/axis_1.4_wsdl4j.jar
    AddJar $JARDIR/axis_1.4_axis-ant.jar
    AddJar $JARDIR/axis_1.4_commons-discovery.jar
    AddJar $JARDIR/axis_1.4_commons-logging.jar


    # regexp
    AddJar $JARDIR/jakarta-regexp-1.2.jar

    # connection pool
    AddJar $JARDIR/commons-dbcp-1.1.jar
    AddJar $JARDIR/commons-pool-1.1.jar
    AddJar $JARDIR/commons-collections-3.0.jar
    
    
    # common configuration  (5.4 Glendale)
    AddJar $JARDIR/commons-configuration-1.4.jar
    AddJar $JARDIR/commons-lang-2.3.jar
    
    # common fileupload   (5.4 Glendale)
    
    AddJar $JARDIR/commons-io-1.3.2.jar
    AddJar $JARDIR/commons-fileupload-1.2.jar
 

    # Cup
    AddJar $JARDIR/Cup.jar

    # FTP
    AddJar $JARDIR/FTPProtocol_2.01q.jar
    AddJar $JARDIR/FTPUI_2.01q.jar

    # JUNIT (only for development)

    # JMS and WebSphere MQ files
    AddJar $JARDIR/com.ibm.mq.jar
    AddJar $JARDIR/com.ibm.mqjms.jar
    AddJar $JARDIR/dhbcore.jar

    # APACHE POI (and HSSF for Excel Integration)
    AddJar $JARDIR/poi-3.2-FINAL.jar

    # JSON4J.jar for use in the New OOTB UI
    AddJar $JARDIR/JSON4J.jar


    # profiler info 
    #if [ ! -z "$CCD_PROFILER_CLASSPATH" ]; then 
    #    AddJar $CCD_PROFILER_CLASSPATH
    #fi


    export JAVA_HOME=${JDK_HOME}

    export CCD_SYSTEM_CLASSPATH=${CCD_CLASSPATH}

    #setting profiler classpath
     export CCD_PROFILER_CLASSPATH=${TOP}/profiler/yourkit/yjp-controller-api-redist.jar${PATH_SEPARATOR}${TOP}/profiler/jprofiler/agent.jar

    export CLASSPATH=${CCD_SYSTEM_CLASSPATH}${PATH_SEPARATOR}${CCD_USER_CLASSPATH}
    export CCD_CLASSPATH=${CCD_SYSTEM_CLASSPATH}${PATH_SEPARATOR}${CCD_USER_CLASSPATH}

    if [ ! -z "$CCD_PROFILER_CLASSPATH" ]; then 
        export CLASSPATH=${CLASSPATH}${PATH_SEPARATOR}${CCD_PROFILER_CLASSPATH}
        export CCD_CLASSPATH=${CCD_CLASSPATH}${PATH_SEPARATOR}${CCD_PROFILER_CLASSPATH}
    fi

    # option to start jvm in client or server mode
    # use: -client for client (default)
    #      -server for server
    export CLIENT_SERVER_JAVA_OPTION=

    export POLICY_FILE=${CCD_ETC_DIR}/default/java.policy
    export BASIC_JAVA_OPTIONS="-DTOP=${TOP} -DCCD_ETC_DIR=${CCD_ETC_DIR} -Dfile.encoding=ISO8859_1"
    export COMMON_JAVA_OPTIONS="${CLIENT_SERVER_JAVA_OPTION} ${CUSTOM_JAVA_RT_OPTIONS} ${BASIC_JAVA_OPTIONS} -classpath "${CCD_CLASSPATH}" -Djava.security.policy=${POLICY_FILE} -Dtrigo_url=${CCD_URL} -Dvbr.home=${VBR_HOME}"

    export JAVA_RT="${JDK_HOME}/bin/java ${COMMON_JAVA_OPTIONS}"
    # JAVA_RT_HOTSPOT is now defined in $TOP/bin/go/init_svc_vars.sh

    export JAVA_RT_NOOUT="${JAVA_RT} -Druntime_log=${TOP}/logs/default/runtime.log"

    export BASIC_JAVA_OPTIONS_CMD="-DTOP=${TOP} -DCCD_ETC_DIR=${CCD_ETC_DIR} "
    export COMMON_JAVA_OPTIONS_CMD="${CLIENT_SERVER_JAVA_OPTION} ${BASIC_JAVA_OPTIONS_CMD} -classpath ${CCD_CLASSPATH} -Djava.security.policy=${POLICY_FILE} -Dtrigo_url=${CCD_URL}"
    export JAVA_RT_CMD="${JDK_HOME}/bin/java ${COMMON_JAVA_OPTIONS_CMD}"
    export RMIREGISTRY_CMD="${JDK_HOME}/bin/rmiregistry"

    export HOTSWAP_DIR=classes/hotswap
}


#--------------------------------------------------------------------------------
# main
#--------------------------------------------------------------------------------

CCDVars
OSInit
DatabaseSetup
AppSvrCheck
AppVars
# ---> Added to deal with user jar files
InitUserClasspath
# <--- Added to deal with user jar files
InitClasspath

unset AddJar
unset AppSvrCheck
unset AppVars
unset DatabaseSetup
unset InitClasspath
# ---> Added to deal with user jar files
unset InitUserClasspath
# <--- Added to deal with user jar files
unset OSInit
unset CCDVars
# ---> Added to deal with user jar files
unset AddUserJar
# <--- Added to deal with user jar files
unset VerifyFile
unset VerifyVar
