summaryrefslogtreecommitdiff
blob: 9b6a94192381e4fa2f689dec35cbf74308cbc702 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/sbin/openrc-run
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

RUNDIR=/var/run/sabnzbd

depend() {
    need net
}

get_var() {
    echo $(sed -n \
        '/^\[misc]/,/^'$1'/ s/^'$1' = \([[:alnum:].]\+\)[\r|\n|\r\n]*$/\1/p' \
        "${SABNZBD_CONFIGFILE}")
}

get_port() {
    if [ "$(get_var 'enable_https')" -eq 1 ]; then
        echo $(get_var 'https_port')
    else
        echo $(get_var 'port')
    fi
}

get_addr() {
    local host=$(get_var 'host')
    local protocol='http'

    [ "${host}" == "0.0.0.0" ] && host=localhost
    [ "$(get_var 'enable_https')" -eq 1 ] && protocol='https'

    echo "${protocol}://${host}:$(get_port)"
}

get_pidfile() {
    echo "${RUNDIR}/sabnzbd-$(get_port).pid"
}

start() {
    ebegin "Starting SABnzbd"

    checkpath -q -d -o ${SABNZBD_USER}:${SABNZBD_GROUP} -m 0770 "${RUNDIR}"

    start-stop-daemon \
        --quiet \
        --start \
        --user ${SABNZBD_USER} \
        --group ${SABNZBD_GROUP} \
        --name sabnzbd \
        --background \
        --pidfile "$(get_pidfile)" \
        --exec /usr/bin/sabnzbd \
        -- \
        --config-file "${SABNZBD_CONFIGFILE}" \
        --logging "${SABNZBD_LOGGING}" \
        --daemon \
        --pid "${RUNDIR}"

    eend $?
}

start_pre() {
    if [ "$RC_CMD" == "restart" ]; then
        local pidfile=$(get_pidfile)
        while [ -e ${pidfile} ]; do
            sleep 1
        done
    fi

    return 0
}

stop() {
    local api_key=$(get_var 'api_key')
    local addr=$(get_addr)
    local rc=1

    ebegin "Stopping SABnzbd @ ${addr}"
    # This can only work if we have enabled the API
    if [ -n "${api_key}" -a "$(get_var 'disable_api_key')" -ne 1 ]; then
        local ret
        einfo "Attempting web-based shutdown @ ${addr}"

        # SABnzbd will return "ok" if shutdown is successful
        ret=$(/usr/bin/curl -k -s "${addr}/sabnzbd/api?mode=shutdown&apikey=${api_key}")
        [ "${ret}" == "ok" ] && rc=0
    fi

    if [ "${rc}" -ne 0 ]; then
        einfo "Falling back to SIGTERM, this may not work if you restarted via the web interface"
        start-stop-daemon \
            --stop \
            --pidfile $(get_pidfile) \
            --retry SIGTERM/1/SIGKILL/5
        rc=$?
    fi

    eend ${rc}
}