#!/bin/sh /etc/rc.common
 
USE_PROCD=1
 
START=99
STOP=01

start_date_serial() {
    local section=$1

    config_get ENABLED $section enabled
    [ "$ENABLED" = "0" ] && return 0

    config_get SERIAL_DEV $section device 
    config_get SERIAL_BAUD $section baudrate
    config_get SERIAL_PARITY $section parity
    config_get SERIAL_DATABITS $section databits
    config_get SERIAL_STOPBITS $section stopbits
    config_get SERIAL_DUPLEX $section full_duplex_enabled "0"
    config_get SERIAL_FLOWCTRL $section flowcontrol "none"
    config_get INTERVAL $section interval
    config_get TIME_FORMAT $section time_format
    config_get UTC $section utc

    procd_open_instance
    procd_set_param command /bin/sh "/sbin/date_over_serial"
    procd_append_param command "$SERIAL_DEV" "$SERIAL_BAUD" "$SERIAL_DATABITS" "$SERIAL_PARITY" "$SERIAL_STOPBITS" "$SERIAL_DUPLEX" "$SERIAL_FLOWCTRL"
    procd_append_param command "$INTERVAL" "$TIME_FORMAT" "$UTC"
    procd_set_param respawn ${respawn_threshold:-0} ${respawn_timeout:-1} ${respawn_retry:-0} 
    procd_close_instance
}

start_service() {
    config_load date_over_serial
    config_foreach start_date_serial serial_device
}

service_triggers() {
	procd_add_reload_trigger "date_over_serial"
}
