#!/bin/sh
# chkconfig: 345 09 91
# description: Plesk Firewall

SERVICE_NAME="psa-firewall"
MODULE="firewall"
PRODUCT_ROOT_D="/usr/local/psa"

action="$1"
conf="$PRODUCT_ROOT_D/var/modules/$MODULE/${MODULE}-active.sh"
active_flag="$PRODUCT_ROOT_D/var/modules/$MODULE/active.flag"
emergency="$PRODUCT_ROOT_D/var/modules/$MODULE/${MODULE}-emergency.sh"

case "$action" in

	start)
		if [ ! -f "$active_flag" ]; then
			echo "$SERVICE_NAME: service is disabled"
			exit 1
		fi
		if [ -f "$conf" ]; then
			if "$conf"; then
				echo "$SERVICE_NAME: firewall configuration successfully applied"
				exit 0
			else
				echo "$SERVICE_NAME: failed to apply firewall configuration"
				exit 1
			fi
		else
			echo "$SERVICE_NAME: firewall configuration not found"
			exit 1
		fi
		;;

	stop)
		if [ ! -f "$active_flag" ]; then
			exit 0
		fi
		if "$emergency"; then
			echo "$SERVICE_NAME: firewall successfully disabled"
			exit 0
		else
			echo "$SERVICE_NAME: failed to disable firewall"
			exit 1
		fi
		;;

	restart|force-reload)
		"$0" stop
		"$0" start
		exit
		;;

	help)
		echo "usage: $0 <arg>"
		echo "where argument is one from the list:"
		cat << EOF
	start       -- start ${SERVICE_NAME}
	stop        -- stop ${SERVICE_NAME}
	restart     -- restart ${SERVICE_NAME} if running or start if not running
	help        -- this screen

EOF
		exit 1
		;;

	*)
		echo "usage: $0 (start|stop|restart|help)"
		exit 1
		;;
esac
