#! /bin/sh
# Copyright (c) 2007 Author: Carsten Schoene <cs@linux-administrator.com>
#
# /etc/init.d/softflowd
#
#   and symbolic its link
#
# /usr/sbin/rcsoftflowd
#
# System startup script for the softflowd daemon
#
### BEGIN INIT INFO
# Provides: softflowd
# Required-Start: $remote_fs $portmap
# Required-Stop:  $null
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Start softflowd
# Description: starts the softflowd daemon. 
### END INIT INFO

SOFTFLOWD_BIN=/usr/sbin/softflowd
SOFTFLOWD_PID=/var/run/softflowd.pid
test -x $SOFTFLOWD_BIN || { echo "$SOFTFLOWD_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0; else exit 5; fi; }

SOFTFLOWD_CONFIG=/etc/sysconfig/softflowd
test -r $SOFTFLOWD_CONFIG || { echo "$SOFTFLOWD_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0; else exit 6; fi; }

# Read config
. $SOFTFLOWD_CONFIG

. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
	echo -n "Starting softflowd"
	startproc -p $SOFTFLOWD_PID $SOFTFLOWD_BIN $SOFTFLOWD_OPTIONS $OPTIONS
	rc_status -v
	;;
    stop)
	echo -n "Shutting down softflowd"
	killproc -p $SOFTFLOWD_PID -TERM $SOFTFLOWD_BIN
	rc_status -v
	;;
    try-restart|condrestart)
        ## Do a restart only if the service was active before.
        ## Note: try-restart is now part of LSB (as of 1.9).
        ## RH has a similar command named condrestart.
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
	$0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        rc_status
	;;
    restart)
	$0 stop
	sleep 1
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service softflowd"
	killproc -p $SOFTFLOWD_PID -HUP $SOFTFLOWD_BIN
	rc_status -v
	;;
    reload)
	echo -n "Reload service softflowd"
	killproc -p $SOFTFLOWD_PID -HUP $SOFTFLOWD_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for softflowd: "
	checkproc -p $SOFTFLOWD_PID $SOFTFLOWD_BIN
	rc_status -v
	;;
    probe)
	if [ /etc/softflowd/softflowd.conf -nt $SOFTFLOWD_PID ]; then
	  echo reload
        elif [ $SOFTFLOWD_CONFIG -nt $SOFTFLOWD_PID ]; then
          echo restart
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
