#!/bin/sh /etc/rc.common

. $IPKG_INSTROOT/lib/functions/network.sh

START=90
USE_PROCD=1
NAME=tunneldigger-broker

PIDPATH=/var/run
CONFIGFILE=/var/etc/tunneldigger-broker.cfg
HOOKPATH=/usr/lib/tunneldigger-broker/hooks

missing() {
	echo "Not starting tunneldigger-broker - missing $1" >&2
	exit 1
}

cfg_append() {
	local value="$1"
	echo "$value" >> $CONFIGFILE
}

cfg_append_section() {
	local name="$1"
	cfg_append "[${name}]"
}

cfg_append_kv() {
	local key="$1"
	local value="$2"
	cfg_append "${key}=${value}"
}

cfg_append_option() {
	local cfg="$1"
	local option="$2"
	local key="$3"
	config_get value "$cfg" "${option}"
	[ -z "${value}" ] && missing ${option}
	cfg_append_kv "${key}" "${value}"
}

parse_broker() {
	local section="$1"
	cfg_append_section broker		
	config_get interface "$section" interface

	[ ! -z "${interface}" ] && {
		# Resolve logical interface name.
		unset _interface address
		network_get_device _interface "${interface}" || _interface="${interface}"
		cfg_append_kv interface "${_interface}"
		network_get_ipaddr address "${interface}"
		cfg_append_kv address "${address}"
	}

	OPTIONS="max_tunnels tunnel_id_base connection_rate_limit connection_rate_limit_per_ip_count connection_rate_limit_per_ip_time pmtu"
	for option in ${OPTIONS}; do
		cfg_append_option "$section" "${option}" "${option}"
	done

	config_get port "$section" port
	cfg_append_kv "port" "${port// /,}"

	cfg_append_section hooks
	cfg_append_kv "session.up" "${HOOKPATH}/setup"
	cfg_append_kv "session.pre-down" "${HOOKPATH}/teardown"
	cfg_append_kv "session.mtu-changed" "${HOOKPATH}/mtu-changed"
	cfg_append_kv "broker.connection-rate-limit" "${HOOKPATH}/connection-rate-limit"
}

parse_log() {
	local section="$1"
	cfg_append_section log
	
	OPTIONS="verbosity"
	for option in ${OPTIONS}; do
		cfg_append_option "$section" "${option}" "${option}"
	done

	config_get_bool log_ip_addresses "$section" log_ip_addresses 1
	if [ "${log_ip_addresses}" -eq 1 ]; then
		cfg_append_kv "log_ip_addresses" "true"
	else
		cfg_append_kv "log_ip_addresses" "false"
	fi
}

start_service() {
	# Prepare the configuration file from UCI settings.
	rm -f ${CONFIGFILE}
	config_load tunneldigger-broker
	config_foreach parse_broker broker
	config_foreach parse_log log
	
	# Start the service using procd.
	procd_open_instance
	procd_set_param command "/usr/bin/python"
	procd_append_param command -m tunneldigger_broker.main
	procd_append_param command "${CONFIGFILE}"

	# Set up a trigger when the interface changes state
	local netdev
	network_get_physdev netdev $(uci show "tunneldigger-broker.@broker[0].interface" | cut -d \' -f 2)
	procd_set_param netdev $netdev

	procd_set_param respawn
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}

reload_service() {
	restart
}

service_triggers() {
	local interface=$(uci show "tunneldigger-broker.@broker[0].interface" | cut -d \' -f 2)
	procd_add_interface_trigger "interface.*.up" $interface /etc/init.d/tunneldigger-broker restart
	procd_add_reload_trigger "tunneldigger-broker"
}

