summaryrefslogtreecommitdiff
blob: ad11573f5498bb74fc01c1ee0cc8d142ac3bf4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/sbin/openrc-run
# Copyright 1999-2016 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

PIDFILE="/run/sabnzbd/sabnzbd.pid"

depend() {
	need net
}

get_var() {
	grep -P -o -m 1 "(?<=^${1} = ).*" "${SABNZBD_CONFIGFILE}" || echo 0
}

start() {
	ebegin "Starting SABnzbd"

	checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "$(dirname "${PIDFILE}")"

	start-stop-daemon \
		--quiet \
		--start \
		--user ${SABNZBD_USER} \
		--group ${SABNZBD_GROUP} \
		--pidfile "${PIDFILE}" \
		--wait 1000 \
		--exec /usr/bin/sabnzbd \
		-- \
		--config-file "${SABNZBD_CONFIGFILE}" \
		--logging "${SABNZBD_LOGGING}" \
		--daemon \
		--pidfile "${PIDFILE}"

	eend $?
}

stop() {
	local protocol="http"
	local host="$(get_var "host")"
	local port="$(get_var "port")"

	if [ $(get_var "enable_https") -eq 1 ]; then
		protocol="https"
		port="$(get_var "https_port")"
	fi

	case "${host}" in
		*:*) host="[${host}]" ;;
	esac

	local url="${protocol}://${host}:${port}/sabnzbd/api?mode=shutdown"

	if [ $(get_var "disable_api_key") -eq 0 ]; then
		url="${url}&apikey=$(get_var "api_key")"
	fi

	local signals="TERM/1/KILL/1"

	ebegin "Stopping SABnzbd"

	if [ "$(wget -q -t 1 -O - -T 10 "${url}")" = "ok" ]; then
		signals="CONT/5/${signals}"
	fi

	start-stop-daemon \
		--stop \
		--pidfile "${PIDFILE}" \
		--retry "${signals}"

	eend $?
}