#!/bin/sh

#set -x
mode=$1

if [ "$UID" != "0" ]; then
    echo "You must be root to run this script"
    exit 0
fi

if [ -z `which ufw` ]; then
	echo "UFW not found"
	exit 1
fi

workaround()
{
	mkdir ~/.ufw-tmp
	label=`/usr/sbin/pdpl-file /etc/ufw/ufw.conf`
	if ! [ -z $label ]; then
		/usr/sbin/pdpl-file $label ~/.ufw-tmp
	fi
	export TMPDIR=~/.ufw-tmp
}

workaround_cleanup()
{
	rm -rf ~/.ufw-tmp
}

if [ "$mode" = "enable" ]; then
	workaround
	ufw enable
	workaround_cleanup

elif [ "$mode" = "disable" ]; then 
	workaround
	ufw disable
	workaround_cleanup

elif [ "$mode" = "status" ]; then 
	if [ "`ufw status`" = "Status: active" ]; then
		echo "ACTIVE";
		exit 0;
	else
		echo "INACTIVE";
		exit 1;
	fi

else
	echo "Usage: astra-ufw-control <enable/disable/status>"
	exit 0
fi
exit 0
