#!/bin/sh /etc/rc.common
#
# Copyright (C) 2019 Mister Benjamin <144dbspl@gmail.com>

NAME='etherwake-nfqueue'

START=60
USE_PROCD=1

PROGRAM=${NAME}

start_service()
{
    local value

    config_load ${NAME}

    config_get_bool value setup sudo 0
    [ "${value}" -ne 0 ] && PROGRAM="sudo ${PROGRAM}"

    config_get_bool value setup debug 0
    if [ "${value}" -ne 0 ]; then
        PROCD_DEBUG=1
        append PROGRAM '-D'
    fi

    config_foreach start_instance target
}

start_instance()
{
    local section="$1"
    local value name mac

    config_get_bool value "${section}" enabled 1
    [ "${value}" -ne 1 ] && return 0

    config_get value "${section}" name
    [ -z "${value}" ] && value="{section}"
    name=${value}

    config_get mac "${section}" mac
    [ -z "${mac}" ] && {
        echo "${initscript}: Target ${name} has no MAC address"
        return 1
    }

    procd_open_instance ${name}
    procd_set_param command ${PROGRAM}
    procd_set_param respawn
    procd_set_param stdout 1
    procd_set_param stderr 1

    config_get_bool value "${section}" broadcast 0
    [ "${value}" -ne 0 ] && procd_append_param command -b

    config_get value "${section}" interface
    [ -n "${value}" ] && procd_append_param command -i "${value}"

    config_get value "${section}" password
    [ -n "${value}" ] && procd_append_param command -p "${value}"

    config_get value "${section}" nfqueue_num 0
    procd_append_param command -q "${value}"

    procd_append_param command "${mac}"

    procd_close_instance
}
