#! /bin/sh
#
# bareos-ctl-fd This shell script takes care of starting and stopping
#		the bareos File daemon.
#
#   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.
#
# description: Backup Archiving REcovery Open Sourced.
#

PSCMD="ps -ax -o pid,command"
PS="ps"

#
# On Solaris, you may need to use nawk, or alternatively,
#  add the GNU binaries to your path, such as /usr/xpg4/bin
#
AWK=/usr/bin/awk

# All these are not *really* needed but it makes it
#  easier to "steal" this code for the development
#  environment where they are different.
#


BAREOS_FILEDAEMON_BINARY=${BAREOS_FILEDAEMON_BINARY:-/usr/pkg/sbin/bareos-fd}
PIDDIR=${PIDDIR:-/var/run/bareos}
SUBSYSDIR=/var/db/bareos

BAREOS_CONFIG_DIR=${BAREOS_CONFIG_DIR:-/usr/pkg/etc/bareos}
BAREOS_FD_PORT=${BAREOS_FD_PORT:-9102}
BAREOS_FD_USER=${BAREOS_FD_USER:-root}
BAREOS_FD_GROUP=${BAREOS_FD_GROUP:-bareos}

Bareos="Bareos"
PIDOF=""
PGREP="/usr/bin/pgrep"

OS=`uname -s`

#
# Source the generic functions.
#
. /usr/pkg/lib/bareos/scripts/bareos-ctl-funcs

# if /lib/tls exists, force Bareos to use the glibc pthreads instead
if [ -d "/lib/tls" -a $OS = "Linux" -a `uname -r | cut -c1-3` = "2.4" ] ; then
   export LD_ASSUME_KERNEL=2.4.19
fi

case "$1" in
    start)
        if [ -x ${BAREOS_FILEDAEMON_BINARY} ]; then
            echo "Starting the $Bareos File daemon"
            OPTIONS=''
            if [ "${BAREOS_FD_USER}" != '' ]; then
                OPTIONS="${OPTIONS} -u ${BAREOS_FD_USER}"
            fi

            if [ "${BAREOS_FD_GROUP}" != '' ]; then
                OPTIONS="${OPTIONS} -g ${BAREOS_FD_GROUP}"
            fi

            if [ "${BAREOS_CONFIG_DIR}" != '' ]; then
                OPTIONS="${OPTIONS} -c ${BAREOS_CONFIG_DIR}"
            fi

            if [ "x${VALGRIND_FD}" = "x1" ]; then
                valgrind --leak-check=full ${BAREOS_FILEDAEMON_BINARY} -v $2 $3 ${OPTIONS}
            else
                ${BAREOS_FILEDAEMON_BINARY} -v $2 $3 ${OPTIONS}
            fi
        fi
        ;;

    stop)
        if [ -x ${BAREOS_FILEDAEMON_BINARY} ]; then
            echo "Stopping the $Bareos File daemon"
            killproc ${BAREOS_FILEDAEMON_BINARY} ${BAREOS_FD_PORT}
        fi
        ;;

    restart)
        $0 stop
        sleep 5
        $0 start
        ;;

    status)
        [ -x ${BAREOS_FILEDAEMON_BINARY} ] && status ${BAREOS_FILEDAEMON_BINARY} ${BAREOS_FD_PORT}
        exit $?
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0
