#! /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 -e"
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.
#
BACFDBIN=/usr/sbin
BACFDCFG=/etc/bareos
PIDDIR=/var/run
SUBSYSDIR=/var/lock

FD_PORT=9102

FD_USER=root
FD_GROUP=root
Bareos="Bareos"
PIDOF="/bin/pidof"
PGREP="/usr/bin/pgrep"

OS=`uname -s`

#
# Source the generic functions.
#
. /etc/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)
      [ -x ${BACFDBIN}/bareos-fd ] && {
	 echo "Starting the $Bareos File daemon"
	 OPTIONS=''
	 if [ "${FD_USER}" != '' ]; then
	    OPTIONS="${OPTIONS} -u ${FD_USER}"
	 fi

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

	 if [ "x${VALGRIND_FD}" = "x1" ]; then
	    valgrind --leak-check=full ${BACFDBIN}/bareos-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bareos-fd.conf
	 else
	    ${BACFDBIN}/bareos-fd $2 $3 ${OPTIONS} -v -c ${BACFDCFG}/bareos-fd.conf
	 fi
      }
      ;;

   stop)
      # Stop the FD first so that SD will fail jobs and update catalog
      [ -x ${BACFDBIN}/bareos-fd ] && {
	 echo "Stopping the $Bareos File daemon"
	 killproc ${BACFDBIN}/bareos-fd ${FD_PORT}
      }
      ;;

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

   status)
      [ -x ${BACFDBIN}/bareos-fd ] && status ${BACFDBIN}/bareos-fd  ${FD_PORT}
      ;;

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