#!/bin/sh
#
# tomcat This shell script takes care of starting and stopping Tomcat
#
# chkconfig: 345 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Description: start and stop tomcat
# Short-Description: start and stop tomcat
### END INIT INFO
#

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

. /etc/rc.d/init.d/functions

export NAME=tomcat
export TOMCAT_HOME=/opt/$NAME
export CATALINA_PID=/var/run/tomcat/$NAME.pid
export CATALINA_OPTS="-Xms512m -Xmx4096m -Dlog4j.configuration=file://${deploy.dir}/conf/log4j.properties"
export TOMCAT_LOCK=/var/lock/subsys/$NAME


start() {
    runuser -s /bin/bash root -c "$TOMCAT_HOME/bin/startup.sh"
	RETVAL=$?
	echo -n "Starting $NAME"
	if [ $RETVAL = 0 ]; then
		touch $TOMCAT_LOCK
		echo_success
	else
		echo_failure	
	fi	
	echo
}


stop() {
	$TOMCAT_HOME/bin/shutdown.sh
	RETVAL=$?
	echo -n "Stopping $NAME"
	if [ $RETVAL = 0 ]; then
		rm -f $TOMCAT_LOCK
		echo_success
	else
		echo_failure	
	fi	
	echo
}


case "$1" in
	start)
		start
		;; 
	stop)
		if [ -s "$CATALINA_PID" ]; then
			stop
		else
			echo "It seems like tomcat is not running. You may try force-stop"
			echo -n "Stopping $NAME"
			echo_warning
			echo
		fi 
		;; 
	force-stop)
		stop
		;; 
	restart)
		$0 stop
#		for i in {1..18}
#		do
#			echo -n .
#			sleep 1
#		done
#		echo .
		$0 start
		;; 
	status)
		status -p $CATALINA_PID $NAME
		;; 
	version)
		$TOMCAT_HOME/bin/catalina.sh version
		;;
	*) 
		echo "Usage: $0 {start|stop|force-stop|restart|status|version}"
		exit 1
esac
exit 0
