#! /bin/sh
#
# fd	       This shell script takes care of starting and stopping
#	       the Bacula File daemon for Client only machines.
#
#   This is pretty much watered down version of the RedHat script
#   that works on Solaris as well as Linux, but it won't work everywhere.
#
#   It would be preferable to do the "make install-auto-start-fd", then 
#   this script is not necessary, but it is here in case you want to do
#   it by hand.
#
# description: It comes by night and sucks the vital essence from your computers.
#

PSCMD="@PSCMD@"

# A function to stop a program.
killproc() {
    RC=0
    # Test syntax.
    if [ $# = 0 ]; then
	    echo "Usage: killproc {program} [signal]"
	    return 1
    fi

    notset=0
    # check for second arg to be kill level
    if [ "$2" != "" ] ; then
	    killlevel=$2
    else
	    notset=1
	    killlevel="-9"
    fi

    # Save basename.
    base=`basename $1`

    # Find pid.
    pid=`pidofproc $base`

    # Kill it.
    if [ "$pid" != "" ] ; then
	    if [ "$notset" = "1" ] ; then
		   if ps -p $pid>/dev/null 2>&1; then
		       # TERM first, then KILL if not dead
		       kill -TERM $pid 2>/dev/null
		       sleep 1
		       if ps -p $pid >/dev/null 2>&1 ; then
			    sleep 1
			    if ps -p $pid >/dev/null 2>&1 ; then
				    sleep 3
				    if ps -p $pid >/dev/null 2>&1 ; then
				       kill -KILL $pid 2>/dev/null
				    fi
			    fi
		       fi
		    fi
		    ps -p $pid >/dev/null 2>&1
		    RC=$?
		    [ $RC -eq 0 ] && failure "$base shutdown" || success "$base shutdown"
#		    RC=$((! $RC))
	    # use specified level only
	    else
		    if ps -p $pid >/dev/null 2>&1; then
			    kill $killlevel $pid 2>/dev/null
			    RC=$?
			    [ $RC -eq 0 ] && success "$base $killlevel" || failure "$base $killlevel"
		    fi
	    fi
    else
	failure "$base shutdown"
    fi
    # Remove pid file if any.
    if [ "$notset" = "1" ]; then
	rm -f /var/run/$base.pid
    fi
    return $RC
}

# A function to find the pid of a program.
pidofproc() {
    # Test syntax.
    if [ $# = 0 ] ; then
	    echo "Usage: pidofproc {program}"
	    return 1
    fi

    # First try "/var/run/*.pid" files
    if [ -f /var/run/$1.pid ] ; then
	    pid=`head -1 /var/run/$1.pid`
	    if [ "$pid" != "" ] ; then
		    echo $pid
		    return 0
	    fi
    fi

    # Next try "pidof"
#   pid=`pidof $1`
#   if [ "$pid" != "" ] ; then
#	    echo $pid
#	    return 0
#   fi

    # Finally try to extract it from ps
    ${PSCMD} | grep $1 | awk '{ print $1 }' | tr '\n' ' '
    return 0

}

status() {
    # Test syntax.
    if [ $# = 0 ] ; then
	    echo "Usage: status {program}"
	    return 1
    fi

#   # First try "pidof"
#   pid=`pidof $1`
#   if [ "$pid" != "" ] ; then
#	    echo "$1 (pid $pid) is running..."
#	    return 0
#   else
	    pid=`${PSCMD} | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } 
		       { if ((prog == $2) || (("(" prog ")") == $2) ||
			 (("[" prog "]") == $2) ||
		       ((prog ":") == $2)) { print $1 ; exit 0 } }' $1`
	    if [ "$pid" != "" ] ; then
		    echo "$1 (pid $pid) is running..."
		    return 0
	    fi
#   fi

    # Next try "/var/run/*.pid" files
    if [ -f /var/run/$1.pid ] ; then
	    pid=`head -1 /var/run/$1.pid`
	    if [ "$pid" != "" ] ; then
		    echo "$1 dead but pid file exists"
		    return 1
	    fi
    fi
    # See if @subsysdir@/$1 exists
    if [ -f @subsysdir@/$1 ]; then
	    echo "$1 dead but subsys locked"
	    return 2
    fi
    echo "$1 is stopped"
    return 3
}

success() {
  return 0
}

failure() {
  rc=$?
  return $rc
}

case "$1" in
    start)
       echo "Starting the Bacula File daemon"
       @sbindir@/bacula-fd $2 -c @sysconfdir@/bacula-fd.conf
       ;;
    stop)
       echo "Stopping the Bacula File daemon"
       killproc @sbindir@/bacula-fd
       echo
       ;;
    restart)
       $0 stop
       sleep 5
       $0 start
       ;;
    status)
       status @sbindir@/bacula-fd
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|status}"
       exit 1
       ;;
esac
exit 0
