#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          qos
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: qos init script
# Description:       Provides control over qos daemon.
#                    <...>
#                    <...>
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=userapd
NAME=qos
DESC="Quality of Service system" 
PIDFILE=/var/run/userapd


. /lib/lsb/init-functions


# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
	. /etc/default/$NAME
fi


function test_pid() {
    PID=`cat $1 2>/dev/null`
    if [ -z "$PID" -o ! -d /proc/$PID ]; then
	return 1
    else
	return 0
    fi
}


case "$1" in
  start)
	test_pid $PIDFILE
	if [ $? -eq 0 ]; then
	    log_warning_msg "Qos daemon is already running"
	else 
	    log_daemon_msg "Starting $DESC " "$NAME"
	    /usr/sbin/setup-qos start
	    log_end_msg $?
        fi
	;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
	/usr/sbin/setup-qos stop
	killproc -p $PIDFILE userapd
	RETVAL=$?
	[ $RETVAL -eq 0 ] && [ -e "$PIDFILE" ] && rm -f $PIDFILE
	log_end_msg $RETVAL
        ;;
  restart|force-reload)
	${0} stop
	${0} start
	;;
  status)
	status_of_proc -p $PIDFILE userapd qos
	exit $?
        ;;
  reload)
        log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
        log_warning_msg "cannot re-read the config file (use restart)."
        ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|status}" >&2
	exit 1
	;;
esac

exit 0
