#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=49
USE_PROCD=1
PROG=/usr/sbin/htpdate




validate_htpdate_section() {
	uci_load_validate htpdate htpdate "$1" "$2" \
		'server:list(host)' \
		'proxy_host:host' \
		'proxy_port:port:8080' \
		'debug:bool:0' \
		'sanity_check:bool:1' \
		'option:list(string)' \
		'enabled:bool:1'
}


start_htpdate_instance() {
	local peer

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

	[ "$enabled" = 0 ] && return


	procd_open_instance
	procd_set_param command "$PROG" -f

	[ -n "$proxy_host" ] && procd_append_param command -P $proxy_host:$proxy_port
	[ "$debug" = "1" ] && procd_append_param command -d
	[ "$sanity_check" = "0" ] && procd_append_param command -t
	[ -n "$option" ] && procd_append_param command $option
	for peer in $server; do
		procd_append_param command $peer
	done

	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	validate_htpdate_section htpdate start_htpdate_instance
}

service_triggers() {
	procd_add_reload_trigger "htpdate"
	procd_add_validation validate_htpdate_section
}
