#!/bin/sh
#
# Copyright (C) 2008-2024 by Jeroen Ploemen <linux@jcf.pm>
# released under GPL, version 2 or later

################################################
#                                              #
#  TO CONFIGURE EDIT /etc/default/sabnzbdplus  #
#                                              #
################################################

### BEGIN INIT INFO
# Provides:          sabnzbdplus
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:      $named dbus avahi network-manager wicd
# Should-Stop:       $named dbus avahi network-manager wicd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: SABnzbd+ binary newsgrabber
# Description:       SABnzbd+ is a web-based binary newsgrabber with nzb
#                    support, designed to download files from Usenet.
#                    This script provides that functionality as a system
#                    service, starting the program on boot.
### END INIT INFO

NAME="sabnzbdplus"
DAEMON="/usr/bin/sabnzbdplus"
SETTINGS="/etc/default/$NAME"
SERVICE_FUNCTIONS="/usr/lib/sabnzbdplus/service_functions.sh"

([ -x "$DAEMON" ] && [ -r "$SETTINGS" ] && [ -r "$SERVICE_FUNCTIONS" ]) || exit 0

DESC="SABnzbd+ binary newsgrabber"
PIDFILE="/var/run/$NAME/pid"
DEFOPTS="--daemon --pidfile \"$PIDFILE\""

# these are only accepted from the settings file
unset USER CONFIG HOST PORT EXTRAOPTS

[ -r /etc/default/locale ] && {
	. /etc/default/locale;
	[ -n "$LANG" ] && export LANG;
}
export PYTHONIOENCODING=utf-8

. /lib/lsb/init-functions
. "$SERVICE_FUNCTIONS"

is_running() {
	# don't try to match --name: it changes after restarting via the web interface
	start-stop-daemon --status --quiet --user "${USER%:*}" --pidfile "$PIDFILE"
	RET=$?
	[ $RET -gt 3 ] && exit 1 || return $RET
}

load_settings() {
	if [ -z "$USER" ]; then
		. "$SETTINGS"

		[ -z "${USER%:*}" ] && {
			log_action_msg "$DESC: service not configured, edit $SETTINGS";
			return 1;
		}

		parse_settings
		OPTIONS="$DEFOPTS $OPTIONS"
	fi
	return 0
}

start_sab() {
	load_settings || exit 0
	if ! is_running; then
		install --directory --owner="${USER%:*}" --group=root --mode=0700 "$(dirname "$PIDFILE")" || exit 1
		log_daemon_msg "Starting $DESC"
		eval start-stop-daemon --start --quiet --chuid "$USER" --exec "$DAEMON" -- $OPTIONS
		log_end_msg $? || exit $?
	else
		log_action_msg "$DESC: already running (pid $(cat "$PIDFILE"))"
	fi
}

stop_sab() {
	load_settings || exit 0
	if is_running; then
		log_daemon_msg "Stopping $DESC"
		start-stop-daemon --stop --quiet --user "${USER%:*}" --pidfile "$PIDFILE" --retry 30
		log_end_msg $? || exit $?
	else
		log_action_msg "$DESC: not running"
	fi
}

case "$1" in
	start)
		start_sab
	;;
	stop)
		stop_sab
	;;
	force-reload|restart|try-restart)
		[ "$1" = 'try-restart' ] && {
			(load_settings && is_running) || exit 0
		}
		stop_sab
		start_sab
	;;
	status)
		load_settings || exit 4
		if is_running; then
			log_action_msg "$DESC: running (pid $(cat "$PIDFILE"))"
		else
			log_action_msg "$DESC: not running"
			[ -f "$PIDFILE" ] && exit 1 || exit 3
		fi
	;;
	*)
		echo "Usage: $0 {start|stop|restart|force-reload|status}" >&2
		exit 3
	;;
esac

exit 0
