#!/bin/sh
#
# Copyright (c) 2021, The NomadBSD Project
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# PROVIDE: load_acpi
# REQUIRE: LOGIN initgfx kld
# KEYWORD: nojail
#
# Add these lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# load_acpi_enable (bool):      Set to YES by default.
#                               Set to NO to disable.
#

. /etc/rc.subr

name=load_acpi
rcvar=load_acpi_enable
start_cmd="load_acpi_start"
stop_cmd=":"

load_rc_config $name

: ${load_acpi_enable:="YES"}

vendors="asus fujitsu hp ibm panasonic sony toshiba wmi"

asus_ptrn="(asus|asustek)"
toshiba_ptrn="(toshiba)"
ibm_ptrn="(ibm|lenovo)"
sony_ptrn="(sony)"
wmi_ptrn="(acer|hewlett-packard)"
hp_ptrn="(hewlett-packard)"
fujitsu_ptrn="(fujitsu)"
panasonic_ptrn="(panasonic)"

asus_kmod="acpi_asus"
toshiba_kmod="acpi_toshiba"
ibm_kmod="acpi_ibm"
sony_kmod="acpi_sony"
wmi_kmod="acpi_wmi"
hp_kmod="acpi_hp"
fujitsu_kmod="acpi_fujitsu"
panasonic_kmod="acpi_panasonic"

# The acpi_video module will not be loaded if the system's model name
# matches one of the patterns. The Fn keys for controlling the brightness
# will not work on some devices if the acpi_video(4) module is loaded.
acpi_video_exclude_patterns()
{
cat << PATTERNS_END
.*ThinkPad.*(T450|T470|x260|x230)
.*EliteBook.*820.*G2
PATTERNS_END
}

match_acpi_video_exclude_patterns()
{
	local device="$(kenv smbios.system.product) $(kenv smbios.system.family)"
	local IFS=$'\n'
	for p in $(acpi_video_exclude_patterns); do
		echo ${device} | egrep -iq $p && return 0
	done
	return 1
}

match_system_maker_patterns()
{
	local maker="$(kenv smbios.system.maker 2>/dev/null)"
	[ -z "${maker}" ] && return

	for i in ${vendors}; do
		eval ptrn=\$${i}_ptrn
		if (echo ${maker} | grep -Eiqw "${ptrn}"); then
			eval kmod=\$${i}_kmod
			kldload -n ${kmod}
		fi
	done
}

load_acpi_start()
{
	match_system_maker_patterns

	if ! match_acpi_video_exclude_patterns; then
		kldload -n acpi_video
	fi
}

run_rc_command "$1"
