#! /bin/bash 
#
# 
# chkconfig: 345 16 84

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/lvs-kiss
CONTROL=/usr/sbin/lvs-kiss-control
NAME=lvs-kiss
DESC="Linux Virtual Server - kept simple"
PIDFILE="/var/run/${NAME}.pid"
SOCKFILE="/var/run/${NAME}.sock"
OPTIONS="--pidfile $PIDFILE "
STATUSFILE="/tmp/${NAME}.status"

test -f $DAEMON || exit 0
test -f /etc/lvs-kiss.conf || exit 0

case "$1" in
    start)
        OTHERS="$(pidof -o $PPID -o '%PPID' -x $DAEMON)"
	if [ -n "$OTHERS" ]
            then echo "There seems to be other ${NAME}es alive - I won't start a new one."
            exit 1
	fi
        rm -f "$SOCKFILE"
	echo -n "Starting $NAME: "
        $DAEMON $OPTIONS
        echo -n ". . . "
        $CONTROL ping
	echo "$NAME."
	;;

    stop)
        echo -n "Stopping $NAME via command socket. . . "
        $CONTROL shutdown &> /dev/null

        if [ $? -eq 0 ]
            then echo "$NAME"
            exit 0
        fi
        echo "FAILURE - trying with signals"

        PID=$(cat $PIDFILE)
        echo -n "Sending SIGTERM to $NAME (pid $PID): "
        kill $PID &> /dev/null

        if [ $? -ne 0 ]
            then echo "$NAME (pid $PID) is not running"

            PIDS="$(pidof -o '%PPID' -x $DAEMON)"

            if test -n "$PIDS"
                then echo "I found other programs (${PIDS}) invoked as lvs-kiss."
            fi

            exit 1
        fi
        
        for i in $(seq 1 15)
          do kill -0 $PID &> /dev/null
          if [ $? -ne 0 ]
              then
              echo "$NAME."
              exit 0
          else
              echo -n " ."
              sleep 1
          fi
        done
        echo -n "$NAME is not responding to SIGTERM - sending SIGKILL  "
        kill -9 $PID
        exit 1
        ;;

  reload)
        echo -n "Reloading $NAME config with socket command. . . "
        $CONTROL reload &> /dev/null

        if  [ "$?" -eq 0 ]
            then echo "$NAME."
            exit 0
        else
            echo "FAILURE - trying with signals" 
            
            PID=$(cat $PIDFILE)
            echo "Sending SIGHUP til $NAME (pid $PID): "
            kill -HUP $PID &> /dev/null
            if [ $? -ne 0 ]
                then echo "$NAME (pidfile says PID $PID) is not running - exiting"
                exit 1
            fi
            echo "$NAME."
        fi
  ;;
    
    status)

        $CONTROL status  2> /dev/null
        
        if  [ "$?" -eq 0 ]
            then exit 0
        else
            echo "Socket command failed FAILED - trying with signals"
            mv -f $STATUSFILE ${STATUSFILE}.old &> /dev/null
            kill -USR1 $(cat $PIDFILE) &> /dev/null
            if [ $? -ne 0 ]
                then echo "$NAME is not running - exiting"
                exit 1
            fi
            echo -n "Waiting for status . . ."
            until [ -r $STATUSFILE ]
              do echo -n " ."
              sleep 1
            done
            echo " OK"
            cat $STATUSFILE
        fi
        ;;
    
    restart|force-reload)
	echo "Restarting $NAME: "
	$0 stop
	echo -n "Sleeping . . .";
	for i in 1 2 3; do sleep 1; echo -n " .";done
        echo ""
	$0 start
	;;

    *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;

esac

exit 0
