#!/bin/sh
set -e

#
# Example initscript for ufw
#

PATH="/sbin:/bin"
config_prefix="/etc"

[ -d /lib/ufw ] || exit 0

for s in "/lib/ufw/ufw-init-functions" "$config_prefix/ufw/ufw.conf" ; do
    if [ -s "$s" ]; then
        . "$s"
    else
        echo "Could not find $s (aborting)"
        exit 1
    fi
done

error=0
case "$1" in
start)
    if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
        echo "Starting firewall:" "ufw"
        output=`ufw_start` || error="$?"
        if [ ! -z "$output" ]; then
            /bin/echo -e "$output"
        fi
    else
        echo "Skip starting firewall:" "ufw (not enabled)"
    fi
    exit $error
    ;;
stop)
    if [ "$ENABLED" = "yes" ] || [ "$ENABLED" = "YES" ]; then
        echo "Stopping firewall:" "ufw"
        output=`ufw_stop` || error="$?"
        if [ ! -z "$output" ]; then
            /bin/echo -e "$output"
        fi
    else
        echo "Skip stopping firewall:" "ufw (not enabled)"
    fi
    exit $error
    ;;
restart|force-reload)
    echo "Reloading firewall:" "ufw"
    output=`ufw_reload` || error="$?"
    if [ ! -z "$output" ]; then
        /bin/echo -e "$output"
    fi
    exit $error
    ;;
status)
    output=`ufw_status` || error="$?"
    if [ ! -z "$output" ]; then
        /bin/echo -e "$output"
    fi
    exit $error
    ;;
*)
    echo "Usage: /etc/init.d/ufw {start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0

