#!/bin/sh

# chkconfig: 2345 85 15
# description: Thruk FCGI Daemon

### BEGIN INIT INFO
# Provides:          thruk
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop the Thruk FCGI Daemon
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=thruk
PIDFILE=/var/cache/thruk/thruk.pid
STARTURL="http://localhost/thruk/cgi-bin/remote.cgi"
WGET="/usr/bin/wget"

if [ ! -z $OMD_SITE ]; then
    STARTURL="http://localhost/$OMD_SITE/thruk/cgi-bin/remote.cgi"
    PIDFILE=$OMD_ROOT/tmp/thruk/thruk.pid
    . $OMD_ROOT/etc/omd/site.conf
    if [ -e $OMD_ROOT/.thruk ]; then
        . $OMD_ROOT/.thruk
    fi
    if [ "$CONFIG_APACHE_MODE" != "own" ]; then
        echo "only available for apaches 'own' mode!"
        exit 1
    fi
    # make sure apache is running
    omd status apache > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "apaches is not running, cannot start!"
        exit 1
    fi
else
    # load extra environment variables
    if [ -f /etc/sysconfig/thruk ]; then
      . /etc/sysconfig/thruk
    fi
    if [ -f /etc/default/thruk ]; then
      . /etc/default/thruk
    fi
fi

# make it possible to override startcmd by sysconfig
if [ -z "$STARTCMD" ]; then
  STARTCMD="$WGET --no-proxy --no-check-certificate --post-data='' -q -O /dev/null $STARTURL"
fi

case "$1" in
    start)
        if [ "$STARTCMD" = "" ]; then
            echo "$NAME starts up on the first request"
        elif [ ! -f $WGET ]; then
            echo "wget not found, cannot send initial request"
        else
            if [ ! -z $OMD_SITE ]; then
              if [ -d "/var/lock/subsys" ]; then
                touch /var/lock/subsys/thruk >/dev/null 2>&1
              fi
            fi
            echo -n "Starting $NAME..."
            $STARTCMD > /dev/null 2>&1 &
            for x in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5; do
                echo -n "."
                test -f $PIDFILE && break;
                sleep 1
            done
            test -f $PIDFILE
            if [ $? -eq 0 ]; then
                pid=`cat $PIDFILE 2>/dev/null`
                echo "($pid) OK"
                exit 0;
            else
                echo "failed"
                exit 1;
            fi
        fi
    ;;
    stop)
        echo -n "Stopping $NAME..."
        if [ ! -z $OMD_SITE ]; then
          rm -f /var/lock/subsys/thruk >/dev/null 2>&1
        fi
        pid=`cat $PIDFILE 2>/dev/null`
        if [ -z $pid ]; then
            echo ". Not running."
        else
            kill $pid
            for x in 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5; do
                echo -n "."
                ps -p $pid > /dev/null 2>&1 || break;
                sleep 1
            done
            ps -p $pid > /dev/null 2>&1;
            if [ $? -ne 0 ]; then
                echo "OK"
                exit 0;
            else
                echo "failed"
                exit 1;
            fi
        fi
    ;;
    status)
        pid=`cat $PIDFILE 2>/dev/null`
        if [ "$pid" != "" ]; then
            ps -p $pid > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                echo "$NAME is running with pid $pid"
                exit 0;
            fi
        fi
        echo "$NAME is not running"
        exit 1;
    ;;
    reload|force-reload|restart)
        $0 stop && $0 start
        exit $?
        ;;
    *)
        echo "Usage: $NAME {start|stop|status|reload|restart|force-reload}"
        exit 1
        ;;
esac

exit 0
