#!/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. 
  
if [ -d ${TOP} ]; then
    . ${TOP}/setup/init_ccd_vars.sh
else
    ${ECHO} "\$TOP not defined!  Cannot continue."
    exit 99
fi

${ECHO}
${ECHO}
${ECHO} "Installing ccd  WAR file on Application Server: ${WAS_APPSERVERNAME} on node ${WAS_NODENAME}"
${ECHO}
${ECHO} "Warning!  Running this script will uninstall any previously"
${ECHO} "installed Enterprise Appplication called ccd_${WAS_APPSERVERNAME}!"
${ECHO}
${ECHO} "Are you sure you want to do this? [y/N] "
read ret
case $ret in
	[Yy]*)
	    #continue
	;;
	*)
        ${ECHO} Canceled!
        exit 1
	;;
esac

case ${CCD_DB} in
    db2)
    DB_JAR=${DB2_HOME}/java12/db2java.zip
    ;;
    oracle)
    DB_JAR=${ORACLE_JDBC_DRIVER}
    ;;
esac

WAS_CPATH=$DB_JAR

for i in `echo $CCD_CLASSPATH | sed -e 's/:/ /g'`; do
    if [ `dirname $i` = "${TOP}/jars" ]; then
        WAS_CPATH="$WAS_CPATH;$i"
    fi
done

${ECHO} "*** Installing ccd.war file..."
MAXMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($1,"[0-9]+") { print substr($1, RSTART, RLENGTH); exit}'`
MINMEM=`echo $APPSVR_MEMORY_FLAG | ${AWK} -F " " 'match($2,"[0-9]+") { print substr($2, RSTART, RLENGTH); exit}'`
SVC_NAME=${_CCD_SVC_NAME=appsvr_${SHORT_HOSTNAME}}

case ${CCD_APPSVR} in
    websphereAE)
    ${WAS_HOME}/bin/wscp.sh -p ${CCD_ETC_DIR}/default/websphere/wscp.properties  -f ${TOP}/bin/websphere/install_war.tcl ${WAS_NODENAME} ${WAS_APPSERVERNAME} ${TOP} ${WAS_VHOST} ${CCD_ETC_DIR} ${MINMEM} ${MAXMEM} ${DB_JAR} ${SVC_NAME} ${_CCD_CONF_APPSVR_PORT} ${USER}
    ${ECHO} "Creating suppliers symlink..."
    ln -s ${TOP}/public_html/suppliers ${WAS_HOME}/installedApps/ccd_${WAS_APPSERVERNAME}.ear/ccd.war/
    ;;
    websphere5)
    ${WAS_HOME}/bin/wsadmin.sh -f ${TOP}/bin/websphere/install_war.jacl ${WAS_NODENAME} ${WAS_APPSERVERNAME} ${TOP}/etc/default/websphere/webapps/ccd.war ccd_${WAS_APPSERVERNAME} ${WAS_VHOST} ${TOP} ${CCD_ETC_DIR} ${MINMEM} ${MAXMEM} ${WAS_CPATH} ${SVC_NAME} ${_CCD_CONF_APPSVR_PORT} ${USER}
    ${ECHO} "Creating suppliers symlink..."
    ln -s ${TOP}/public_html/suppliers ${WAS_HOME}/installedApps/${WAS_NODENAME}/ccd_${WAS_APPSERVERNAME}.ear/ccd.war/
    ;;
    websphere)
        if [ -e ${CCD_ETC_DIR}/default/websphere/server-cfg.xml.default ]; then
            cat ${CCD_ETC_DIR}/default/websphere/server-cfg.xml.default | ${SED} \
                -e "s#%%NODENAME%%#${HOSTNAME}#g" \
                -e "s#%%WAS_ROOT%%#${WAS_HOME}#g" \
                -e "s#%%JDK_HOME%%#${WAS_HOME}/java#g" \
                -e "s#%%CCD_URL%%#${CCD_URL}#g" \
                -e "s#%%TOP%%#${TOP}#g" \
                -e "s#%%CCD_ETC_DIR%%#${CCD_ETC_DIR}#g" \
                -e "s#%%WAS_CPATH%%#${WAS_CPATH}#g" \
                -e "s#%%USER%%#${USER}#g" \
                -e "s#%%HTTP_PORT%%#${_CCD_CONF_APPSVR_PORT}#g" \
                -e "s#%%DIAG_PORT%%#${_CCD_CONF_TOMCAT_AJP12}#g" \
                -e "s#%%BOOTSTRAP_PORT%%#${_CCD_CONF_TOMCAT_AJP13}#g" \
                -e "s#%%svc_name%%#appsvr_${SHORT_HOSTNAME}#g" \
                > ${CCD_ETC_DIR}/default/websphere/server-cfg.xml
        else
            ${ECHO} "Could not find server-cfg.xml.default in ${CCD_ETC_DIR}/default/websphere"
        fi
        
        if [ -e ${CCD_ETC_DIR}/default/websphere/webapps/ccd.war ]; then
            ${WAS_HOME}/bin/SEAppInstall.sh -install ${CCD_ETC_DIR}/default/websphere/webapps/ccd.war -configFile ${CCD_ETC_DIR}/default/websphere/server-cfg.xml -contextroot "/" -interactive false -precompileJsp false -displayname trigo
        else
            ${ECHO} "Could not find ccd.war. (TOP == \"$TOP\")"
        fi

        ${ECHO} "Creating suppliers symlink..."
        ln -s ${TOP}/public_html/suppliers ${CCD_ETC_DIR}/default/websphere/installedApps/ccd.ear/ccd.war/

    ;;
    *)
        ${ECHO} "Error!: CCD_APPSVR set to \"$CCD_APPSVR\". Unknown value"
    ;;
esac

${ECHO} "*** Done!"
