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


# get params
. ${TOP}/bin/get_params.sh

cd $TOP

# stop everything first
if [ -n "${_CCD_APPSERVERNAMES}" ]; then
    ${ADMIN_DIR}/abort_local.sh --appservernames=${_CCD_APPSERVERNAMES}
elif [ -z "${_CCD_APPSERVERS}" ]; then
    ${ADMIN_DIR}/abort_local.sh
else
    ${ADMIN_DIR}/abort_local.sh --appservers=${_CCD_APPSERVERS}
fi

sleep 2

if [ "${_CCD_RM_LOGS}" = "yes" ]; then
    LOGS_DIR=${TOP}/logs
    ${ECHO} "removing logs directory: ${LOGS_DIR}"
    ${RM} -rf ${LOGS_DIR}
fi

# we use stop=no here because we already stopped everything
${ADMIN_DIR}/start/start_rmiregistry.sh --stop=no

sleep 2

if [ "${_CCD_DEBUG}" = "yes" ]; then
    . ${TOP}/bin/ccd_debugging.sh on
fi


${ADMIN_DIR}/start/start_admin.sh --stop=no
if [ -n "${_CCD_APPSERVERNAMES}" ]; then
    n=1
    while [ -n "`${ECHO} ${_CCD_APPSERVERNAMES}|cut -f $n -d ,`" ];
    do
        ${ADMIN_DIR}/start/start_appsvr.sh --stop=no --svc_name=appsvr${n}_${SHORT_HOSTNAME} --appservername=`${ECHO} ${_CCD_APPSERVERNAMES}|cut -f $n -d ,`
        let n=$n+1
    done
elif [ -z "${_CCD_APPSERVERS}" ]; then
    ${ADMIN_DIR}/start/start_appsvr.sh --stop=no
elif [ ${_CCD_APPSERVERS} -gt 1 ]; then
    case ${CCD_APPSVR} in
        weblogic92)
            APP_SERVER_PREFIX=${CCD_WL_SERVER}
            ;;
        websphere6)
            APP_SERVER_PREFIX=${WAS_APPSERVERNAME}
            ;;
    esac
    for ((n=1; n<=${_CCD_APPSERVERS}; n++))
    do
        ${ADMIN_DIR}/start/start_appsvr.sh --stop=no --svc_name=appsvr${n}_${SHORT_HOSTNAME} --appservername=${APP_SERVER_PREFIX}${n}
    done
else
    ${ADMIN_DIR}/start/start_appsvr.sh --stop=no
fi
${ADMIN_DIR}/start/start_eventprocessor.sh --stop=no
${ADMIN_DIR}/start/start_queuemanager.sh --stop=no
${ADMIN_DIR}/start/start_scheduler.sh --stop=no
${ADMIN_DIR}/start/start_workflowengine.sh --stop=no

# For WebLogic platforms the Web Services need to be redeployed before they can be used
# For WebSphere Application Server, if the user specifies that Web Services should be redeployed
# (e.g. after re-running install_war.sh), wait 10 seconds for the AppServer to fully start up
if [ "${CCD_APPSVR}" = "weblogic92" ]; then
    ${ECHO} Waiting for Application Server to come up
    sleep 5
    ${ECHO} Re-deploying web services
    $JAVA_RT com.ibm.ccd.connectivity.common.DeployWebServices
elif [ "${_CCD_REDEPLOY}" = "yes" ]; then
    ${ECHO} Waiting for Application Server to come up
    sleep 10
    ${ECHO} Re-deploying web services
    $JAVA_RT com.ibm.ccd.connectivity.common.DeployWebServices
fi

${ECHO} ""
${ECHO} "Verify that the application is up and running by executing the following script:"
${ECHO} "   $TOP/bin/go/rmi_status.sh"
${ECHO} ""
${ECHO} "Verify that the following services have started:"
${ECHO} "   admin_${SHORT_HOSTNAME}"
if [ -n "${_CCD_APPSERVERNAMES}" ]; then
    n=1
    while [ -n "`${ECHO} ${_CCD_APPSERVERNAMES}|cut -f $n -d ,`" ];
    do
       ${ECHO} "   appsvr${n}_${SHORT_HOSTNAME}"
       let n=$n+1
    done 
elif [ -z "${_CCD_APPSERVERS}" ]; then
    ${ECHO} "   appsvr_${SHORT_HOSTNAME}"
elif [ ${_CCD_APPSERVERS} -gt 1 ]; then
    for ((n=1; n<=${_CCD_APPSERVERS}; n++))
    do
        ${ECHO} "   appsvr${n}_${SHORT_HOSTNAME}"
    done
else
    ${ECHO} "   appsvr_${SHORT_HOSTNAME}"
fi
${ECHO} "   eventprocessor_${SHORT_HOSTNAME}"
${ECHO} "   queuemanager_${SHORT_HOSTNAME}"
${ECHO} "   scheduler_${SHORT_HOSTNAME}"
${ECHO} "   workflowengine_${SHORT_HOSTNAME}"
${ECHO} ""
${ECHO} "Log files for the running services can be located at:"
${ECHO} "   $TOP/logs/svc/"
${ECHO} ""
