#!/bin/sh
#
# Ian - 16/11/2011
# /etc/init.d/newznab: start and stop the newznab update script
#
# run update-rc.d newznab_ubuntu.sh defaults
### BEGIN INIT INFO
# Provides: Newznab
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start newznab at boot time
# Description: Enable newznab service provided by daemon.
### END INIT INFO
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
NORMAL=$(tput sgr0)
col=20
# Newznab variables
NN_PATH="/var/www/misc/update_scripts"
NN_BINUP="update_binaries.php"
NN_RELUP="update_releases.php"
NN_SLEEP_TIME="10" # in seconds . 10sec is good for 100s of groups. 600sec might be a good start for fewer.
NN_PID_PATH="/var/run/"
SCREEN_NAME="newznab"
PGREP_SEARCH="SCREEN -dmS $SCREEN_NAME"
PIDFILE="newznab.pid"
PRETTY_NAME="Newznab binaries update"
test -f /lib/lsb/init-functions || exit 1
. /lib/lsb/init-functions
do_start() {
if pgrep -f "$PGREP_SEARCH" > /dev/null
then
echo "$PRETTY_NAME is already running.";
return 1;
fi
echo -n "Starting $PRETTY_NAME ... "
screen -dmS ${SCREEN_NAME} /var/www/misc/update_scripts/nix_scripts/newznab_local.sh &
PID=`echo $!`
echo $PID > ${NN_PID_PATH}${PIDFILE}
sleep 1
if pgrep -f "$PGREP_SEARCH" > /dev/null
then
printf '%s%*s%s\n' "$GREEN" $col '[OK]' "$NORMAL"
else
printf '%s%*s%s\n' "$RED" $col '[FAILED]' "$NORMAL"
fi
}
do_stop() {
echo -n "Stopping Newznab binaries update ... "
if pgrep -f "$PGREP_SEARCH" > /dev/null
then
kill -9 `pgrep -f "$PGREP_SEARCH"`
screen -wipe
fi
printf '%s%*s%s\n' "$GREEN" $col '[OK]' "$NORMAL"
}
do_status() {
if pgrep -f "$PGREP_SEARCH" > /dev/null
then
echo "$PRETTY_NAME is running."
else
echo "$PRETTY_NAME is not running."
fi
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
status)
do_status
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 [start|stop|status|restart]"
exit 1
esac