#! /bin/sh
#
# bacula  This shell script takes care of starting and stopping
#	  the bacula Director daemon on Debian/Ubuntu/Kubuntu 
#	  systems. 
#
#   Kern E. Sibbald - 21 March 2008
#
#  For Bacula release @VERSION@ (@DATE@) -- @DISTNAME@
#

### BEGIN INIT INFO
# Provides:		bacula-dir
# Required-Start:	$local_fs $remote_fs $network $time
# Required-Stop:	$local_fs $remote_fs $network $time
# Default-Start:	2 3 4 5
# Default-Stop: 	0 1 6
# Short-Description:	Bacula Director
# Description:		Bacula is a network backup and restore program
### END INIT INFO


NAME="bacula-dir"
DESC="Bacula Director"
DAEMON=@sbindir@/${NAME}
BUSER=@dir_user@
BGROUP=@dir_group@
BOPTIONS="-c @sysconfdir@/${NAME}.conf"
BPORT=@dir_port@

PATH=/sbin:/bin:/usr/sbin:/usr/bin

test -f $DAEMON || exit 0

#
# Disable Glibc malloc checks, it doesn't help and it keeps from getting
#   good dumps
MALLOC_CHECK_=0
export MALLOC_CHECK_

if [ -n "`getent services ${NAME}`" ]; then
   BPORT=`getent services ${NAME} | awk '{ gsub("/tcp","",$2); print $2; }'`
fi

if [ -f /etc/default/$NAME ]; then
   . /etc/default/$NAME
fi

mkdir -p @piddir@
PIDFILE=@piddir@/${NAME}.${BPORT}.pid

if [ "x${BUSER}" != "x" ]; then
   USERGRP="--chuid ${BUSER}"
   if [ "x${BGROUP}" != "x" ]; then
      USERGRP="${USERGRP}:${BGROUP}"
   fi
fi

case "$1" in
   start)
      echo -n "Starting ${DESC}: "
      start-stop-daemon --start --quiet --pidfile ${PIDFILE} ${USERGRP} --exec ${DAEMON} -- ${BOPTIONS}
      RETVAL=$?
      echo "${NAME}"
      ;;
   stop)
      echo -n "Stopping ${DESC}: "
      start-stop-daemon --oknodo --stop --quiet  ${USERGRP} --exec ${DAEMON} -- ${BOPTIONS}
      RETVAL=$?
      echo "${NAME}"
      ;;
   restart|force-reload)
      $0 stop
      sleep 5
      $0 start
      ;;
   *)
      echo "Usage: /etc/init.d/${NAME} {start|stop|restart|force-reload}" >&2
      exit 1
      ;;
esac
exit $RETVAL
