#!/usr/sbin/openrc-run

description="Advanced Configuration and Power Interface daemon"

command="/usr/sbin/acpid"
pidfile="/run/acpid.pid"

extra_started_commands="reload"

depend() {
	need localmount
	use logger
	after rsyslog
}

load_modules() {
	local mod

	[ -f /proc/modules ] || return 0
	if [ "${MODULES:-}" = "all" ]; then
		MODULES="$(sed -rn 's#^(/lib/modules/[^/]+/)?kernel/(drivers|ubuntu)/acpi/([^/]+/)*(.*)\.ko:.*#\4#p' "/lib/modules/$(uname -r)/modules.dep")"
	fi

	if [ -z "${MODULES:-}" ]; then
		return 0
	fi

	ebegin "Loading ACPI kernel modules"
	unset MODPROBE_OPTIONS
	modprobe --all --use-blacklist $MODULES 2>/dev/null
	eend $?
}

start() {
	[ -x "$command" ] || return 1
	[ -r /etc/default/acpid ] && . /etc/default/acpid
	load_modules || true

	ebegin "Starting ${RC_SVCNAME}"
	start-stop-daemon -S -p "$pidfile" \
		-x "$command" -- ${OPTIONS:-}
	eend $?
}

stop() {
	ebegin "Stopping ${RC_SVCNAME}"
	start-stop-daemon -K -p "$pidfile"
	eend $?
}

reload() {
	ebegin "Reloading acpid configuration"
	start-stop-daemon -K -s HUP -p "$pidfile"
	eend $?
}
