#!/bin/sh
### BEGIN INIT INFO
# Provides:          backuppc
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Launch backuppc server
# Description:       Launch backuppc server, a high-performance,
#                    enterprise-grade system for backing up PCs.
### END INIT INFO
# DESCRIPTION
#
#   Startup init script for BackupPC on Debian.
#
# Distributed with BackupPC version 4.4.0, released 20 Jun 2020.
#

set -e

#
BINDIR=__INSTALLDIR__/bin
DATADIR=__TOPDIR__
RUNDIR=__RUNDIR__
USER=__BACKUPPCUSER__
#
NAME=backuppc
DAEMON=BackupPC

test -x $BINDIR/$DAEMON || exit 0

if [ ! -d $RUNDIR ]; then
    mkdir -p $RUNDIR
    chown $USER $RUNDIR
    chmod 0755 $RUNDIR
fi

case "$1" in
  start)
    echo -n "Starting $NAME: "
    start-stop-daemon --start --pidfile $RUNDIR/BackupPC.pid \
			    -c $USER --exec $BINDIR/$DAEMON -- -d
    echo "ok."
    ;;
  stop)
    echo -n "Stopping $NAME: "
    start-stop-daemon --stop --pidfile $RUNDIR/BackupPC.pid -u $USER \
			    --oknodo --retry 30 -x /usr/bin/perl
    echo "ok."
      ;;
  restart)
    echo -n "Restarting $NAME: "
    start-stop-daemon --stop --pidfile $RUNDIR/BackupPC.pid -u $USER \
			    --oknodo --retry 30 -x /usr/bin/perl
    start-stop-daemon --start --pidfile $RUNDIR/BackupPC.pid \
			    -c $USER --exec $BINDIR/$DAEMON -- -d
    echo "ok."
    ;;
  reload|force-reload)
    echo "Reloading $NAME configuration files"
    start-stop-daemon --stop --pidfile $RUNDIR/BackupPC.pid \
			    --signal 1 -x /usr/bin/perl
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload}"
    exit 1
    ;;
esac

exit 0
