#!/bin/sh
### BEGIN INIT INFO
# Provides:          epoptes
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Epoptes service
# Description:       A twisted-based daemon that manages epoptes-client
#                    and GUI connections.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/python
PIDFILE=/var/run/epoptes.pid
LOGFILE=/var/log/epoptes.log
DESC="Epoptes service"

# If this is an LTSP client, then only run the service if EPOPTES=True
if egrep -qs 'ltsp|nfs|nbd' /proc/cmdline && [ -x /usr/bin/getltscfg ]; then
    case "$(getltscfg EPOPTES || true)" in
       [Tt][Rr][Uu][Ee]|[Yy]|[Yy][Ee][Ss])
            ;;
       *)
            exit 0
            ;;
    esac
fi

. /lib/lsb/init-functions

case "$1" in
    start)
        log_daemon_msg "Starting the epoptes daemon"
        start-stop-daemon --start --startas /usr/bin/twistd --quiet --oknodo --pidfile "$PIDFILE" --exec "$DAEMON" -- \
            --pidfile "$PIDFILE" --logfile "$LOGFILE" epoptes
        log_end_msg $?
        ;;
    stop)
        log_daemon_msg "Stopping the epoptes daemon"
        start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE" || true
        log_end_msg $?
        ;;
    restart|force-reload)
        "$0" stop
        sleep 2
        "$0" start
        ;;
    status)
        status_of_proc -p $PIDFILE $DAEMON epoptes && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
        exit 1
    ;;
esac
