#!/bin/sh -e

##########################################################################
#   Title:
#       Optional, defaults to the name of the script sans extention
#
#   Name:
#       script - short description
#
#   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
	cat << EOM

There are two touchpad drivers for MacBooks.

"wsp" is the new driver, with added features like two-finger tap.  It is
likely the better choice for most hardware currently in use.  It may not
work on very old hardware, though.

"atp" is the old driver, which may not work at all on newer hardware.

EOM
	driver=''
	while [ 0"$driver" != 0wsp ] && [ 0"$driver" != 0atp ]; do
	    printf "Driver to use? [wsp] "
	    read driver
	    if [ -z "$driver" ]; then
		driver=wsp
	    fi
	done
	auto-set-conf-var ${driver}_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
