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

. ${TOP}/bin/get_params.sh
SVC_NAME=${_CCD_SVC_NAME:=appsvr_${SHORT_HOSTNAME}}
. ${ADMIN_DIR}/init_svc_vars.sh

WL_APPSERVERNAME=${_CCD_APPSERVERNAME:=${CCD_WL_SERVER}}
WAS_APPSERVERNAME=${_CCD_APPSERVERNAME:=${WAS_APPSERVERNAME}}

if [ ! "${_CCD_STOP}" = "no" ]; then
    ${ADMIN_DIR}/abort/abort_appsvr.sh --svc_name=${SVC_NAME}
fi

CheckDbSchemaVersion
if [ "$?" -eq "1" ]
then
	exit 1
fi

${ECHO} "starting service '${SVC_NAME}'"
cd $TOP

# The following script checks if trace is enabled and sets the tracing aspect jar in classpath
. ${TOP}/bin/check_trace.sh appsvr


# now start the right java process
case "${CCD_APPSVR}" in
    tomcat60)
        if [ ! \( -r $TOP/public_html/WEB-INF/web.xml -o -r $TOP/etc/default/tomcat60/webapps/ROOT.war \) ]; then
        	${ECHO} "No web.xml in ${TOP}/public_html/WEB-INF.  Please run $TOP/bin/gen_web_xml.pl"
        	${ECHO} "  Exiting."
        	exit
        fi
        
        InitEtcDirForAppSvr tomcat60
        # the 'cd' is only for a the dtds...
        # no need to be service specific

        cd ${SVC_ETC_DIR}/tomcat60
        ${RM} ${TOP}/logs/${SVC_NAME}/tomcat/work/*

		# make temp dir
		${MKDIR} ${TOP}/logs/${SVC_NAME}/tomcat/temp
		
		# Set juli LogManager if it is present
		if [ -r "${SVC_ETC_DIR}"/tomcat60/logging.properties ]; then
  			APPSVR_OPTS="$APPSVR_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Djava.util.logging.config.file="${SVC_ETC_DIR}/tomcat60/logging.properties"
		fi
		
		${ECHO} "Tomcat is using:"
		${ECHO} "    Config: ${SVC_ETC_DIR}/tomcat60/server.xml"
		${ECHO} "    Logging config: ${SVC_ETC_DIR}/tomcat60/logging.properties"
		${ECHO} "    HTTP port: ${_CCD_CONF_APPSVR_PORT}"
		${ECHO} "    AJP13 port: ${_CCD_CONF_TOMCAT_AJP13}"
		${ECHO} "    Shutdown port: ${_CCD_CONF_TOMCAT_SHUTDOWN}"
		${ECHO} "    temp dir: ${TOP}/logs/${SVC_NAME}/tomcat/temp"
		
        EXTRA_APPSVR_VARS="-DCCD_HOSTNAME=${SHORT_HOSTNAME} -DCCD_APPSVR_PORT=${_CCD_CONF_APPSVR_PORT} -DCCD_TOMCAT_AJP13=${_CCD_CONF_TOMCAT_AJP13}"
        APPSVR_OPTS="${APPSVR_OPTS} ${EXTRA_APPSVR_VARS}"

        startWithLogs ${JDK_HOME}/bin/java \
            ${APPSVR_OPTS} \
            ${CCD_APPSVR_JVM_DEBUG_OPTS}\
            ${CCD_APPSVR_JVM_PROFILING_AGENT} \
            -classpath ${CCD_CLASSPATH} \
            -Dcatalina.base=${TOMCAT_HOME} \
            -Dcatalina.home=${TOMCAT_HOME} \
            -Djava.io.tmpdir=${TOP}/logs/${SVC_NAME}/tomcat/temp \
            ${CCD_APPSVR_JVM_PROFILING_OPTS}\
	    -Dprofiler_opts=${CCD_APPSVR_PROFILER_NAME}__${CCD_APPSVR_PROFILER_PORT} \
            org.apache.catalina.startup.Bootstrap \
            -config ${SVC_ETC_DIR}/tomcat60/server.xml start
         ;;

    weblogic92)
        InitEtcDirForAppSvr weblogic92
        cd ${WL_HOME}/server/bin
        export CLASSPATH=
        # Call commEnv here AFTER setting the java_vendor to get common environmental settings.
        . ${WL_HOME}/common/bin/commEnv.sh

        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}"
        cd ${CCD_WL_DOMAIN_DIR}

        STARTMODE=true
        BEA_HOME=`cd ${WL_HOME}/..; pwd`
        APPSVR_OPTS="${APPSVR_OPTS} -Dccd_wl_pw=${CCD_WL_PW}"

        ADMIN_URL=http://localhost:${WL_ADMIN_PORT}

        # Start WebLogic server
        echo CLASSPATH="${CLASSPATH}"
        echo
        echo PATH="${PATH}"
        echo

        startWithLogs ${JDK_HOME}/bin/java \
            ${APPSVR_OPTS} \
            ${CCD_APPSVR_JVM_DEBUG_OPTS} \
            ${CCD_APPSVR_JVM_PROFILING_AGENT} \
            -classpath ${CLASSPATH} \
            -Dweblogic.Name=${WL_APPSERVERNAME} \
            -Dplatform.home=${WL_HOME} \
            -Dwls.home=${WL_HOME}/server \
            -Dwli.home=${WL_HOME}/integration \
            -Dweblogic.management.discover=false \
            -Dweblogic.management.server=${ADMIN_URL} \
            -Dweblogic.management.username=${CCD_WL_USER} \
            -Dweblogic.management.password=${CCD_WL_PW} \
            -Dweblogic.ProductionModeEnabled=${STARTMODE} \
            -Djava.security.policy==${WL_HOME}/server/lib/weblogic.policy \
	    -Dprofiler_opts=${CCD_APPSVR_PROFILER_NAME}__${CCD_APPSVR_PROFILER_PORT} \
            weblogic.Server
        ;;
        
    websphere6)
        InitEtcDirForAppSvr websphere
        startWithLogs ${WAS_PROFILE_HOME}/bin/startServer.sh ${WAS_APPSERVERNAME}
        # startwithLogs puts PID of startServer.sh in svc.pid, not websphere PID, so
		# we need to wait for WAS to get its PID
		# PID and LOG are defined in init_svc_vars.sh
		${ECHO} -n "     Waiting for WebSphere to start"
		iter=0
		while [ "$iter" -le "15" ]
		do
			${ECHO} -n "."
			let "iter++"
			sleep 2
			if [ ! -e ${LOG} ]
			then
				continue
			fi
			c=`${GREP} -c "process id is" ${LOG}`
			if [ "$c" -eq "1" ]
			then
       			pid=`${AWK} '/^ADMU3000I:.*process id is/{print $NF}' ${LOG}`
        		if [ ! -z "$pid" ]
        		then
        			${ECHO} $pid > ${PID}
        		fi
				break
			fi
		done
		${ECHO}
        ;;
        
    *)
        ${ECHO} "$0: Error!: CCD_APPSVR set to \"$CCD_APPSVR\". Unknown value"
        ;;
esac
