#!/bin/sh

# /etc/rc.d/rc.wicd
# Start/stop/restart the Wicd daemon

# This is defined in /usr/lib/python2.6/site-packages/wicd/wpath.py
PIDFILE="/var/run/denyhosts.pid"
DAEMON="/usr/bin/denyhosts.py"

# Define start and stop functions

denyhosts_start() {
  if [ -e $PIDFILE ]; then
    echo "denyhosts appears to already be running."
    echo "If this is not the case, then remove "
    echo "$PIDFILE and try again..."
    exit 1
  else
    echo "Starting denyhosts daemon: $DAEMON &"
    $DAEMON -c /etc/denyhosts/denyhosts.cfg --daemon 2>/dev/null 1>&2 &
  fi
}

denyhosts_force_stop() {
  echo "Stopping denyhosts daemon..."
  if [ -e $PIDFILE ]; then
    kill -9  $(cat $PIDFILE) 2>/dev/null
    rm -f $PIDFILE 2>/dev/null
  else
    pkill -f denyhosts.py 2>/dev/null
  fi
}

denyhosts_status() {
	if [ -e $PIDFILE ]; then
		echo "denyhosts service is running"
	else
		echo "denyhosts service is not running"
	fi
}

denyhosts_stop() {
  echo "Stopping denyhosts daemon ..."
  if [ -e $PIDFILE ]; then
	  kill -9 $(cat $PIDFILE) > /dev/null 2>&1
	  rm -f $PIDFILE 2>/dev/null
  else
	  pkill -f denyhosts.py 2>/dev/null
  fi
  rm -rf $PIDFILE >/dev/null
}


# See how we were called and take appropriate action	

case $1 in
  start)
    denyhosts_start
   ;;
  status)
    denyhosts_status
   ;;
  stop)
    denyhosts_stop
   ;;
  force_stop)
    denyhosts_force_stop
   ;;
  restart)
    denyhosts_force_stop
    denyhosts_start
   ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
   ;;
esac

