#!/bin/sh -e

##########################################################################
#   Synopsis:
#       auto-wacom-setup
#
#   Description:
#       Install and configure software needed for wacom tablets.  Also
#       prompts to enable individual users.
#
#   Arguments:
#       None.
#
#   Returns:
#
#   See also:
#       
#   History:
#   Date        Name        Modification
#   2022-07-12  J Bacon     Begin
##########################################################################

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


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

if [ $# != 0 ]; then
    usage
fi
	
case $(auto-ostype) in
FreeBSD)
    # pkg-message:
    # moused(8) may interfere with tablet input by attaching to fake /dev/ums*.
    # To prevent that add a usb_quirk(4) then detach and attach the tablet.
    # /usr/local/etc/rc.d/wacom is an example how preserve quirks across reboot.

    # https://www.davidschlachter.com/misc/freebsd-webcam-browser
    LOADER_CONF=/boot/loader.conf
    RC_COND=/etc/rc.conf
    pkg install -y webcamd libwacom xf86-input-wacom xorg-server
    
    # Load cuse now and at reboot
    kldload cuse || true
    auto-set-conf-var cuse_load '"YES"' $LOADER_CONF $0

    # Enable and start webcamd
    auto-enable-service webcamd $0

    # Restart devd
    auto-enable-service devd $0
    service devd restart
    
    # Add users to webcamd to allow access to tablet
    user_name='x'
    while [ -n "$user_name" ]; do
	printf "Username to use webcam? (Press Enter to skip) "
	read user_name
	if [ -n "$user_name" ]; then
	    pw groupmod webcamd -m $user_name
	fi
    done
    ;;

*)
    # FIXME: Make auto-unsupported-os return 1?
    auto-unsupported-os $0
    exit 1
    ;;

esac
