#!/bin/sh
#
# Startup script for Geronimo 1.0, the Apache J2EE Server
#
# chkconfig: - 80 20
# description: Geronimo 1.0 is the Apache Server for J2EE 1.4
# processname: geronimo
# pidfile: /var/run/geronimo.pid
# config:  /etc/geronimo/geronimo.conf
#
# Gomez Henri <hgomez@users.sourceforge.net>
# Keith Irwin <keith_irwin@non.hp.com>
# Nicolas Mailhot <nicolas.mailhot@one2team.com>
# Ralph Apel <r.apel@r-apel.de>
#
# version 1.00 - Created from tomcat5 init script
#

# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

# Get Geronimo config

GERONIMO_CFG="/etc/geronimo/geronimo.conf"

[ -r "$GERONIMO_CFG" ] && . "${GERONIMO_CFG}"

# Path to the Geronimo launch script (direct don't use wrapper)
GERONIMO_SCRIPT=/usr/bin/dgeronimo

# Path to the script that will refresh jar symlinks on startup
GERONIMO_RELINK_SCRIPT="/usr/share/geronimo-1.0/bin/relink"

# Geronimo name :)
GERONIMO_PROG=geronimo
        
# if GERONIMO_USER is not set, use geronimo like Apache HTTP server
if [ -z "$GERONIMO_USER" ]; then
    GERONIMO_USER="geronimo"
fi

# Since the daemon function will sandbox geronimo
# no environment stuff should be defined here anymore.
# Please use the /etc/geronimo/geronimo.conf file instead ; it will
# be read by the dgeronimo script

RETVAL=0

# See how we were called.
start() {
    echo -n "Starting $GERONIMO_PROG: "

	if [ -f /var/lock/subsys/geronimo ] ; then
 		if [ -f /var/run/geronimo.pid ]; then
   	      		read kpid < /var/run/geronimo.pid
         		if checkpid $kpid 2>&1; then
             			echo "process allready running"
             			return -1
         		else
             			echo "lock file found but no process running for pid $kpid, continuing"
 			fi
 		fi
 	fi
 
 	export GERONIMO_PID=/var/run/geronimo.pid
 	touch $GERONIMO_PID
 	chown $GERONIMO_USER:$GERONIMO_USER $GERONIMO_PID

        $GERONIMO_RELINK_SCRIPT

        if [ -x /etc/rc.d/init.d/functions ]; then
        	daemon --user $GERONIMO_USER $GERONIMO_SCRIPT start 
    	else
        	su - $GERONIMO_USER -c "$GERONIMO_SCRIPT start"
    	fi

    	RETVAL=$?
    	echo
    	[ $RETVAL = 0 ] && touch /var/lock/subsys/geronimo
    	return $RETVAL
}

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

    if [ -f /var/lock/subsys/geronimo ] ; then
      if [ -x /etc/rc.d/init.d/functions ]; then
          daemon --user $GERONIMO_USER $GERONIMO_SCRIPT stop
      else
          su - $GERONIMO_USER -c "$GERONIMO_SCRIPT stop"
      fi
      RETVAL=$?

      if [ $RETVAL = 0 ]; then
        count=0;

        if [ -f /var/run/geronimo.pid ]; then

            read kpid < /var/run/geronimo.pid
            let kwait=$SHUTDOWN_WAIT

            until [ `ps --pid $kpid | grep -c $kpid` = '0' ] || [ $count -gt $kwait ]
            do
                echo "waiting for processes to exit";
                sleep 1
                let count=$count+1;
            done

            if [ $count -gt $kwait ]; then
                echo "killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
                kill -9 $kpid
            fi
        fi
    
		rm -f /var/lock/subsys/geronimo /var/run/geronimo.pid
    fi

    fi
}


# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 2	
        start
        ;;
  condrestart)
        if [ -f /var/run/geronimo.pid ] ; then
                stop
                start
        fi
        ;;
  *)
        echo "Usage: $GERONIMO_PROG {start|stop|restart|condrestart}"
        exit 1
esac

exit $RETVAL
