#!/bin/bash -e

[ -r /etc/network/simple-firewall.env ] && . /etc/network/simple-firewall.env
[ -r ./simple-firewall.env ] && . ./simple-firewall.env

[ "function" == "$(type -t simple_start)" -a "function" == "$(type -t simple_stop)" ] || {
	echo "Functions simple_start or simple_stop is not defined. Does simple-firewall.env exist?" >&2
	exit 2
}

usage() {
	echo "Usage: $0 [-v] [-h] (start | stop | restart)"
}

VERBOSITY=

while getopts "vh" NAME; do
	case "$NAME" in
		v)
			VERBOSITY=1
			;;
		h)
			usage
			exit 0
			;;
		\?)
			usage >&2
			exit 1
			;;
	esac
done

shift $((OPTIND - 1))

case "$1" in
	start)
		[ -n "$VERBOSITY" ] && echo "Starting firewall..."
		simple_start
		[ -n "$VERBOSITY" ] && echo "Done"
		;;
	stop)
		[ -n "$VERBOSITY" ] && echo "Stoping firewall..."
		simple_stop
		[ -n "$VERBOSITY" ] && echo "Done"
		;;
	restart)
		[ -n "$VERBOSITY" ] && echo "Stoping firewall..."
		simple_stop
		[ -n "$VERBOSITY" ] && echo "Starting firewall..."
		simple_start
		[ -n "$VERBOSITY" ] && echo "Done."
		;;
	*)
		usage >&2
		exit 1
	;;
esac

exit 0
