#!/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?startup"
STARTTIMEOUT=15
STARTLOG="/tmp/thrukstart.log"
CMD="/usr/bin/wget"

if [ ! -z $OMD_SITE ]; then
    STARTURL="http://localhost/$OMD_SITE/thruk/cgi-bin/remote.cgi?startup"
    STARTLOG="$OMD_ROOT/tmp/thrukstart.log"
    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" -a "$CONFIG_APACHE_MODE" != "ssl" ]; then
        echo "only available for apache mode 'own' or 'ssl'!"
        exit 1
    fi
    # make sure apache is running
    omd status apache > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "apache 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
  if ! test -x $CMD; then
    # use curl as fallback
    CMD="/usr/bin/curl"
    STARTCMD="$CMD --noproxy '*' --location --insecure --data '' --verbose --max-time $STARTTIMEOUT $STARTURL"
  else
    STARTCMD="$CMD --no-proxy --no-check-certificate --post-data='' -t 1 --debug --timeout=$STARTTIMEOUT -O - $STARTURL"
  fi
fi

get_pids() {
    pids=`cat $PIDFILE 2>/dev/null`
    if [ "$pids" != "" ]; then
        newpids=""
        for pid in $pids; do
            ps -p $pid > /dev/null 2>&1 && newpids=" $pid"
        done
        pids="$newpids"
    fi
    echo "$newpids"
}

case "$1" in
    start)
        if [ "$STARTCMD" = "" ]; then
            echo "$NAME starts up on the first request"
        elif [ ! -x $CMD ]; then
            echo "$CMD 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..."
            STARTLOGDIR=$(dirname $STARTLOG)
            [ -d $STARTLOGDIR ] || mkdir -p $STARTLOGDIR
            $STARTCMD >$STARTLOG 2>&1 &
            WGETPID=$!
            for x in $(seq $(( $STARTTIMEOUT * 10 )) ); do
                [ $((x%5)) -eq 0 ] && echo -n .
                kill -0 $WGETPID >/dev/null 2>&1 || break;
                sleep 0.1
            done
            test -f $PIDFILE
            pids=`get_pids`
            if [ "$pids" != "" ]; then
                echo "($pids) OK"
                rm -f $STARTLOG
                exit 0;
            else
                echo "failed"
                cat $STARTLOG
                rm -f $STARTLOG
                exit 1;
            fi
        fi
    ;;
    stop)
        echo -n "Stopping $NAME..."
        pids=`get_pids`
        if [ "$OMD_SITE" = "" ]; then
          rm -f /var/lock/subsys/thruk >/dev/null 2>&1
        fi
        if [ "$pids" = "" ]; then
            echo ". Not running."
        else
            kill $pids >/dev/null 2>&1
            if [ "$OMD_SITE" != "" ]; then
                pkill -u $OMD_SITE -f thruk_fastcgi.pl
            fi
            for x in $(seq 150); do
                [ $((x%5)) -eq 0 ] && echo -n "."
                newpids=""
                for pid in $pids; do
                    ps -p $pid > /dev/null 2>&1 && newpids=" $pid"
                done
                pids="$newpids"
                [ "$pids" = "" ] && break
                sleep 0.1
            done
            if [ "$pids" = "" ]; then
                echo "OK"
                exit 0
            else
                echo "failed"
                exit 1
            fi
        fi
    ;;
    status)
        pids=`get_pids`
        if [ "$pids" != "" ]; then
            echo "$NAME is running (pid $pids)."
            exit 0
        fi
        echo "$NAME is not running"
        exit 1
    ;;
    reload|force-reload|restart)
        $0 stop && $0 start
        exit $?
        ;;
    condrestart)
        $0 status && $0 restart
        ;;
    *)
        echo "Usage: $NAME {start|stop|status|reload|restart|force-reload|condrestart}"
        exit 1
        ;;
esac

exit 0
