#! /bin/sh
# 
# DISCLAIMER
#   Trigo Technologies, Inc. disclaims all warranties or representations herein,
#   whether express, implied or statutory, to the fullest extent
#   permissible by law, including the implied warranties of title,
#   fitness for a particular purpose and merchantability.
# 
# COPYRIGHTS
#   Copyright 2001, 2002 Trigo Technologies, Inc. All rights reserved.
# 
#   Trigo Technologies, Inc.
#   8000 Marina Blvd, Suite 810
#   Brisbane, CA 94005
#   U.S.A.
#   www.trigo.com
# 
# TRADEMARKS
#   Trigo, Trigo Enterprise, Trigo Content Suite, Trigo Commerce Suite,
#   and Trigo Platform are trademarks of Trigo Technologies, Inc.
#   All other company and product names are trademarks or registered
#   trademarks of their respective owners.
#


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



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

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

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

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

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


    # use trigo_etc_dir=$TOP/etc
    TRIGO_ETC_DIR=$TOP/etc; export TRIGO_ETC_DIR

    if [ -z "${TRIGO_ETC_DIR}" ]; then
        TRIGO_ETC_DIR=$TOP/etc; export TRIGO_ETC_DIR
    fi

    VerifyVar "TRIGO_ETC_DIR" "$TRIGO_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
    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 ${TRIGO_ETC_DIR}/default/austin.properties
}


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

DatabaseSetup() {

    VerifyVar "TRIGO_DB" "$TRIGO_DB" "oracle"

# * Set PATH for Database Specific SQL Command Line Processor
# * Set TRIGO_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 ${TRIGO_DB} in
        oracle)
            TRIGO_SQLPLUS="sqlplus -s"; export TRIGO_SQLPLUS
            TRIGO_SQLPLUS_LOGIN="sqlplus"; export TRIGO_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
                TRIGO_SQLPLUS="db2cmd.exe -w -i DB2 -t"; export TRIGO_SQLPLUS
            elif [ "$PLATFORM" = "CYGWIN" ]; then
            	TRIGO_SQLPLUS="db2cmd.exe -w -i DB2 -t"; export TRIGO_SQLPLUS
            else
                TRIGO_SQLPLUS="db2 -t +c "; export TRIGO_SQLPLUS
            fi
            TRIGO_SQLPLUS_LOGIN=$TRIGO_SQLPLUS; export TRIGO_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! TRIGO_DB has not been set! Build Processes will fail"
    esac

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

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


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

AppSvrCheck() {
    VerifyVar "TRIGO_APPSVR" "$TRIGO_APPSVR" "tomcat"

    case "${TRIGO_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 "TRIGO_WL_DOMAIN_DIR" "$TRIGO_WL_DOMAIN_DIR"
            VerifyVar "TRIGO_WL_SERVER" "$TRIGO_WL_SERVER"
            VerifyVar "TRIGO_WL_PW" "$TRIGO_WL_PW"
            VerifyFile "$TRIGO_WL_DOMAIN_DIR/SerializedSystemIni.dat" "Please copy it from your weblogic installation directory"
            VerifyFile "$TRIGO_WL_DOMAIN_DIR/fileRealm.properties" "Please copy it from your weblogic installation directory"
            VerifyFile "$TRIGO_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!: TRIGO_APPSVR set to \"$TRIGO_APPSVR\". Unknown value"
            ;;
    esac

    ${ECHO} "  Setting up for $TRIGO_APPSVR"

}

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

AppVars() {

    #soap
    export TRIGO_SOAP_URL="http://${HOSTNAME}:${_TRIGO_CONF_APPSVR_PORT}/soap/servlet/rpcrouter"
}
















#--------------------------------------------------------------------------------
# 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.1" ]; then
        export BATCH_SUFFIX="sh"
        export PATH_SEPARATOR=";"
        export PLATFORM=CYGWIN
        ${ECHO} "  Setting up for cygwin"
    else
        export BATCH_SUFFIX="bat"
        export PATH_SEPARATOR=";"
        export EXTRA_JAVAC_FLAGS=
        export PLATFORM=WINDOWS
        ${ECHO} "  Setting up for windows"
    fi
}


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

InitClasspath() {
    export TRIGO_CLASSPATH=

    AddJar $TOP/jars/trigo_svr.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_1.3.jar



    # servlet + app server classes
    case "${TRIGO_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!: TRIGO_APPSVR set to \"$TRIGO_APPSVR\". Unknown value"
            ;;
    esac

    # JVM for jikes
    AddJar $JDK_HOME/jre/lib/rt.jar
    AddJar $JDK_HOME/lib/tools.jar
    #java activation frameword
    AddJar $JARDIR/activation_1.0.2.jar
    # Apache Axis (SOAP)
    AddJar $JARDIR/axis_1.1.jar
    AddJar $JARDIR/trigo-axis_1.1.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 [ "$TRIGO_DB" = "oracle" ]; then
    	# oracle
        AddJar $JARDIR/classes12.jar
    elif [ "$TRIGO_DB" = "db2" ]; then
        # DB2
        AddJar $DB2_HOME/java/db2java.zip
        AddJar $DB2_HOME/java/db2jcc.jar
    else
        ${ECHO} "Error! TRIGO_DB has not been set correctly! Build Processes will fail"
    fi

    # connection pool
    AddJar $JARDIR/connPool.jar

    # JFLex & Cup
    AddJar $JARDIR/Cup.jar
    AddJar $JARDIR/JFlex.jar
    # XPC
    AddJar $JARDIR/xpc.jar
    AddJar $JARDIR/ccs_xdk.jar
    AddJar $JARDIR/ccs_util.jar
    AddJar $JARDIR/bussdocs.jar
    # XML Connect
    AddJar $JARDIR/EbxJNIClient.jar
    # FTP
    AddJar $JARDIR/FTPProtocol_2.01p.jar
    # Java Secure Sockets Extension
    AddJar $JARDIR/jnet.jar
    AddJar $JARDIR/jcert.jar
    # Java Tar
    AddJar $JARDIR/tar.jar
    # Excel reader
    AddJar $JARDIR/xlrd.jar

    # junit
    AddJar $JARDIR/junit-3.8.1.jar

    # spell checker
    AddJar $JARDIR/ssce.jar

    AddJar $JARDIR/utapi.jar
    AddJar $JARDIR/xml4j.jar

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

    AddJar $JARDIR/trove.jar

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

    export JAVA_HOME=${JDK_HOME}

    export CLASSPATH=${TRIGO_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=${TRIGO_ETC_DIR}/default/java.policy
    export BASIC_JAVA_OPTIONS="-DTOP=${TOP} -DTRIGO_ETC_DIR=${TRIGO_ETC_DIR} -Dfile.encoding=ISO8859_1"
    export COMMON_JAVA_OPTIONS="${CLIENT_SERVER_JAVA_OPTION} ${BASIC_JAVA_OPTIONS} -classpath ${CLASSPATH} -Djava.security.policy=${POLICY_FILE} "

    export JAVA_RT="${JDK_HOME}/bin/java ${COMMON_JAVA_OPTIONS}"

    # used in scripts
    export JAVA_RT_NOOUT="${JAVA_RT} -Druntime_log=${TOP}/logs/default/runtime.log"
    export JAVA_RT_HOTSPOT="${JDK_HOME}/bin/java ${COMMON_JAVA_OPTIONS}"
    export RMIREGISTRY_CMD="${RMIREGISTRYEXEC_PATH}/rmiregistry"

    export HOTSWAP_DIR=classes/hotswap
}

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

TrigoVars
OSInit
DatabaseSetup
AppSvrCheck
AppVars
InitClasspath

unset AddJar
unset AppSvrCheck
unset AppVars
unset DatabaseSetup
unset InitClasspath
unset OSInit
unset TrigoVars
unset VerifyFile
unset VerifyVar
