#!/bin/sh
#
#     SUSE system startup script for service/daemon openfire
#     Copyright (C) 2007-2009 Alexandre Duarte Rogoski, Exata T.I.
#     Authors: Alexandre Duarte Rogoski <alexandre@exatati.com.br>,
#              Leonardo Duarte Rogoski <leo@exatati.com.br>
#
#     This library is free software; you can redistribute it and/or modify it
#     under the terms of the GNU Lesser General Public License as published by
#     the Free Software Foundation; either version 2.1 of the License, or (at
#     your option) any later version.
#
#     This library is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
#     USA.
#
# /etc/init.d/openfire
#   and its symbolic link
# /usr/sbin/rcopenfire
#
### BEGIN INIT INFO
# Provides:          openfire
# Required-Start:    $syslog $local_fs $remote_fs $network mysql
# Should-Start:      $time ypbind mysql
# Required-Stop:     $syslog $remote_fs $network
# Should-Stop:       $time ypbind sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Openfire daemon startup script
# Description:       Openfire is an XMPP server, which is a server that facilitates
#	XML based communication, such as chat.
### END INIT INFO

# config: /opt/openfire/conf/openfire.xml
# config: /etc/sysconfig/openfire
# pidfile: /var/run/openfire.pid

# Check for existence of needed config file and read it
OPENFIRE_CONFIG=/etc/sysconfig/openfire
test -r $OPENFIRE_CONFIG || {
    echo "$OPENFIRE_CONFIG not existing";
	if [ "$1" = "stop" ]; then
	    exit 0
	else
	    exit 6
	fi
}

# Read config
. $OPENFIRE_CONFIG

# If a openfire home variable has not been specified, try to determine it.
if [ -z "$OPENFIRE_HOME" -o ! -d "$OPENFIRE_HOME" ]; then
    if [ -d "/usr/share/openfire" ]; then
	OPENFIRE_HOME="/usr/share/openfire"
    else
	echo "Could not find Openfire installation under /usr/share."
	echo "Please specify the Openfire installation location as variable OPENFIRE_HOME"
	echo "in /etc/sysconfig/openfire."
	exit 1
    fi
fi

# If openfire user is not set in sysconfig, set to daemon.
[ -z "$OPENFIRE_USER" ] && OPENFIRE_USER="daemon"

# If pid file path is not set in sysconfig, set to /var/run/openfire.pid.
[ -z "$OPENFIRE_PIDFILE" ] && OPENFIRE_PIDFILE="/var/run/openfire.pid"

# If log path is not set in sysconfig, set to $OPENFIRE_HOME/logs.
[ -z "$OPENFIRE_LOGDIR" ] && OPENFIRE_LOGDIR="${OPENFIRE_HOME}/logs"

# Prepare location of openfire libraries
OPENFIRE_LIB="${OPENFIRE_HOME}/lib"

# Prepare openfire command line
OPENFIRE_OPTS="${OPENFIRE_OPTS} -DopenfireHome=${OPENFIRE_HOME} -Dopenfire.lib.dir=${OPENFIRE_LIB}"

# Prepare local java class path
if [ -z "$LOCALCLASSPATH" ]; then
    LOCALCLASSPATH="${OPENFIRE_LIB}/startup.jar"
else
    LOCALCLASSPATH="${OPENFIRE_LIB}/startup.jar:${LOCALCLASSPATH}"
fi

# Source LSB init functions
# providing start_daemon, killproc, pidofproc, 
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     be verbose in local rc status and clear it afterwards
#      rc_status -v -r  ditto and clear both the local and overall rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear both the local and overall rc status
#      rc_exit          exit appropriate to overall rc status
#      rc_active        checks whether a service is activated by symlinks
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0       - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

# Attempt to locate java installation.
if [ -z "$JAVA_HOME" ]; then
    if [ -d "${OPENFIRE_HOME}/jre" ]; then
	JAVA_HOME="${OPENFIRE_HOME}/jre"
    elif [ -d "/etc/alternatives/jre" ]; then
	JAVA_HOME="/etc/alternatives/jre"
    else
	jdks=`ls -r1d /usr/java/j*`
	for jdk in $jdks; do
	    if [ -f "${jdk}/bin/java" ]; then
		JAVA_HOME="$jdk"
		break
	    fi
	done
    fi
fi
JAVACMD="${JAVA_HOME}/bin/java"

if [ ! -d "$JAVA_HOME" -o ! -x "$JAVACMD" ]; then
    echo "Error: JAVA_HOME is not defined correctly."
    echo "       Can not sure execute $JAVACMD."
    exit 1
fi

# Export any necessary variables
export JAVA_HOME JAVACMD

# Lastly, prepare the full command that we are going to run.
OPENFIRE_RUN_CMD="${JAVACMD} -server ${OPENFIRE_OPTS} -classpath \"${LOCALCLASSPATH}\" -jar \"${OPENFIRE_LIB}/startup.jar\""

start() {
    OLD_PWD=`pwd`
    cd $OPENFIRE_LOGDIR

    # Start daemons.
    echo -n "Starting Openfire "

    rm -f nohup.out
    su -s /bin/sh -c "nohup $OPENFIRE_RUN_CMD > $OPENFIRE_LOGDIR/nohup.out 2>&1 &" $OPENFIRE_USER
    RETVAL=$?
    PID=`ps ax --width=1000 | grep openfire | grep startup.jar | awk '{print $1}'`

    if [ $RETVAL -eq 0 -a ! -z "$PID" -a ! -z "$OPENFIRE_PIDFILE" ]; then
	echo $PID > $OPENFIRE_PIDFILE
    fi

    echo

    [ $RETVAL -eq 0 -a -d /var/lock/subsys ] && touch /var/lock/subsys/openfire

    sleep 1 # allows prompt to return
    cd $OLD_PWD
}

stop() {
    # Stop daemons.
    echo -n "Shutting down Openfire "

    [ -f "$OPENFIRE_PIDFILE" ] && kill `cat $OPENFIRE_PIDFILE`
	RETVAL=$?
    echo

    [ $RETVAL -eq 0 -a -f "$OPENFIRE_PIDFILE" ] && rm -f $OPENFIRE_PIDFILE
    [ $RETVAL -eq 0 -a -f "/var/lock/subsys/openfire" ] && rm -f /var/lock/subsys/openfire
}

restart() {
    stop
    sleep 10 # give it a few moments to shut down
    start
}

condrestart() {
    [ -e "/var/lock/subsys/openfire" ] && restart
    return 0
}

status() {
    echo -n "Checking for service Openfire "
    pid=`cat $OPENFIRE_PIDFILE 2>&1`
    if [ "$?" = "1" ]; then
	echo "openfire is not running"
	RETVAL=0
    else 
	ps -p $pid > /dev/null 2>&1
	if [ "$?" = "0" ]; then 
	    echo "openfire is running"
	    RETVAL=0
	else 
	    echo "openfire is not running"
	    RETVAL=0
	fi
    fi
}

# Handle how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	restart
	;;
    condrestart)
	condrestart
	;;
    reload)
	restart
	;;
    status) 
	status
	;;
    *)
	echo "Usage $0 {start|stop|restart|status|condrestart|reload}"
	exit 1
	;;    
esac
rc_exit
