#! /bin/sh
#

### BEGIN INIT INFO
# Provides:          rustdesk-hbbs
# Required-Start:    $local_fs $remote_fs $syslog $named $network $time
# Required-Stop:     $local_fs $remote_fs $syslog $named $network
# Should-Start:
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop the RustDesk HBBS server
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/hbbs
NAME=rustdesk-hbbs
DESC="RustDesk Signal Server (HBBS)"
PIDFILE="/run/$NAME.pid"

OPTIONS=
WORKDIR="/var/lib/rustdesk-server"
LOGDIR="/var/log/rustdesk-server"

test -x $DAEMON || exit 0

if ! [ -x "/lib/lsb/init-functions" ]; then
	. /lib/lsb/init-functions
else
	echo "E: /lib/lsb/init-functions not found, lsb-base (>= 3.0-6) needed"
	exit 1
fi

# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
	. /etc/default/$NAME
fi

# recreate dirs if needed
for dir in $WORKDIR $LOGDIR
do
	[ ! -d "$dir" ] || {
		mkdir -p "$dir"
		##chown XXX "$WORKDIR"
	}
done

set -e

case "$1" in
  start)
	if [ "$INETD" = 1 ]; then
		exit 0
	fi
	log_daemon_msg "Starting $DESC" "$NAME"
	cd "$WORKDIR"
	$DAEMON $OPTIONS >"$LOGDIR/$NAME.log" 2>"$LOGDIR/$NAME.err" &
	echo $$ >$PIDFILE
	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --retry 15
	log_end_msg $?
	;;
  reload|force-reload)
	log_daemon_msg "Reloading $DESC configuration files" "$NAME"
	start-stop-daemon --stop --signal HUP --quiet --pidfile $PIDFILE
	log_end_msg $?
	;;
  status)
    status_of_proc -p $PIDFILE "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
  restart)
	$0 stop
	sleep 1
	$0 start
	;;
  *)
	log_failure_msg "Usage: $N {start|stop|restart|reload|force-reload}" 
	exit 1
	;;
esac

exit 0
