#! /bin/sh
#
#  Developer's script. It assumes that everything is in the
#  source tree; that this script is invoked at the top level,
#  and that the working directory is one level up.
#

dir_port=8101
fd_port=8102
sd_port=8103
piddir=../working
subsysdir=../working
pwd=`pwd`

# 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 third arg to be kill level
    if [ "$3" != "" ] ; then
        killlevel=$3
    else
        notset=1
        killlevel="-9"
    fi

    # Get base program name
    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 $piddir/$base.$2.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

    # Get base program name
    base=`basename $1`

    # First try PID file
    if [ -f $piddir/$base.$2.pid ] ; then
        pid=`head -1 $piddir/$base.$2.pid`
        if [ "$pid" != "" ] ; then
            echo $pid
            return 0
        fi
    fi

    # Next try "pidof"
   if [ -x /sbin/pidof ] ; then
       pid=`/sbin/pidof $1`
   fi
   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

    # Get base program name
    base=`basename $1`

   # First try "pidof"
   if [ -x /sbin/pidof ] ; then
       pid=`/sbin/pidof $1`
   fi
   if [ "$pid" != "" ] ; then
       echo "$base (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 "$base (pid $pid) is running..."
           return 0
       fi
   fi

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

success() {
  return 0
}

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

# Start daemons using "test" configuration files
case "$1" in
    start)
       echo "Starting the Storage daemon"
       ${pwd}/src/stored/bacula-sd $2 -v -v -c ${pwd}/src/stored/stored.conf
       echo "Starting the File daemon"
       ${pwd}/src/filed/bacula-fd $2 -v -c ${pwd}/src/filed/filed.conf
       sleep 1
       echo "Starting the Director daemon"
       ${pwd}/src/dird/bacula-dir $2 -v -c ${pwd}/src/dird/dird.conf
       ;;
    stop)
       echo "Stopping the File daemon"
       killproc ${pwd}/src/filed/bacula-fd $fd_port
       echo "Stopping the Storage daemon"
       killproc ./src/stored/bacula-sd     $sd_port
       echo "Stopping the Director daemon"
       killproc ${pwd}/src/dird/bacula-dir $dir_port
       echo
       ;;
    restart)
       $0 stop
       $0 start
       ;;
    status)
       status ${pwd}/src/stored/bacula-sd $sd_port
       status ${pwd}/src/filed/bacula-fd  $fd_port
       status ${pwd}/src/dird/bacula-dir  $dir_port
       ;;
    *)
       echo "Usage: $0 {start|stop|restart|status}"
       exit 1
       ;;
esac
exit 0
