#! /bin/sh
#
# duct      Duct monitoring agent
#
# chkconfig: 2345 80 30
# description: Duct is a monitoring agent which can create metrics
#              and route them to other services
# processname: master
# pidfile: /var/run/duct.pid
# config: /etc/duct/duct.yml

### BEGIN INIT INFO
# Provides:          duct
# Required-Start:    $remote_fs $network $named
# Required-Stop:     $remote_fs $network $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop duct
# Description:       Duct is a monitoring agent which can create metrics
#                    and route them to other services
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=`which twistd`
NAME=duct
DESC=Duct
FDHACK=4096

# Get lsb functions
. /lib/lsb/init-functions

test -x $DAEMON || exit 0

LOGDIR=/var/log
PIDFILE=/var/run/$NAME.pid
#LOGGER="--logfile=${LOGDIR}/duct.log"
LOGGER="--syslog"
DODTIME=2
DAEMON_OPTS="--pidfile=${PIDFILE} ${LOGGER} duct -c /etc/duct/duct.yml"

if [ -f /etc/default/$NAME ]; then
    . /etc/default/$NAME
fi

set -e

ulimit -n $FDHACK

running_pid()
{
    # Check if a given process pid's cmdline matches a given name
    pid=$1
    name=$2
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    (cat /proc/$pid/cmdline | tr "\000" "\n"|grep -q $name) || return 1
    return 0
}

running()
{
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    # Obtain the pid and check it against the binary name
    pid=`cat $PIDFILE`
    running_pid $pid $DAEMON || return 1
    return 0
}

force_stop() {
# Forcefully kill the process
    [ ! -f "$PIDFILE" ] && return
    if running ; then
        kill -15 $pid
        # Is it really dead?
        [ -n "$DODTIME" ] && sleep "$DODTIME"s
        if running ; then
            kill -9 $pid
            [ -n "$DODTIME" ] && sleep "$DODTIME"s
            if running ; then
                echo "Cannot kill $NAME (pid=$pid)!"
                exit 1
            fi
        fi
    fi
    rm -f $PIDFILE
    return 0
}

case "$1" in
  start)
        log_begin_msg "Starting $DESC: $NAME"
	start-stop-daemon --start --quiet --pidfile $PIDFILE \
		--exec $DAEMON -- $DAEMON_OPTS
        log_end_msg $?
	;;
  stop)
        log_begin_msg "Stopping $DESC: $NAME"
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE 
        log_end_msg $?
	;;
  force-stop)
	echo -n "Forcefully stopping $NAME: "
        force_stop
        if ! running ; then
            echo "$NAME."
        else
            echo " ERROR."
        fi
	;;
  force-reload)
	start-stop-daemon --stop --test --quiet --pidfile $PIDFILE --exec $DAEMON \ && $0 restart || exit 0
	;;
  restart)
        echo -n "Restarting $NAME: "

	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE 
	[ -n "$DODTIME" ] && sleep $DODTIME

	start-stop-daemon --start --quiet --pidfile $PIDFILE \
		--exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  status)
        status_of_proc -p "$PIDFILE" "$DAEMON" "$DESC"
        ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
	exit 1
	;;
esac

exit 0
