#!/bin/sh
#
# /etc/init.d/FOO
#
# Template and example of a LSB conform init script for the service FOO
# See http://www.linuxbase.org/spec/ for details
#
### BEGIN INIT INFO
# Provides:          FOO
# Required-Start:    $syslog $remote_fs
# Should-Start:      $time $portmap
# Required-Stop:     $syslog $remote_fs
# Should-Stop:       $time $portmap
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: FOO daemon used for ABC
# Description:       The FOO service is used for ZZZ
#	The (long) description can spread multiple lines
#	which start with '#<TAB>' or # followed by at least
#       two spaces.
# X-UnitedLinux-Default-Enabled: yes
### END INIT INFO
# 
# Remarks:
# - The LSB actually might be interpreted that there is only a single space
#   between the the colon after the keyword and the first argument, but
#   most or all LSB install_initd's support more than one space
# - Local extensions start with a X-[LANA registered provider or domain]-
#   The X-UnitedLinux-Default-Enabled is one of the few actually used
#   extension (enable service by when installing package, but keep current
#   status when updating).
#
# The other keywords are described by the LSB. Notes on the differences
# between Required-Start and Should-Start (which was introduced by LSB 1.9):
#
# a) Required-Start/Required-Stop is used as hard dependencies, i.e. for
#    services which have to be started before this service. For instance
#    needs the 'nfs-kernel-server' the portmap service. Requiring
#      Required-Start: $portmap
#    ensures that the portmapper is started before the nfs server.
#    If a required service is missing, an init script cannot be installed
#    and an init script which is required by another cannot be removed.
#
# b) Should-Start/Should-Stop is used for weak dependencies. This ensures
#    that the order of the init scripts is useful. A possible use for
#    'autofs' is to ask for
#      Should-Start: nis
#    which enables to read maps via NIS. Note that not all LSB install_initd
#    programs support Should-Start (Debian's does) and therefore one should
#    try hard not to rely on its support.
#
#    The Required-Stop/Should-Stop usually contain the same services as
#    Required-Start/Should-Start do.
#
#    Besides using the services provided by the Provides section of
#    init script, those predefined facilities are available
#    (while they start with a $ they are no shell variables):
#
#    a) LSB 1.1 facility names
#    - $local_fs    all local filesystems are mounted
#    - $remote_fs   all remote filesystems are mounted
#                   (note that /usr might be remote)
#    - $syslog      system logging is operational
#    - $network     low level networking (ethernet card etc.)
#    - $named       hostname resolution available
#    - $netdaemons  all network daemons are running
#                   (Removed in LSB 1.2)
#    b) LSB 1.2 facility names
#    - $time        the system time has been set (e.g. NTP)
#    - $portmap     daemons providing SunRPC/ONCRPC portmapping service
#                   are running
#
# The LSB specifies those runlevels, most services use 3 and 5 for
# Default-Start and 0 1 2 6 for Default-Stop.
# 
# 0 - halt
# 1 - single user mode
# 2 - multiuser with no network services exported
# 3 - normal/full multiuser
# 4 - reserved for local use (usually normal/full multiuser)
# 5 - multiuser with xdm or equivalent
# 6 - reboot
# 
# Note on that script names should follow the LSB:
# http://www.linuxbase.org/spec/gLSB/gLSB/scrptnames.html
# There is a registry for script names that are reserved for use by
# distributions and registered script and provider names at
# http://www.lanana.org/

# Source LSB init functions
# This provides start_daemon, killproc, pidofproc,
# log_success_msg, log_failure_msg and log_warning_msg.
. /lib/lsb/init-functions

# Since init scripts are config files, they might be left after an uninstall
# Check whether the binary is still present:
FOO_BIN=/usr/sbin/FOO
test -x "$FOO_BIN" || {log_failure_msg "$FOO_BIN not installed"; exit 5}

# Check whether a required configuration file is available
FOO_CONFIG=/etc/FOO.conf
test -r "$FOO_CONFIG" || {log_failure_msg "$FOO_CONFIG not existing"; exit 6}

# Those LSB defined exit status codes shall be used (except for status)
# 0     sucess
# 1	generic or unspecified error (current practice)
# 2	invalid or excess argument(s)
# 3	unimplemented feature (for example, "reload")
# 4	user had insufficient privilege
# 5	program is not installed
# 6	program is not configured
# 7    	program is not running
# 8-199 reserved (8-99 LSB, 100-149 distribution, 150-199 application)
# 
# Note that those situation shall also be regarded as success:
# * restarting a service (instead of reloading it)
#   with the "force-reload" argument
# * running "start" on a service already running
# * running "stop" on a service already stopped or not running
# * running "restart" on a service already stopped or not running
# * running "try-restart" on a service already stopped or not running
#

case "$1" in
    start)
	# Start service with startproc which shall return the
        # LSB exit status
	start_daemon "$FOO_BIN" -c "$FOO_CONFIG"
	STATUS=$?
	if [ "$STATUS" = 0 ]
	then
            log_success_msg "Starting FOO"
        else
  	    log_failure_msg "Starting FOO"
        fi
	exit $STATUS
	;;
    stop)
	echo -n "Shutting down FOO "
	# Stop the service with killproc which shall return the
        # LSB exit status
	killproc "$FOO_BIN"
	STATUS=$?
	if [ "$STATUS" = 0 ]
	    log_success_msg "Shutting down FOO"
	    exit 0
        else
	    log_failure_msg "Shutting down FOO"
        fi
	exit $STATUS
	;;
    try-restart)
	# Do a restart only if the service is running
	# try-restart has been added to the LSB in 1.9
	# RedHat's similar command is called condrestart.
	$0 status > /dev/null
	STATUS=$?
	if [ "$STATUS" = 0 ]
	then
	    $0 restart
	else
	    log_success_msg "Try-Restarting FOO: not running"
	fi
	exit 0 # not running is also regarded as success
	;;
    restart)
	# Restart service (if running) or start service
	$0 stop
	$0 start
	;;
    force-reload)
	# Reload the configuartion. Usually a SIGHUP is used for this
	# If it doesn't support his signal, restart it (only if running)

        # Supports signalling
	killproc -HUP "$FOO_BIN"
	SIGNAL=$?
	if [ "$SIGNAL" = 0 ]
	then
	    log_success_msg "Reloading FOO"
	else
	    log_failure_msg "Reloading FOO"
	fi
	exit $STATUS

	# Otherwise
	#$0 try-restart
	;;
    reload)
	# Reload configuration file, but don't restart if it is not supported

	# If it supports signaling:
	killproc -HUP "$FOO_BIN"
	SIGNAL=$?
	if [ "$SIGNAL" = 0 ]
	then
	    log_success_msg "Reloading FOO"
	else
	    log_failure_msg "Reloading FOO"
	fi
	exit $STATUS
	
	# Otherwise
	# exit 3
	;;
    status)
	# Use pidofproc to check the status of the service,
	# pidofproc returns the exit status code of 0 when it the process is
	# running.

	# LSB defined exit status codes for status:
	# 0	program is running or service is OK
	# 1	program is dead and /var/run pid file exists
	# 2	program is dead and /var/lock lock file exists
	# 3	program is not running
	# 4	program or service status is unknown
	# 5-199	reserved (5-99 LSB, 100-149 distribution, 150-199 applications)
	
	checkproc "$FOO_BIN" > /dev/null
	STATUS=$?
	if [ "$SIGNAL" = 0 ]
	then
	    log_success_msg "Checking FOO"
	else
	    log_warning_msg "Checking FOO: Not running"
	fi
	exit $STATUS
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
