#!/sbin/openrc-run
# SPDX-License-Identifier: MIT

description="Firmware update daemon to periodically check URLs"

command="/usr/libexec/swupdate-url"
pidfile="/run/swupdate_url.pid"

# options (set variables in /etc/conf.d/swupdate-url)
# schedule: date -d formatted time specification for start of next occurence,
#           e.g. '0 tomorrow' or 'next week' (default: '0 tomorrow')
# rdelay: time up until which script can sleep, in seconds (default: 6h)
# urlfile: file where update urls to check are stored (default: /etc/swupdate.watch)
# startup: also check on boot (default: no). Note this isn't needed with state file.
# statefile: where to remember next schedule target.
#             default /var/log/swupdate/swupdate-url.state

depend() {
	need localmount net
	after ntp-client
}

error() {
	eend 1 "$@"
	exit 1
}

start() {
	local schedule="${schedule:-0 tomorrow}"
	local rdelay="${rdelay:-21600}"
	local urlfile="${urlfile:-/etc/swupdate.watch}"
	local startup="${startup:-}"
	local statefile="${statefile-/var/log/swupdate/swupdate-url.state}"

	ebegin "Starting swupdate-url polling service"

	set -- --schedule "$schedule" --rdelay "$rdelay" --urlfile "$urlfile" --state "$statefile"
	case "$startup" in
	[Yy]|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) set -- "$@" -O;;
	esac

	start-stop-daemon --start --quiet --exec "${command}" \
		--background --make-pidfile --pidfile="$pidfile" \
		-- "$@"

	eend $? "Could not start swupdate-url"
}
