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

description="Handle reset button at boot"

valid_duration=30000
press_time=5000

depend() {
	after udev-trigger
}

background_start() {
	# we need to wait for udev to create input file...
	udevadm settle

	if ! [ -e /dev/input/by-path/platform-gpio-keys-event ]; then
		eend 1 "button input file not found"
		exit
	fi

	# close stdio to allow openrc to continue
	exec </dev/null >/dev/null 2>&1

	buttond -i /dev/input/by-path/platform-gpio-keys-event \
		--exit-timeout "$valid_duration" \
		-l PROG1 -t "$press_time" \
		-a 'abos-ctrl reset-default -f &' \
		--exit-after
}

start() {
	ebegin "Starting reset button handler"

	background_start &
}
