#! /bin/sh
#
# bareos-ctl-sd This shell script takes care of starting and stopping
#		the bareos Storage 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.
#
BACSDBIN=/usr/sbin
BACSDCFG=/etc/bareos
PIDDIR=/var/run
SUBSYSDIR=/var/lock

SD_PORT=9103

SD_USER=bareos
SD_GROUP=bareos
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 ${BACSDBIN}/bareos-sd ] && {
	 echo "Starting the $Bareos Storage daemon"
	 OPTIONS=''
	 if [ "${SD_USER}" != '' ]; then
	    OPTIONS="${OPTIONS} -u ${SD_USER}"
	 fi

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

	 if [ "x${VALGRIND_SD}" = "x1" ]; then
	    valgrind --leak-check=full ${BACSDBIN}/bareos-sd $2 $3 ${OPTIONS} -v -c ${BACSDCFG}/bareos-sd.conf
	 else
	    ${BACSDBIN}/bareos-sd $2 $3 ${OPTIONS} -v -c ${BACSDCFG}/bareos-sd.conf
	 fi
      }
      ;;


   stop)
      [ -x ${BACSDBIN}/bareos-sd ] && {
	 echo "Stopping the $Bareos Storage daemon"
	 killproc ${BACSDBIN}/bareos-sd ${SD_PORT}
      }
      ;;

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

   status)
      [ -x ${BACSDBIN}/bareos-sd   ] && status ${BACSDBIN}/bareos-sd  ${SD_PORT}
      ;;

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