#! /bin/sh
#
# Copyright (C) 2000-2015 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# bacula  This shell script takes care of starting and stopping
#	  the bacula Director daemon on Debian GNU Hurd systems.
#
#   Kern E. Sibbald - 21 March 2008
#
#  For Bacula release @VERSION@ (@DATE@) -- @DISTNAME@
#
### BEGIN INIT INFO
# Provides:	     bacula-sd
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start @BACULA@ Storage daemon at boot time
# Description:	     Enable @BACULA@ Storage daemon.
### END INIT INFO


NAME="bacula-sd"
DESC="@BACULA@ Storage Daemon"
DAEMON=@sbindir@/${NAME}
BUSER=@sd_user@
BGROUP=@sd_group@
BOPTIONS="-c @sysconfdir@/${NAME}.conf"
BPORT=@sd_port@

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

test -f $DAEMON || exit 0

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

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

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

RETVAL=0
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
      RETVAL=$?
      ;;
   *)
      echo "Usage: /etc/init.d/${NAME} {start|stop|restart|force-reload}" >&2
      exit 1
      ;;
esac
exit $RETVAL
