#!/bin/sh
### BEGIN INIT INFO
# Provides:          dyfi
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: dyfi-update SysV init script
# Description:       SysV init script for starting and stopping
#                    dyfi-update.pl - A perl client for updating dy.fi
#                    hostnames automatically.
### END INIT INFO

set -e
# Specify the location of the pid file:
PIDFILE=/var/run/dyfi-update.pid
# Specify the location of the configuration file:
CFGFILE=/etc/dyfi-update.conf
# Specify the location of the perl file:
DAEMON=/usr/share/dyfi/dyfi-update.pl

# Get lsb functions
. /lib/lsb/init-functions

# See how we were called.
case "$1" in
  start)
	echo "Starting dy.fi updating daemon..."
	start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- -f $CFGFILE
	[ $? -eq 0 ] && [ -d /var/lock/subsys ] && touch /var/lock/subsys/dyfi-update
        ;;
  stop)
  	if [ -f $PIDFILE ]; then
		echo "Stopping dy.fi updating daemon..."
		kill `cat $PIDFILE`
		if [ $? -eq 0 ]; then
			rm $PIDFILE
			[ -d /var/lock/subsys ] && rm /var/lock/subsys/dyfi-update
		fi
	else
		echo "dy.fi updating daemon is not running (no pid file)"
	fi
  	
	;;
  restart)
	$0 stop
	sleep 1
	$0 start
	;;
  reload|force-reload)
	log_begin_msg "Reloading dy.fi updating daemon..."
	restart
	log_end_msg $?
	;;
  status)
	if [ -f $PIDFILE ]; then
		echo "dy.fi is running"
	else
		echo "dy.fi is not running (no pid file)"
	fi
	;;
  *)
        echo "Usage: dyfi-update {start|stop|restart|reload|force-reload|status}"
        exit 1
esac

exit 0

