#!/usr/bin/execlineb -P
# htpdate in foreground (-F), matching debian/sv/htpdate.

ifelse { test -f /etc/default/htpdate } {
	envfile /etc/default/htpdate
	importas -uD "" HTP_DAEMON HTP_DAEMON
	importas -sCuD "-s" HTP_OPTIONS HTP_OPTIONS
	importas -sCuD "" HTP_PROXY HTP_PROXY
	importas -sCuD "" HTP_SERVERS HTP_SERVERS
	envfile -I /etc/s6-rc/config/htpdate.conf
	importas -sCuD "-F" Args Args

	ifelse { test "${HTP_DAEMON}" = no } {
		s6-pause
	} {
		# Drop accidental -D from defaults (daemonize); -F is required for s6.
		backtick -n opts {
			pipeline { echo ${HTP_OPTIONS} } { sed "s/-D//g" }
		}
		importas -sCu opts opts
		fdmove -c 2 1
		exec /usr/sbin/htpdate ${Args} ${opts} ${HTP_PROXY} ${HTP_SERVERS}
	}
}
s6-pause
