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

START=99
STOP=02

USE_PROCD=1
PROG=/usr/sbin/ssdpd

log() {
	echo "${@}"|logger -t ssdpd.init -p info
}

validate_ssdpd_ssdp_section()
{
	uci_validate_section ssdpd ssdpd "ssdp" \
		'enabled:bool:true' \
		'debug:bool:false' \
		'ipv6_enabled:bool:false' \
		'socket_path:string' \
		'ttl:uinteger' \
		'interface:string'
}

configure_ssdp()
{
	local enabled ipv6_enabled socket_path ttl interface

	config_load ssdpd

	validate_ssdpd_ssdp_section || {
		log "Validation of ssdp section failed"
		return 1;
	}

	[ ${enabled} -eq 0 ] && return 0

	mkdir -p /tmp/ssdp/description/

	procd_set_param command ${PROG}

	if [ ${ipv6_enabled} -eq 1 ]; then
		procd_append_param command -6
	fi

	if [ ${debug} -eq 1 ]; then
		procd_append_param command -d
	fi

	if [ -n "${socket_path}" ]; then
		procd_append_param command -s ${socket_path}
	fi

	if [ -n "${ttl}" ]; then
		procd_append_param command -t ${ttl}
	fi

	# If no interface is given defaults for br-lan
	procd_append_param command -i ${interface:-br-lan}
}

start_service() {
	procd_open_instance ssdp
	configure_ssdp
	procd_set_param respawn
	procd_close_instance
}

reload_service() {
	stop
	start
}

service_triggers()
{
	procd_add_reload_trigger "ssdpd"
}
