#!/bin/bash
#
# Tomcat startup file
#
# chkconfig: 2345 99 99
# description: Tomcat startup file
#
# note: 
#       To install tomcat startup script in Fedora,
#       copy this script to /etc/rc.d/init.d,
#       then run:
#               chmod +x tomcat
#               chkconfig --add tomcat
#

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

RETVAL=0
prog="tomcat"

# Some functions to make the below more readable
TOMCAT_BIN=/opt/tomcat/bin


runlevel=$(set -- $(runlevel); eval "echo \$$#" )

start()
{
        echo -n $"Starting $prog: "
        cd $TOMCAT_BIN
        ./startup.sh && success || failure
        RETVAL=$?
        [ "$RETVAL" = 0 ] && touch /var/lock/subsys/tomcat
        echo
}

stop()
{
        echo -n $"Stopping $prog: "
        cd $TOMCAT_BIN
        ./shutdown.sh && success || failure
        RETVAL=$?
        [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/tomcat
        echo
}

status()

{
        SEARCH=`ps -ef|awk '!/awk/ && /\/opt\/tomcat/'`
        RETVAL=$?
        if [ -n "$SEARCH" ]; then
                echo Tomcat is running...
        else
                echo Tomcat is not running...
        fi
        echo
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                sleep 3
                start

                ;;
        status)
                status
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                RETVAL=1
esac
exit $RETVAL