commit:     975400d5765f7405f611ddf66a3ad717cb21a203
Author:     Michał Kępień <github <AT> kempniu <DOT> pl>
AuthorDate: Wed Oct  5 09:08:55 2016 +0000
Commit:     Justin Bronder <jsbronder <AT> gentoo <DOT> org>
CommitDate: Fri Oct 28 12:57:14 2016 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=975400d5

net-nntp/sabnzbd: Rework init script

 net-nntp/sabnzbd/files/sabnzbd.initd | 130 ++++++++++++++---------------------
 1 file changed, 51 insertions(+), 79 deletions(-)

diff --git a/net-nntp/sabnzbd/files/sabnzbd.initd 
b/net-nntp/sabnzbd/files/sabnzbd.initd
old mode 100644
new mode 100755
index 9b6a941..ad11573
--- a/net-nntp/sabnzbd/files/sabnzbd.initd
+++ b/net-nntp/sabnzbd/files/sabnzbd.initd
@@ -1,99 +1,71 @@
 #!/sbin/openrc-run
-# Copyright 1999-2014 Gentoo Foundation
+# Copyright 1999-2016 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
 
-RUNDIR=/var/run/sabnzbd
+PIDFILE="/run/sabnzbd/sabnzbd.pid"
 
 depend() {
-    need net
+       need net
 }
 
 get_var() {
-    echo $(sed -n \
-        '/^\[misc]/,/^'$1'/ s/^'$1' = \([[:alnum:].]\+\)[\r|\n|\r\n]*$/\1/p' \
-        "${SABNZBD_CONFIGFILE}")
+       grep -P -o -m 1 "(?<=^${1} = ).*" "${SABNZBD_CONFIGFILE}" || echo 0
 }
 
-get_port() {
-    if [ "$(get_var 'enable_https')" -eq 1 ]; then
-        echo $(get_var 'https_port')
-    else
-        echo $(get_var 'port')
-    fi
+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 $?
 }
 
-get_addr() {
-    local host=$(get_var 'host')
-    local protocol='http'
+stop() {
+       local protocol="http"
+       local host="$(get_var "host")"
+       local port="$(get_var "port")"
 
-    [ "${host}" == "0.0.0.0" ] && host=localhost
-    [ "$(get_var 'enable_https')" -eq 1 ] && protocol='https'
+       if [ $(get_var "enable_https") -eq 1 ]; then
+               protocol="https"
+               port="$(get_var "https_port")"
+       fi
 
-    echo "${protocol}://${host}:$(get_port)"
-}
+       case "${host}" in
+               *:*) host="[${host}]" ;;
+       esac
 
-get_pidfile() {
-    echo "${RUNDIR}/sabnzbd-$(get_port).pid"
-}
+       local url="${protocol}://${host}:${port}/sabnzbd/api?mode=shutdown"
 
-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 $?
-}
+       if [ $(get_var "disable_api_key") -eq 0 ]; then
+               url="${url}&apikey=$(get_var "api_key")"
+       fi
 
-start_pre() {
-    if [ "$RC_CMD" == "restart" ]; then
-        local pidfile=$(get_pidfile)
-        while [ -e ${pidfile} ]; do
-            sleep 1
-        done
-    fi
+       local signals="TERM/1/KILL/1"
 
-    return 0
-}
+       ebegin "Stopping SABnzbd"
 
-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}
+       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 $?
 }

Reply via email to