#! /bin/bash
#

### BEGIN INIT INFO
# Provides:          conmand
# Required-Start:    $local_fs $remote_fs $syslog $named $network $time
# Required-Stop:     $local_fs $remote_fs $syslog $named $network
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop the conmand console management daemon
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/conmand
NAME=conmand
DESC=conmand
CONFIG=/usr/local/etc/conman.conf

test -x $DAEMON || exit 0

if ! [ -x "/lib/lsb/init-functions" ]; then
        . /lib/lsb/init-functions
else   
        echo "E: /lib/lsb/init-functions not found, lsb-base (>= 3.0-6) needed"
        exit 1
fi

set -e

case "$1" in
  start)
        log_daemon_msg "Starting $DESC" "$NAME"
        $DAEMON
        log_end_msg $?
        ;;
  stop)
        log_daemon_msg "Stopping $DESC" "$NAME"
        if [ `$DAEMON -q 2>&1 | grep ERROR | wc -l` -ne 1 ]; then
                $DAEMON -k
                # wait until it is really stopped, might take a while
                while [ `$DAEMON -q 2>&1 | grep ERROR | wc -l` -ne 1 ]; do
                        echo -e ".\c"
                        sleep 1
                done
        fi
        log_end_msg $?
        ;;
  reload|force-reload)
        log_daemon_msg "Reloading $DESC configuration files" "$NAME"
        $DAEMON -r
        log_end_msg $?
        ;;
  status)
        $DAEMON -q
    ;;
  restart)
        $0 stop
        sleep 4
        $0 start
        ;;
  *)   
        log_failure_msg "Usage: $N {start|stop|restart|reload|force-reload}"
        exit 1
        ;;
esac

exit 0
