#! /bin/sh
#
# bareos  This shell script takes care of starting and stopping
#	  the bareos Director daemon on Debian GNU Hurd systems.
#
#   Kern E. Sibbald - 21 March 2008
#
#
### BEGIN INIT INFO
# Provides:	     bareos-sd
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start @BAREOS@ Storage daemon at boot time
# Description:	     Enable @BAREOS@ Storage daemon.
### END INIT INFO


NAME="bareos-sd"
DESC="@BAREOS@ Storage Daemon"
DAEMON=@sbindir@/${NAME}
BUSER=@sd_user@
BGROUP=@sd_group@
BOPTIONS="-c @confdir@/${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

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 ${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
