diff options
Diffstat (limited to 'net-nntp/sabnzbd/files/sabnzbd.initd')
-rw-r--r-- | net-nntp/sabnzbd/files/sabnzbd.initd | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/net-nntp/sabnzbd/files/sabnzbd.initd b/net-nntp/sabnzbd/files/sabnzbd.initd new file mode 100644 index 000000000000..5cfc91b9e70a --- /dev/null +++ b/net-nntp/sabnzbd/files/sabnzbd.initd @@ -0,0 +1,99 @@ +#!/sbin/runscript +# 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} +} |