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

USE_PROCD=1
START=99
STOP=15

append_arg() {
	local cfg="$1"
	local opt="$2"
	local var="$3"
	local def="$4"
	local val
	config_get val "$cfg" "$var"
	[ -n "$val" ] || [ -n "$def" ] && procd_append_param command "$opt" "${val:-$def}"
}

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

	config_get_bool enabled "$cfg" 'enabled' 1
	[ "$enabled" -gt 0 ] || return 0

	procd_open_instance
	procd_set_param command /usr/bin/ttymidi-sysex
	append_arg "$cfg" -b baudrate
	append_arg "$cfg" -n name
	append_arg "$cfg" -s device
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	config_load ttymidi && config_foreach start_instance ttymidi
}

service_triggers() {
	procd_add_reload_trigger ttymidi
}
