#!/bin/bash
#
# Script used to start and stop the nzbget usenet service
#
# Copyright (C) 2009 orbisvicis <orbisvicis@users.sourceforge.net>
# Copyright (C) 2009-2012 Andrey Prygunkov <hugbug@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
### BEGIN INIT INFO
# Provides:          nzbget
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop nzbget through init
# Description:
#
### END INIT INFO

# --- CONFIGURATION -----------------------------------------------
# Location of the nzbget executable
NZBGET_BINARY="/usr/local/share/nzbget/nzbget"

# Additional options, e. g. config file location:
. /etc/default/nzbget
NZBGET_OPTS=" -c $CONFIG_FILE"
# -----------------------------------------------------------------

. /lib/lsb/init-functions

test "$ENABLED" = '1' || exit 0
test -x $NZBGET_BINARY || exit 0
set -e

check_retval() {
        if [ $? -eq 0 ]; then
                log_end_msg 0
                return 0
        else
                log_end_msg 1
                exit 1
        fi
}

is_running() {
	PID=$(ps -A | grep '[n]zbget' | awk '{print $1}');
	NRPID=($PID);
        if [ ! -z "$PID" ] && [ ${#NRPID[@]} -gt 3 ]; then
        	kill -0 $PID 2>/dev/null;
                RUNNING=$?;
                if [ $RUNNING -eq 1 ]; then
                	echo 0;
                else
                	echo 1;
                fi
	else
        	echo 0
        fi
}


ACTION=$1;

case "$ACTION" in
	start)
		if [ $(is_running) -eq 0 ]; then
			log_daemon_msg "Starting nzbget"
			nice -n $NICE "$NZBGET_BINARY" $NZBGET_OPTS -D 2>/dev/null 1>/dev/null >/dev/null
			check_retval;
		else
			PID=$(ps -A | grep nzbget | grep -v $$ | awk '{print $1}');
	                log_success_msg "nzbget already running (pid $PID)"
        	fi
		;;
	stop)
		if [ $(is_running) -eq 1 ]; then
			log_daemon_msg "Stopping nzbget"
			"$NZBGET_BINARY" $NZBGET_OPTS -Q 2>/dev/null 1>/dev/null >/dev/null
			sleep 10;
			check_retval;
		else
	                log_success_msg "nzbget not running"
	        fi
		;;
	restart)
                if [ $(is_running) -eq 1 ]; then
                        log_daemon_msg "Stopping nzbget"
                        "$NZBGET_BINARY" $NZBGET_OPTS -Q 2>/dev/null 1>/dev/null >/dev/null
			sleep 10
                        check_retval;
                else
                        log_success_msg "nzbget not running"
                fi

                if [ $(is_running) -eq 0 ]; then
                        log_daemon_msg "Starting nzbget"
                        nice -n $NICE "$NZBGET_BINARY" $NZBGET_OPTS -D 2>/dev/null 1>/dev/null >/dev/null
                        check_retval;
                else
                        PID=$(ps -A | grep nzbget | grep -v $$ | awk '{print $1}');
                        log_success_msg "nzbget already running (pid $PID)"
               	fi
		;;
	status)
		"$NZBGET_BINARY" $NZBGET_OPTS -L S
		;;
	pstatus)
		retval=$(pgrep -l -f nzbget  > /dev/null ; echo $?)
		if [ "$retval" = "0" ] ; then
			echo "        ------- nzbget *is* running -------"
			ps -Ho user,pid,cmd:32,pcpu -C nzbget
			exit 0
		else
			echo "        ------- nzbget is *not* running -------"
			exit 0
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status|pstatus|usage}"
		exit 1
esac

exit 0
