#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

FAIL2BAN="/usr/bin/fail2ban-client"

case "$1" in
  start)
    stat_busy "Starting fail2ban"

    [ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban
    [ -f /var/run/fail2ban/fail2ban.pid ] && rm -f /var/run/fail2ban/fail2ban.pid
    $FAIL2BAN start >/dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon fail2ban
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping fail2ban"
    if [ "`$FAIL2BAN stop`" == "Shutdown successful" ]; then
      rm -f /var/run/fail2ban/fail2ban.pid &> /dev/null
      rm_daemon fail2ban
      stat_done
    else
          stat_fail
    fi
    ;;
  restart)
    $0 stop
    sleep 3
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
