#! /bin/bash
#
### BEGIN INIT INFO
# Provides:          ioniced
# Required-Start:    boot.localfs boot.crypto
# Should-Start:
# Required-Stop:     
# Should-Stop:
# Default-Start:     B
# Default-Stop:      
# Short-Description: Start the ionice daemon
# Description:       Start the ionice daemon which checks
#	for new non-user (thus system) processes and 
#	changed their I/O priority in some cases in order 
#	to leave the user undisturbed by low priority tasks
#	with many disk accesses.
### END INIT INFO
# 

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
FOO_BIN=/daten/scripte/ioniced.pl
test -x $FOO_BIN || { echo "$FOO_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

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

# Read config	
#. $FOO_CONFIG

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting ioniced.pl as a daemon "
	## Start daemon with startproc(8). If this fails
	## the return value is set appropriately by startproc.
	cd # put nohup.out in /root
	$FOO_BIN &>/dev/null &
	disown

	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down ioniced.pl "
	## Stop daemon with killproc(8) and if this fails
	## killproc sets the return value according to LSB.

	killall ioniced.pl

	# Remember status and be verbose
	rc_status -v
	;;
    restart)
        "$0" stop
        "$0" start
        ;;
    status)
	echo -n "Checking for service ioniced.pl "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# NOTE: checkproc returns LSB compliant status values.
	/sbin/checkproc ioniced.pl
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	rc_status -v
	;;
    dump)
        lines=$(echo $(/bin/ps -C ioniced.pl | wc -l))
        if [ $lines -eq 1 ]
        	then
        	echo -n "No instance of ioniced.pl found."
        	success=no
        elif [ $lines -eq 2 ]
        	then
	        killall -USR1 ioniced.pl && { echo -n "I/O priority status has been dumped"; success=yes; } || 
        		{ echo -n "I/O priority could not be dumped"; success=no; }
        else # $lines -gt 2
        	echo -n "More than one instance of ioniced.pl found; no signal sent."
        	success=no
        fi
        test $success = yes && true || false
	rc_status -v
        ;;
    *)
	echo "Usage: $0 {start|stop|restart|status|dump}"
	exit 1
	;;
esac
rc_exit
