#! /bin/bash
### BEGIN INIT INFO
# Provides:          auto_dl
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Auto DL, RSS media downloader daemon
# Description:       Auto DL downloads user defined media files based on RSS feed.
#
### END INIT INFO
#########################################################################################
#
# System-V-init.
#
# © Copyright 2012 Arto Jääskeläinen <temp001(at)pp.inet.fi>
# Part of Auto DL software package. All rights reserved.
# The program is distributed under the terms of the GNU General Public License
#
#    Auto DL 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 3 of the License, or
#    (at your option) any later version.
#
#    Auto DL 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 Auto DL.  If not, see <http://www.gnu.org/licenses/>.
#
#
# V1.0 2012-07-27
#
#########################################################################################

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
DESC="Auto DL, RSS feed based media downloader daemon"
NAME=autodld
DAEMON=/opt/auto_dl/auto_dld
DAEMON_ARGS=""
PIDFILE=/var/run/auto_dld.pid
SCRIPTNAME=/etc/init.d/$NAME

[ -x "$DAEMON" ] || exit 0
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started

if [ -z "$(pgrep "auto_dld")" ]; then
	$DAEMON
else
	echo "No action, $NAME running already"
	return 1
fi
if [ "$?" = 0 ]; then
	echo "$NAME running"
else
	echo "Failed to start"
	return 2
fi
}

do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred


if [ -n "$(pgrep auto_dld)" ]; then 
	killall -q auto_dld
	if [ "$?" = 0 ]; then
	echo "$NAME stopped"
	else
	echo "Failed to stop $NAME"
	return 2
	fi
else
	echo "$NAME was not running"; return 1
fi	
	rm -f $PIDFILE
	return 0
}

idle()
{
time=10	
for (( i=$time; i>0; i-- ))
do
	echo -ne "\rPreparing for restart...$i s   "
	sleep 1
done
echo -ne "\r                           \r"	
}


if [[ $EUID -ne 0 ]]; then
   echo "Only root can do that.";
   exit 1;
fi
case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	do_start
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	do_stop
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
       status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
       ;;
  
  restart)
	if [ -n "$(pgrep auto_dld)" ]; then
		do_stop
		echo -n "Preparing to restart..."
		idle
	fi
	do_start
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|restart}" 
	exit 3
	;;
esac

:
