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

USE_PROCD=1

START=95
STOP=01

CONFIGURATION=netstinky

validate_ids_section()
{
    uci_load_validate netstinky netstinky "$1" "$2" \
            'interface:string:lan' \
            'port:uinteger:8089' \
            'updatehost:string' \
            'updateport:uinteger' \
            'sslnoverify:bool:false'
}

netstinky_instance()
{
    [ "$2" = 0 ] || {
        echo "validation failed"
        return 1
    }

    procd_open_instance
    procd_set_param command /usr/bin/nsids

    if [ -n "${interface}" ]; then
        local iface
        network_get_device iface "${interface}"
        procd_append_param command -i "${iface}"
    fi
    procd_append_param command -p "${port}"
    [ -n "${updatehost}" ] && procd_append_param command --update-host "${updatehost}"
    [ -n "${updateport}" ] && procd_append_param command --update-port "${updateport}"
    [ "${sslnoverify}" -eq 1 ] && procd_append_param command --ssl-no-verify

    procd_set_param stderr 1
    procd_set_param stdout 1

    procd_add_mdns "netstinky" "tcp" "$port"

    procd_close_instance
}

start_service()
{
    . /lib/functions/network.sh

    config_load "${CONFIGURATION}"
    config_foreach validate_ids_section netstinky netstinky_instance
}

service_triggers()
{
    procd_add_config_trigger "config.change" "netstinky" /etc/init.d/nsids reload

    config_load "${CONFIGURATION}"

    procd_add_validation validate_ids_section
}

