#!/bin/sh /etc/rc.common
# Copyright (C) 2014-2016 OpenWrt.org

START=90
STOP=10
USE_PROCD=1

append_arg() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local def="$4"
	local val

	config_get val "$cfg" "$var"
	[ -n "$val" -o -n "$def" ] && procd_append_param command $opt "${val:-$def}"
}

append_bool() {
	local cfg="$1"
	local var="$2"
	local opt="$3"
	local def="$4"
	local val

	config_get_bool val "$cfg" "$var" "$def"
	[ "$val" = 1 ] && procd_append_param command "$opt"
}

start_instance() {
	local cfg="$1"
	local aux

	config_get_bool aux "$cfg" 'disabled' '0'
	[ "$aux" = 1 ] && return 1

	procd_open_instance

	procd_set_param command /usr/bin/dump1090
	procd_append_param command "--quiet"

	append_arg "$cfg" device_index "--device-index"
	append_arg "$cfg" gain "--gain"
	append_bool "$cfg" enable_agc "--enable-agc"
	append_arg "$cfg" freq "--freq"
	append_arg "$cfg" ifile "--ifile"
	append_arg "$cfg" iformat "--iformat"
	append_bool "$cfg" throttle "--throttle"
	append_bool "$cfg" raw "--raw"
	append_bool "$cfg" net "--net"
	append_bool "$cfg" modeac "--modeac"
	append_bool "$cfg" net_only "--net-only"
	append_arg "$cfg" net_bind_address "--net-bind-address"
	append_arg "$cfg" net_ri_port "--net-ri-port"
	append_arg "$cfg" net_ro_port "--net-ro-port"
	append_arg "$cfg" net_sbs_port "--net-sbs-port"
	append_arg "$cfg" net_bi_port "--net-bi-port"
	append_arg "$cfg" net_bo_port "--net-bo-port"
	append_arg "$cfg" net_ro_size "--net-ro-size"
	append_arg "$cfg" net_ro_interval "--net-ro-interval"
	append_arg "$cfg" net_heartbeat "--net-heartbeat"
	append_arg "$cfg" net_buffer "--net-buffer"
	append_bool "$cfg" net_verbatim "--net-verbatim"
	append_bool "$cfg" forward_mlat "--forward-mlat"
	append_arg "$cfg" lat "--lat"
	append_arg "$cfg" lon "--lon"
	append_arg "$cfg" max_range "--max-range"
	append_bool "$cfg" fix "--fix"
	append_bool "$cfg" no_fix "--no-fix"
	append_bool "$cfg" no_crc_check "--no-crc-check"
	append_bool "$cfg" phase_enhance "--phase-enhance"
	append_bool "$cfg" mlat "--mlat"
	append_bool "$cfg" stats "--stats"
	append_bool "$cfg" stats_range "--stats-range"
	append_arg "$cfg" stats_every "--stats-every"
	append_bool "$cfg" onlyaddr "--onlyaddr"
	append_bool "$cfg" metric "--metric"
	append_bool "$cfg" gnss "--gnss"
	append_arg "$cfg" snip "--snip"
	append_arg "$cfg" debug "--debug"
	append_arg "$cfg" ppm "--ppm"
	append_arg "$cfg" html_dir "--html-dir"
	append_arg "$cfg" write_json "--write-json"
	config_get aux "$cfg" "write_json"
	[ -n "$aux" ] && mkdir -p $aux
	append_arg "$cfg" write_json_every "--write-json-every"
	append_arg "$cfg" json_location_accuracy "--json-location-accuracy"
	append_bool "$cfg" oversample "--oversample"
	append_bool "$cfg" dcfilter "--dcfilter"
	append_bool "$cfg" measure_noise "--measure-noise"

	config_get_bool aux "$cfg" 'respawn' '0'
	[ "$aux" = 1 ] && procd_set_param respawn

	procd_close_instance
}

service_triggers() {
	procd_add_reload_trigger "dump1090"
}

start_service() {
	config_load dump1090
	config_foreach start_instance dump1090
}