#!/bin/sh

if [ "$#" -ne 1 ]; then
    echo "ERROR: $0 requires parameter"
    exit 1
fi

. /etc/tomcat/tomcat.conf
NAME=$1
if [ -f /etc/sysconfig/$NAME ]; then
    . /etc/sysconfig/$NAME
else
    echo "ERROR: Unable to find /etc/sysconfig/$NAME file"
    exit 1
fi
. /usr/libexec/tomcat/preamble

NUXWDOG_PID=${CATALINA_BASE}/logs/wd-$NAME.pid
CATALINA_PID=${CATALINA_BASE}/logs/$NAME.pid
MAIN_CLASS=org.apache.catalina.startup.Bootstrap

FLAGS="$JAVA_OPTS"
OPTIONS="-Dcatalina.base=$CATALINA_BASE \
-Dcatalina.home=$CATALINA_HOME \
-Djava.endorsed.dirs=$JAVA_ENDORSED_DIRS \
-Djava.io.tmpdir=$CATALINA_TMPDIR \
-Djava.util.logging.config.file=${LOGGING_PROPERTIES} \
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

FLAGS="${FLAGS} $CATALINA_OPTS"
if [ "${SECURITY_MANAGER}" = "true" ] ; then
    OPTIONS="${OPTIONS} \
    -Djava.security.manager \
    -Djava.security.policy==${CATALINA_BASE}/conf/catalina.policy"
fi

set_javacmd

# create the nuxwdog config file
nux_fname="${CATALINA_BASE}/conf/nuxwdog.conf"
touch ${nux_fname}
chown ${TOMCAT_USER}: ${nux_fname}

echo "ExeFile ${JAVA_HOME}/bin/java" > $nux_fname
echo "ExeArgs ${JAVA_HOME}/bin/java ${JAVACMD_OPTS} ${FLAGS} -classpath ${CLASSPATH} ${OPTIONS} ${MAIN_CLASS} start" >> $nux_fname
echo "TmpDir ${CATALINA_BASE}/logs/pids" >> $nux_fname
echo "ChildSecurity 1" >> $nux_fname
echo "ExeOut ${CATALINA_BASE}/logs/catalina.out" >> $nux_fname
echo "ExeErr ${CATALINA_BASE}/logs/catalina.out" >> $nux_fname
echo "ExeBackground 1" >> $nux_fname
echo "PidFile $NUXWDOG_PID" >> $nux_fname
echo "ChildPidFile $CATALINA_PID" >> $nux_fname
echo "User ${TOMCAT_USER}" >> $nux_fname

