#!/bin/sh

### BEGIN INIT INFO
# Provides:		miniupnpd
# Required-Start:	$remote_fs
# Required-Stop:	$remote_fs
# Should-Start:		$local_fs $network $time
# Should-Stop:		$local_fs $network $time
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	daemon providing UPnP Internet Gateway Device (IGD) services
# Description:		MiniUPnPd is a small daemon providing UPnP Internet Gateway
#			Device (IGD) services to your network. UPnP and NAT-PMP are
#			used to improve internet connectivity for devices behind a
#			NAT router. Any peer to peer network application such as
#			games, IM, etc. can benefit from a NAT router supporting
#			UPnP and/or NAT-PMP.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DAEMON_NAME="MiniUPnPd"
DAEMON_SERVICE_NAME="UPnP devices daemon"
DAEMON=/usr/sbin/miniupnpd
PIDFILE=/var/run/miniupnpd.pid
CONFFILE=/etc/miniupnpd/miniupnpd.conf
DEFAULT=/etc/default/miniupnpd
SCRIPT_DIR=/etc/miniupnpd

. /lib/lsb/init-functions

if [ -r /lib/init/vars.sh ] ; then
	. /lib/init/vars.sh
fi

read_config () {
	sed -rn '
s|^\s*'"${2}"'='\''([^'\'']+)'\''\s*$|\1|g
t hold
s|^\s*'"${2}"'='\"'([^'\"']+)'\"'\s*$|\1|g
t hold
s|^\s*'"${2}"'=(\S+)\s*$|\1|
t hold
b
: hold
p
	' "${1}"
}

# Make sure the package hasn't been removed but not purged
if ! [ -x ${DAEMON} ] ; then
	exit 0
fi

if [ -f "${DEFAULT}" ] ; then
	. "${DEFAULT}"
else
	log_daemon_msg "${DAEMON_NAME}: Default file not found: exiting"
	log_end_msg 1

	exit 0
fi

if [ -f "${CONFFILE}" ] ; then
	MiniUPnPd_EXTERNAL_INTERFACE=$(read_config "${CONFFILE}" ext_ifname)
	#~ MiniUPnPd_LISTENING_IP=$(read_config "${CONFFILE}" listening_ip)
else
	log_daemon_msg "${DAEMON_NAME}: Config file not found: exiting"
	log_end_msg 1

	exit 0
fi

if [ -z "${MiniUPnPd_EXTERNAL_INTERFACE}" ] ; then
	log_daemon_msg "${DAEMON_NAME}: no interface defined: exiting"
	log_end_msg 1

	exit 0
fi

#~ if [ -z "${MiniUPnPd_LISTENING_IP}" ] ; then
	#~ log_daemon_msg "${DAEMON_NAME}: no listening IP defined: exiting"
	#~ log_end_msg 1

	#~ exit 0
#~ fi

case "${1}" in
	start)
		if [ "${START_DAEMON}" = 0 ] ; then
			log_daemon_msg "${DAEMON_NAME}: ${DEFAULT} isn't set to START_DAEMON=1: exiting"
			log_end_msg 1

			exit 0
		fi

		log_daemon_msg "Initializing ${DAEMON_SERVICE_NAME} iptables" "${DAEMON_NAME}"

		"${SCRIPT_DIR}/iptables_init.sh" -i "${MiniUPnPd_EXTERNAL_INTERFACE}"
		if [ "${MiniUPnPd_ip6tables_enable}" = 1 ] ; then
			"${SCRIPT_DIR}/ip6tables_init.sh" -i "${MiniUPnPd_EXTERNAL_INTERFACE}"
		fi

		start-stop-daemon -q --start --exec "${DAEMON}" -- ${MiniUPnPd_OTHER_OPTIONS}
		RET="${?}"

		case "${RET}" in
			0|1)
				log_end_msg 0
				;;

			*)
				log_end_msg 1

				exit 1
				;;
		esac
		;;

	stop)
		log_daemon_msg "Stopping ${DAEMON_SERVICE_NAME}" "${DAEMON_NAME}"

		start-stop-daemon -q --stop --oknodo --pidfile ${PIDFILE}
		RET="${?}"

		log_daemon_msg "Deconfiguring ${DAEMON_SERVICE_NAME} iptables" "${DAEMON_NAME}"

		"${SCRIPT_DIR}/iptables_removeall.sh" -i "${MiniUPnPd_EXTERNAL_INTERFACE}"
		if [ "${MiniUPnPd_ip6tables_enable}" = 1 ] ; then
			"${SCRIPT_DIR}/ip6tables_removeall.sh" -i "${MiniUPnPd_EXTERNAL_INTERFACE}"
		fi

		case "${RET}" in
			0|1)
				log_end_msg 0
				;;

			*)
				log_end_msg 1

				exit 1
				;;
		esac
		;;

	restart|force-reload)
		${0} stop
		case "$?" in
			0|1)
				${0} start
				case "$?" in
					0) log_end_msg 0 ;;
					1) log_end_msg 1 ;; # Old process is still running
					*) log_end_msg 1 ;; # Failed to start
				esac
				;;
			*)
				# Failed to stop
				log_end_msg 1
				;;
		esac
		;;

	status)
		status_of_proc "${DAEMON}" "${DAEMON_NAME}"
		exit ${?}
		;;

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

exit 0
