#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   Section:
#       8
#
#   Synopsis:
#       
#   Description:
#       
#   Arguments:
#       
#   Returns:
#
#   Examples:
#
#   Files:
#
#   Environment:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2024-09-23  Jason Bacon Add man page template and usage
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $# != 0 ]; then
    usage
fi

case $(auto-ostype) in
FreeBSD)
    mkdir -p /etc/X11/xorg.conf.d
    if dmidecode 2> /dev/null | grep -iq macbook; then
	printf "$0: MacBook\n"
	# https://wiki.freebsd.org/AppleMacbook
	# atp(4) does not work on my MacBook Pro 8,3 (dmidecode)
	# It also appears to be unmaintained (PRs open for years)
	# wsp(4) works flawlessly
	# auto-set-conf-var atp_load '"YES"' /boot/loader.conf $0
	auto-set-conf-var wsp_load '"YES"' /boot/loader.conf $0
	auto-set-conf-var acpi_video_load '"YES"' /boot/loader.conf $0
	auto-set-conf-var asmc_load '"YES"' /boot/loader.conf $0
	
	# Fix ~ and ` keys in /usr/local/share/X11/xkb/symbols/us-macbook
	xkb_dir=/usr/local/share/X11/xkb/symbols
	if [ ! -e $xkb_dir/us-macbook ]; then
	    cp $xkb_dir/us $xkb_dir/us-macbook
	fi
	cat << EOM

If your \` and ~ keys produce < and >, try swapping TLDE and LSGT in
the "mac" section of $xkb_dir/us-macbook.

EOM
	
	printf "Remap right Cmd and Alt keys to Alt and Ctrl? y/[n] "
	read remap
	if [ 0$remap = 0y ]; then
	    aa_dir=/usr/local/etc/auto-admin
	    mkdir -p $aa_dir
	    xsession=$aa_dir/xsession.macbook
	    cat << EOM > $xsession
# Do not edit this file
# auto-generated by $0
# Add additional files to $aa_dir for more customizations
# Run xev to verify Xorg scan codes
# Turn lower right "enter" key on older macs into a Ctrl key
xmodmap -e "keycode 108 = Control_R"
xmodmap -e "add control = Control_R"
    
# Turn right Command key into an Alt (Meta) key
xmodmap -e "keycode 134 = Meta_R Alt_R"
EOM
	    cat << EOM

Config saved to $xsession.
Source this from all Xsession and xinitrc files.

EOM
	fi
	# Swap LSGT and TLDE in /usr/local/share/X11/xkb/symbols/us
	cat << EOM > /etc/X11/xorg.conf.d/macbook-xkb.conf
Section "InputClass"
	Identifier "libinput keyboard catchall"
	MatchIsKeyboard "on"
	MatchDevicePath "/dev/input/event*"
	Driver "libinput"
	Option "XkbRules" "evdev"
	Option "XkbLayout" "us-macbook"
	Option "XkbVariant" "mac"
EndSection
EOM
	chmod 644 /etc/X11/xorg.conf.d/macbook-xkb.conf
    elif dmidecode 2> /dev/null | grep -iq thinkpad; then
	printf "$0: IBM/Lenovo ThinkPad\n"
	# bsdfan works with acpi_ibm to control fan speed based
	# FIXME: Enable in rc.conf?
	auto-set-conf-var acpi_ibm_load '"YES"' /boot/loader.conf $0
	pkg install -y bsdfan
    elif dmidecode 2> /dev/null | grep -iq "Product name: Satellite"; then
	printf "$0: Toshiba Satellite\n"
    fi
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
