#! /bin/sh
# 
# # 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:
# 1) environment variable TOP pointing to the top of the trigo tree
#	   export TOP=/home/username/trigo
# 2) environment variable JDK_HOME, pointing to where the base java directory is.
#	   export JDK_HOME=/usr/local/java1.2
# 3) environment variable TOMCAT_HOME pointing to where the base jakarta-tomcat directory is
#	   export TOMCAT_HOME=/home/username/jakarta-tomcat
# 4) this looks for a java compiler in $JAVAC, or jikes at $JDK_HOME/bin/jikes or uses javac
# 5) your jars should be in JARDIR
#
# Features:
# 1) sets up JAVA_HOME for use by jakarta-tomcat's tomcat.sh.
# 2) sets up JAVAC for use by java.mk  Override it if you wish, or if jikes is elsewhere.
# 3) sets up APPSVR_OPTS for you
# 4) 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.
#
#
# Uses of pa.rc :
#   1) pa.rc is executed in the users environment to set up the environment for a build
#   2) The environment setup by pa.rc is used as the runtime environment for tomcat.
#   3) pa.rc is scanned by install.sh and ccd.rc is generate from it for enterprise deploys
#   4) pa.rc is scanned by install.sh to look for the system jar files that need to be copied into
#      the install.       
#
#--------------------------------------------------------------------------------



#--------------------------------------------------------------------------------
# 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
    # check for blank lines 
    if [ ! -z "$JARNAME" ]; then                                                      
    # Check for the existance of the user jar files.                                
      if [ ! -r "$JARNAME" ]; then                                                    
        ${ECHO} "Error!: User jar $JARNAME not found."                                
      elif [ -d "$JARNAME" ]; then                                                    
        ${ECHO} "Error!: directory $JARNAME supplied."                                
      elif [ -z "$CCD_USER_CLASSPATH" ]; then                                       
        NUMBER_OF_VALID_USER_JAR_FILES=`expr $NUMBER_OF_VALID_USER_JAR_FILES + 1`     
        CCD_USER_CLASSPATH="$JARNAME"                                                 
      else                                                                            
        NUMBER_OF_VALID_USER_JAR_FILES=`expr $NUMBER_OF_VALID_USER_JAR_FILES + 1`     
        CCD_USER_CLASSPATH="$CCD_USER_CLASSPATH$PATH_SEPARATOR$JARNAME"               
      fi                                                                              
    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
}

#--------------------------------------------------------------------------------
# trigo specific variables
#--------------------------------------------------------------------------------

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


    # use trigo_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

    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
    JUNIT_CLASSDIR=$TOP/junit_classes; export JUNIT_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.trigo.com -> baby)
    SHORT_HOSTNAME=`hostname|${SED} -e 's/^\([^.]*\)\..*$/\1/'|${TR} a-z A-Z`; export SHORT_HOSTNAME

    . $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)
            if [ "$PLATFORM" = "WINDOWS" ]; then
                CCD_SQLPLUS="db2cmd.exe -w -i DB2 -t"; export CCD_SQLPLUS
            else
                CCD_SQLPLUS="db2 -t +c "; export CCD_SQLPLUS
            fi
            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" "tomcat"

    case "${CCD_APPSVR}" in
        tomcat33)
            VerifyVar "TOMCAT_HOME" "$TOMCAT_HOME"
            ;;
        weblogic700)
            case "${PLATFORM}" in
                WINDOWS)
                ;;
                *)
                    export WL_HOME_UNIX_STYLE="$WL_HOME"
                ;;
            esac

            VerifyVar "WL_HOME" "$WL_HOME"
            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_PW" "$CCD_WL_PW"
            VerifyFile "$CCD_WL_DOMAIN_DIR/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/DefaultAuthenticatorInit.ldift" "Please copy it from your weblogic installation directory"
            ;;
        websphere)
            VerifyVar "WAS_HOME" "$WAS_HOME"
            ;;

        websphere5)
            VerifyVar "WAS_HOME" "$WAS_HOME"
            VerifyVar "WAS_NODENAME" "$WAS_NODENAME"
            VerifyVar "WAS_APPSERVERNAME" "$WAS_APPSERVERNAME" "TrigoProductCenter"
            ;;
        websphereAE)
            VerifyVar "WAS_HOME" "$WAS_HOME"
            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` = "CYGWIN_NT-5.0" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=";"
        export PLATFORM=CYGWIN
        ${ECHO} "  Setting up for cygwin"
    elif [ `uname` = "CYGWIN_NT-5.1" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=";"
        export PLATFORM=CYGWIN
        ${ECHO} "  Setting up for cygwin"
    elif [ `uname` = "Darwin" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=":"
        export PLATFORM=MACOSX
        ${ECHO} "  Setting up for MacOSX"
    else
        export BATCH_SUFFIX="bat"
        export PATH_SEPARATOR=";"
        export EXTRA_JAVAC_FLAGS=
        export PLATFORM=WINDOWS
        ${ECHO} "  Setting up for windows"
    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

    export CCD_CLASSPATH=$CCD_CLASSPATH$PATH_SEPARATOR$JAVAAPI_CLASSDIR
    export CCD_CLASSPATH=$CCD_CLASSPATH$PATH_SEPARATOR$JUNIT_CLASSDIR

    # JVM for jikes - This must be added before J2EE

    # JAVA JVM
    AddJar $JDK_HOME/jre/lib/rt.jar

    # IBM JVM 1.4.0 and onward has splitted rt.jar into multiple jar
    # AddJar $JDK_HOME/jre/lib/core.jar
    # AddJar $JDK_HOME/jre/lib/server.jar
    # AddJar $JDK_HOME/jre/lib/xml.jar
    # AddJar $JDK_HOME/jre/lib/graphics.jar

    AddJar $JDK_HOME/lib/tools.jar

    #xerces (this has to come before weblogic.jar)
    AddJar $JARDIR/xercesImpl_2.4.0.jar
    AddJar $JARDIR/xerces-xml-apis_2.4.0.jar

    # 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
    AddJar $JARDIR/xalan_2.4.1.jar

    #javamail
    AddJar $JARDIR/mail_ibm_1.3.jar



    # servlet + app server classes
    case "${CCD_APPSVR}" in
        tomcat33)
            AddJar $TOMCAT_HOME/lib/tomcat.jar
            AddJar $TOMCAT_HOME/lib/container/jasper.jar
            AddJar $TOMCAT_HOME/lib/common/tomcat_core.jar
            AddJar $TOMCAT_HOME/lib/common/jasper-runtime.jar
            AddJar $TOMCAT_HOME/lib/common/etomcat.jar
            AddJar $TOMCAT_HOME/lib/common/servlet.jar
            AddJar $TOMCAT_HOME/lib/common/connector_util.jar
            AddJar $TOMCAT_HOME/lib/common/core_util.jar
            AddJar $J2EE_JAR
            ;;
        weblogic700)
            AddJar $WL_HOME/server/lib/weblogic.jar
            ;;
        websphere|websphereAE)
            AddJar $WAS_HOME/lib/jsp.jar
            AddJar $WAS_HOME/lib/j2ee.jar
            AddJar $WAS_HOME/lib/websphere.jar
            AddJar $JARDIR/connector.jar
            ;;
        websphere5)
            AddJar $WAS_HOME/lib/webcontainer.jar
            AddJar $WAS_HOME/lib/j2ee.jar
            AddJar $WAS_HOME/lib/httpsession.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.1.jar
    AddJar $JARDIR/ccd-axis_1.1.jar
    AddJar $JARDIR/ccd-axis.jar
    AddJar $JARDIR/axis_1.1_jaxrpc.jar
    AddJar $JARDIR/axis_1.1_saaj.jar
    AddJar $JARDIR/axis_1.1_wsdl4j.jar
    AddJar $JARDIR/axis_1.1_axis-ant.jar
    AddJar $JARDIR/axis_1.1_commons-discovery.jar
    AddJar $JARDIR/axis_1.1_commons-logging.jar

    # Logging
    AddJar $JARDIR/log4j.jar

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

    if [ "$CCD_DB" = "oracle" ]; then
    	# oracle
        AddJar $ORACLE_JDBC_DRIVER
    elif [ "$CCD_DB" = "db2" ]; then
        # DB2
        AddJar $DB2_HOME/java/db2java.zip
    else
        ${ECHO} "Error! CCD_DB has not been set correctly! Build Processes will fail"
    fi

    # connection pool
    AddJar $JARDIR/commons-dbcp-1.1.jar
    AddJar $JARDIR/commons-pool-1.1.jar
    AddJar $JARDIR/commons-collections-3.0.jar

    # Cup
    AddJar $JARDIR/Cup.jar

    # FTP
    AddJar $JARDIR/FTPProtocol_2.01p.jar

    # JUNIT (only for development)

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


    # Clover
    if [ -n "$CLOVER_JAR" ]; then
        AddJar $CLOVER_JAR
    fi

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

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

    # 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} ${BASIC_JAVA_OPTIONS} -classpath "${CCD_CLASSPATH}" -Djava.security.policy=${POLICY_FILE} -Dtrigo_url=${CCD_URL}"

    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 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
