#!/bin/bash
# 03000-mouse-synaptics.sh
# Prefer synaptics for obvious touchpads

XORG_CONF="$1"

[ -n "$XORG_CONF" ] || exit 2
[ -f "$XORG_CONF" ] || exit 2
[ -r /proc/bus/input/devices ] || exit 1

EVENT_DEV="$(awk '
BEGIN { RS=""; FS="\n" }
$0 ~ /SynPS\/2/ || $0 ~ /TouchPad/ || $0 ~ /Touchpad/ || $0 ~ /ALPS/ || $0 ~ /Synaptics/ {
    for (i = 1; i <= NF; i++) {
        if ($i ~ /^H: Handlers=/ && match($i, /event[0-9]+/)) {
            print "/dev/input/" substr($i, RSTART, RLENGTH)
            exit
        }
    }
}' /proc/bus/input/devices)"

[ -n "$EVENT_DEV" ] || exit 1
[ -e "$EVENT_DEV" ] || exit 1

logger -t retrodebian-xorg-autoconfig "Configuring Synaptics touchpad: $EVENT_DEV"

sed -i \
    -e "s|{InputDeviceDriver}|synaptics|g" \
    -e "s|{InputDeviceDevice}|$EVENT_DEV|g" \
    -e "s|{InputDeviceProtocol}|auto-dev|g" \
    "$XORG_CONF" || exit 2

exit 0