#!/bin/bash
#
# Startup script for the Tomcat Servlet/JSP Container
#
# chkconfig: - 80 20
# description: Tomcat is the servlet container that is used in the
#              official Reference Implementation for the Java Servlet
#              and JavaServer Pages technologies.
# processname: tomcat
# config: /etc/tomcat/server.xml
# pidfile: /var/run/tomcat/tomcat.pid

# Note that it is possible to run multiple instances of tomcat
# without editing this script.  Details may be found in the file
# /usr/share/doc/tomcat-4.1.27/README.multiple.

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

# Work out the service name.
prog=`basename $0 .init`
[ ${prog:0:1} = "S" -o ${prog:0:1} = "K" ] && prog=${prog:3}

# Set the defaults.
CONFIG=/etc/sysconfig/$prog
PIDFILE=/var/run/tomcat/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
CATALINA_HOME=/usr/share/$prog
TOMCAT_USER=$prog
LOGFILE=$CATALINA_HOME/logs/$prog.out
CATALINA_OPTS=
ENSURE_CONFIG_WRITABLE=no
ENSURE_CONFIG_UNWRITABLE=no
ENSURE_WEBAPPS_WRITABLE=no
ENSURE_WEBAPPS_UNWRITABLE=no
SECURITY_MANAGER=no

# Source configuration.
[ -f $CONFIG ] && . $CONFIG

setpermissions() {
    local gid

    # Work out what $TOMCAT_USER's group is.
    gid=`grep "^$TOMCAT_USER:" /etc/passwd | \
             sed 's/^\([^:]*:\)\{3\}\([^:]*\):.*$/\2/'`
    if ! echo $gid | grep -q '^[0-9][0-9]*$'; then
	echo 1>&2 "$prog: unable to determin GID for $TOMCAT_USER"
	return 1
    fi

    # Set the permissions on the config directory.
    if [ "$ENSURE_CONFIG_WRITABLE" = "yes" -a \
	"$ENSURE_CONFIG_UNWRITABLE" = "yes" ]; then
	echo 1>&2 "ENSURE_CONFIG_WRITABLE and _UNWRITABLE are exclusive"
	return 1
    elif [ "$ENSURE_CONFIG_WRITABLE" = "yes" ]; then
	(cd $CATALINA_HOME/conf && \
	    chmod -R og-w . && \
	    chown -R $TOMCAT_USER.$gid . && \
	    chmod 600 tomcat-users.xml) || return 1
    elif [ "$ENSURE_CONFIG_UNWRITABLE" = "yes" ]; then
	(cd $CATALINA_HOME/conf && \
	    chmod -R og-w . && \
	    chown -R 0.0 . && \
	    chgrp $gid tomcat-users.xml
	    chmod 640 tomcat-users.xml) || return 1
    fi

    # Set the permissions on the webapps directory.
    if [ "$ENSURE_WEBAPPS_WRITABLE" = "yes" -a \
	"$ENSURE_WEBAPPS_UNWRITABLE" = "yes" ]; then
	echo 1>&2 "ENSURE_WEBAPPS_WRITABLE and _UNWRITABLE are exclusive"
	return 1
    elif [ "$ENSURE_WEBAPPS_WRITABLE" = "yes" ]; then
	(cd $CATALINA_HOME/webapps && \
	    chmod 755 . && \
	    chown $TOMCAT_USER.$gid .) || return 1
    elif [ "$ENSURE_WEBAPPS_UNWRITABLE" = "yes" ]; then
	(cd $CATALINA_HOME/webapps && \
	    chmod 755 . && \
	    chown 0.0 .) || return 1
    fi
}	    

RETVAL=0

start() {
    local tmpdir cmd newcmd nice

    echo -n $"Starting $prog: "

    if [ ! -f $LOCKFILE ]; then

	# Set the permissions if required
	setpermissions
	RETVAL=$?
	if [ $RETVAL != 0 ]; then
	    failure $"$prog startup"
	    echo
	    return $RETVAL
	fi
	
	cmd="/usr/sbin/tomcat"

	# Ensure that the running process's name is $prog to ensure
	# that service $foo status works.
	if [ $prog != `basename $cmd` ]; then
	    tmpdir=`mktemp -d /tmp/tomcat.XXXXXX`
	    RETVAL=$?
	    if [ $RETVAL = 0 ]; then
		newcmd=$tmpdir/$prog
		ln -s $cmd $newcmd
		RETVAL=$?
	    fi
	    if [ $RETVAL != 0 ]; then
		rm -Rf $tmpdir
		failure $"$prog startup"
		echo
		return $RETVAL
	    fi
	    cmd=$newcmd
	fi

	# Build the command line
	cmd="$cmd -Dcom.redhat.tomcat.sysconf=$CONFIG"
	cmd="$cmd -home $CATALINA_HOME"
	cmd="$cmd -logfile $LOGFILE"
	cmd="$cmd -pidfile $PIDFILE"

	[ "$SECURITY_MANAGER" == "no" ] || cmd="$cmd -security"
	[ "$TOMCAT_USER" = "root" ] || cmd="$cmd -user $TOMCAT_USER"

	cmd="$cmd $CATALINA_OPTS"

	# Make sure it doesn't core dump anywhere; while this could mask
	# problems with the daemon, it also closes some security problems
	ulimit -S -c 0 >/dev/null 2>&1

	# If they set NICELEVEL in /etc/sysconfig/$prog, honor it
	[ -n "$NICELEVEL" ] && nice="nice -n $NICELEVEL"
	
	# Echo daemon
        [ "${BOOTUP:-}" = "verbose" -a -z "$LSB" ] && echo -n " $prog"

	# And start it up.
	$nice initlog $INITLOG_ARGS -c "$cmd"
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
	    success $"$prog startup"
	    touch $LOCKFILE
	else
	    failure $"$prog startup"
	fi

	[ "$tmpdir" ] && rm -Rf $tmpdir
    fi

    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "

    if [ -f $LOCKFILE ]; then
	kill `cat $PIDFILE 2>/dev/null` 2>/dev/null
	RETVAL=$?
 
	if [ $RETVAL = 0 ]; then
	    success $"$prog shutdown"
	    rm -f $LOCKFILE
	else
	    failure $"$prog shutdown"
	fi
    fi

    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status $prog
	;;
  restart|reload)
	stop
	start
	;;
  condrestart)
	if [ -f $LOCKFILE ] ; then
		stop
		start
	fi
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|condrestart|reload|status}"
	exit 1
esac

exit $RETVAL
