#! /bin/sh
### BEGIN INIT INFO
# Provides:          mrs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Required-Start:	 $remote_fs
# Required-Stop:	 $remote_fs
# Short-Description: Start or stop the MRS server
# Description:       This is the startup script for the MRS 6 server
### END INIT INFO

# Author: Maarten L. Hekkelman <maarten@hekkelman.com>

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/bin:/usr/bin:__BIN_DIR__
DESC="MRS server"
NAME=mrs
PIDFILE=__MRS_RUN_DIR__/$NAME.pid
DAEMON=__BIN_DIR__/$NAME
#DAEMON_ARGS="--config=/etc/mrs/mrs-config.xml --pid=$PIDFILE"
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
	$DAEMON server status --pidfile=$PIDFILE && return 1
	$DAEMON server start --pidfile=$PIDFILE || return 2
	return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
	$DAEMON server status --pidfile=$PIDFILE || return 1
	$DAEMON server stop --pidfile=$PIDFILE || return 2
	rm -f $PIDFILE
	return 0
}

case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
  	$DAEMON server status --pidfile=$PIDFILE
  	case "$?" in
  		0)	log_success_msg "MRS Server is running"
			exit 0
			;;
		*)	log_failure_msg "MRS Server is NOT running"
			exit 1
			;;
	esac
    ;;
  reload|force-reload)
	log_daemon_msg "Reloading $DESC" "$NAME"
	$DAEMON server reload --pidfile $PIDFILE
	log_end_msg $?
	;;
  restart)
	log_daemon_msg "Restarting $DESC" "$NAME"
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
	  	# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
	exit 3
	;;
esac

:
