#! /bin/sh
#
#	/etc/rc.d/init.d/barserver
#
#	BAR server start script
#	Copyright 2007-2025 Torsten Rupp
#

### BEGIN INIT INFO
# Provides:          barserver
# Required-Start:    $syslog $local_fs $network $remote_fs
# Required-Stop:     $syslog $local_fs $network $remote_fs
# Should-Start:      $time ypbind sendmail
# Should-Stop:       $time ypbind sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Short-Description: BAR server daemon
# Description:       Backup ARchiver server for backup/restore
### END INIT INFO

NAME=bar
BAR_BIN=/usr/bin/$NAME
BAR_CONFIG_FILENAME=/etc/bar/bar.cfg
BAR_PID_FILENAME=/run/$NAME.pid
BAR_ARGS="--config $BAR_CONFIG_FILENAME --daemon --pid-file=$BAR_PID_FILENAME --verbose=0 --quiet"
BAR_SCRIPT_FILENAME=/etc/init.d/barserver
DESCRIPTION="barserver"

# shell functions
if test -f /etc/rc.status; then
  . /etc/rc.status
fi
if test -f /etc/init.d/functions; then
  . /etc/init.d/functions
fi

# check for binaries
if test ! -x $BAR_BIN; then
  echo "$BAR_BIN not installed"
  exit 5
fi

# check for configuration file
if test ! -r $BAR_CONFIG_FILENAME; then
  echo "$BAR_CONFIG_FILENAME cannot be read"
  exit 6
fi

case "$1" in
    start)
	echo -n "Starting BAR server "
	daemon $BAR_BIN $BAR_ARGS
	;;
    stop)
	echo -n "Shutting down BAR server "
	killproc -p $BAR_PID_FILENAME -TERM $BAR_BIN
	;;
    reload)
        failure
	;;
    restart)
	$0 stop
	$0 start
	;;
    status)
	echo -n "Checking for service BAR server: "
	if test ! -f $BAR_PID_FILENAME; then
          echo "stopped"
        elif test ! -d /proc/`cat $BAR_PID_FILENAME`; then
          echo "stopped"
        else
          echo "running"
        fi
	;;
    *)
	echo "Usage: $0 {start|stop|reload|restart|status}"
	exit 1
	;;
esac
