#!/bin/bash
# Licensed Materials - Property of IBM
# 5724-I68
#
# (C) Copyright IBM Corp. 2000, 2006
#
# US Government Users Restricted Rights - Use, duplication, or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

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


${MKDIR} ${SVC_LOG_DIR}/${SVC_NAME}

PID=${SVC_LOG_DIR}/${SVC_NAME}/svc.pid
LOG=${SVC_LOG_DIR}/${SVC_NAME}/svc.out
JAVA_RT_HOTSPOT="${JDK_HOME}/bin/java ${COMMON_JAVA_OPTIONS}"

# read the right com.ibm.ccd.common.properties
if [ -e "${CCD_ETC_DIR}/${SVC_NAME}/common.properties" ]; then
    . $TOP/bin/read_config_file.sh ${CCD_ETC_DIR}/${SVC_NAME}/common.properties
else
    . $TOP/bin/read_config_file.sh ${CCD_ETC_DIR}/default/common.properties
fi


# start cmd and output stdout and stderr to log file
# also saves pid of cmd to pid file
startWithLogs() {

    #ensure ;'s are escaped before passing to bash, avoid already escaped ;'s
    CMD=`${ECHO} $@ | ${SED} -e 's#\([^\\]\)\;#\1\\\;#g'`
    
    TXT1="Starting trigo service: '${SVC_NAME}' at `date`"
    TXT2="Using command: $CMD"
    TXT3="---------------------------------------------"
    TXT4="Schema synchronization error. The database and codeline are not in step."
    TXT5="To avoid possible data corruption the application will not be started"    
    TXT6="You must update your Schema before proceeding"    
    
    ${JAVA_RT} com.ibm.ccd.synchronize.DBSchemaVersion --verify > $TOP/schemaValid.txt
    NOSYNCH=`${GREP} "False" $TOP/schemaValid.txt|${AWK} '{print $1}'`
    if [ ! -z "${_CCD_NOLOG}" ]; then
        ${ECHO} ${TXT1}
        ${ECHO}
        ${ECHO} ${TXT2}
        ${ECHO} ${TXT3}

        if [ $NOSYNCH ]; then
            ${ECHO} 
            ${ECHO} ${TXT4}
            ${ECHO} ${TXT5} 
            ${ECHO} 
            ${ECHO} ${TXT6}                         
            ${ECHO} 
            exit                            
        else
            bash -c "$CMD" &    
        fi
        ${ECHO} $! > $PID
        ${RM} $TOP/schemaValid.txt
    else
        ${ECHO} ${TXT1} > $LOG
        ${ECHO} >> $LOG
        ${ECHO} ${TXT2} >> $LOG
        ${ECHO} ${TXT3} >> $LOG
        if [ $NOSYNCH ]; then
            ${ECHO} 
            ${ECHO} ${TXT4}
            ${ECHO} ${TXT5} 
            ${ECHO} 
            ${ECHO} ${TXT6}                         
            ${ECHO}     
            exit                
        else
            bash -c "$CMD" >> $LOG 2>&1 &
        fi
        ${ECHO} $! > $PID
        ${RM} $TOP/schemaValid.txt
    fi
}

# FindEtcDirForAppSvr <appsvr type>
#   find if there is a ${CCD_ETC_DIR}/<svc_name>/<appsvr type> directory
#   if it does not exist, use the default directory
InitEtcDirForAppSvr() {
    if [ -d "${CCD_ETC_DIR}/${SVC_NAME}/$1" ]; then
        SVC_ETC_DIR=${CCD_ETC_DIR}/${SVC_NAME}
    else
        SVC_ETC_DIR=${CCD_ETC_DIR}/default
    fi
    
    APPSVR_OPTS="${CLIENT_SERVER_JAVA_OPTION} $APPSVR_MEMORY_FLAG $BASIC_JAVA_OPTIONS -Dtrigo.memflags='$APPSVR_MEMORY_FLAG' -Dsvc_name=${SVC_NAME} -Dsvc_etc_dir=${SVC_ETC_DIR} -Dtrigo_url=${CCD_URL}"
}
