#!/bin/sh
#
# Startup script for Apache Tomcat
#
# chkconfig: 2345 80 15
# description: Apache Tomcat Java Servlets and JSP server
# processname: Tomcat
# pidfile: /var/run/tomcat.pid
# config: /usr/local/tomcat/conf/server.xml

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

pidfile=/var/run/tomcat.pid

export JAVA_HOME=/usr/local/jdk1.5.0_17
export PATH=$JAVA_HOME/bin:$PATH
export TOMCAT_HOME=/usr/local/tomcat
export CATALINA_HOME=$TOMCAT_HOME

TOMCAT_SCRIPT=$CATALINA_HOME/bin/catalina.sh
TOMCAT_USER=tomcat

pidfile_make="ps -ef | grep -v grep | grep $TOMCAT_HOME | awk '{print \$2}' > $pidfile"
pidfile_del="rm -f $pidfile"

[ -f $TOMCAT_SCRIPT ] || exit 0

# See how we were called.
case "$1" in
    start)
        # Start daemons.
        if [ -f /var/lock/subsys/tomcat ] ; then
            echo "Tomcat already started!"
            exit 1
        fi
        echo "Starting Tomcat: "
        su -s /bin/bash - $TOMCAT_USER -c "$TOMCAT_SCRIPT start"
        daemon $pidfile_make
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/tomcat
        ;;
    stop)
        # Stop daemons.
        if [ ! -f /var/lock/subsys/tomcat ] ; then
            echo "Tomcat already stopped!"
            exit 1
        fi
        echo "Shutting down Tomcat: "
        su -s /bin/bash - $TOMCAT_USER -c "$TOMCAT_SCRIPT stop"
        daemon $pidfile_del
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/tomcat
        ;;
    status)
        status tomcat
        exit $?
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Usage: $TOMCAT_PROG {start|stop|status|restart}"
        exit 1
esac

exit 0

