#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/ipband
NAME=ipband
DESC=ipband

set -e

test -x $DAEMON || exit 0

if [ -r /etc/default/ipband ]; then
    . /etc/default/ipband
else
    printf "/etc/default/ipband is not readable, not starting ipband.\n"
    exit 0
fi

case "$1" in
  start)
        printf "Starting $DESC:"
	for config_file in $CONFIG_FILES; do
	    if [ ! -r "$config_file" ]; then
		[ "$CONFIG" = "true" ] || CONFIG="false"
	    else
		CONFIG="true"
		IDENTIFIER="$(printf "$config_file" | tr / _)"
		start-stop-daemon --start --background --make-pidfile \
		    --quiet --pidfile "/var/run/ipband/$IDENTIFIER.pid" \
		    --exec $DAEMON -- $FLAGS -c "$config_file"
		printf " $config_file"
	    fi
	done
	[ "$CONFIG" = "false" ] && printf " no configuration files found"
	printf ".\n"
	;;
  stop)
	printf "Stopping $DESC: "
	for pid_file in /var/run/ipband/*.pid; do
	    if [ ! -f "$pid_file" ]; then continue; fi
	    start-stop-daemon --oknodo --stop --quiet --pidfile "$pid_file" \
		--exec $DAEMON -- $FLAGS
	done
	printf "$NAME.\n"
	;;
  restart|force-reload)
	$0 stop || true
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
