#!/bin/sh
########################################################################
# Begin $rc_base/init.d/miniupnpd
#
# Description : Miniupnp daemon
#
# Authors     : Michael Tremer <michael.tremer@ipfire.org>
#
########################################################################

. /etc/sysconfig/rc
. ${rc_functions}

if [ ! -e /etc/miniupnpd/id ]; then
	uuidgen > /etc/miniupnpd/id
fi

UUID=$(cat /etc/miniupnpd/id)

EXT_DEV=$(cat /var/ipfire/red/iface)
EXT_IP4=$(cat /var/ipfire/red/local-ipaddress)

. /var/ipfire/ethernet/settings

for i in GREEN_ADDRESS BLUE_ADDRESS; do
	[ -n "${!i}" ] && LISTEN_IP="${LISTEN_IP} -a ${!i}"
done

function flush_iptables() {
	# Flush iptables to remove all entries that were left
	iptables -F UPNPFW
	iptables -t nat -F UPNPFW
}

case "${1}" in
	start)
		boot_mesg "Starting miniupnpd..."

		flush_iptables

		loadproc miniupnpd -f /etc/miniupnpd/miniupnpd.conf \
			-i ${EXT_DEV} -o ${EXT_IP4} ${LISTEN_IP} \
			-u ${UUID}
		;;

	stop)
		boot_mesg "Stopping miniupnpd..."
		killproc miniupnpd

		flush_iptables
		;;

	restart)
		${0} stop
		sleep 1
		${0} start
		;;

	status)
		statusproc miniupnpd
		;;

	*)
		echo "Usage: ${0} {start|stop|restart|status}"
		exit 1
		;;
esac

# End $rc_base/init.d/miniupnpd
