#!/usr/bin/env bash
set -u

CONFIG_FILE="${1:-/etc/eGTouchL.ini}"

section() {
    printf '\n===== %s =====\n' "$1"
}

have_cmd() {
    command -v "$1" >/dev/null 2>&1
}

read_sys_file() {
    local path="$1"
    if [ -r "$path" ]; then
        tr -d '\000' < "$path"
    else
        printf ''
    fi
}

section "Monitor names for MonitorName"
if have_cmd xrandr; then
    xrandr --query | awk '
        $2 == "connected" {
            mode = ""
            for (i = 3; i <= NF; i++) {
                if ($i ~ /^[0-9]+x[0-9]+\+/) {
                    mode = $i
                    break
                }
            }
            printf "%-16s %s\n", $1, mode
        }'
else
    echo "xrandr not found. Run this script inside the graphical session, or install xrandr."
fi

section "EETI/eGalax USB controllers for Physical_Address"
found_usb=0
for dev in /sys/bus/usb/devices/*; do
    [ -d "$dev" ] || continue
    vendor="$(read_sys_file "$dev/idVendor")"
    [ "${vendor,,}" = "0eef" ] || continue

    found_usb=1
    base="$(basename "$dev")"
    product="$(read_sys_file "$dev/product")"
    manufacturer="$(read_sys_file "$dev/manufacturer")"
    serial="$(read_sys_file "$dev/serial")"
    busnum="$(read_sys_file "$dev/busnum")"
    devnum="$(read_sys_file "$dev/devnum")"

    printf 'Physical_Address candidate: %s\n' "$base"
    printf '  sysfs:        %s\n' "$dev"
    printf '  bus/dev:      %s/%s\n' "${busnum:-?}" "${devnum:-?}"
    printf '  manufacturer: %s\n' "${manufacturer:-unknown}"
    printf '  product:      %s\n' "${product:-unknown}"
    printf '  serial:       %s\n' "${serial:-none}"

    if have_cmd udevadm; then
        props="$(udevadm info --query=property --path="$dev" 2>/dev/null || true)"
        id_path="$(printf '%s\n' "$props" | awk -F= '$1=="ID_PATH"{print $2; exit}')"
        devpath="$(printf '%s\n' "$props" | awk -F= '$1=="DEVPATH"{print $2; exit}')"
        [ -n "$id_path" ] && printf '  ID_PATH:      %s\n' "$id_path"
        [ -n "$devpath" ] && printf '  DEVPATH:      %s\n' "$devpath"
    fi
done

if [ "$found_usb" -eq 0 ]; then
    echo "No USB device with vendor 0eef found under /sys/bus/usb/devices."
fi

section "Input devices matching eGalax/EETI"
if [ -r /proc/bus/input/devices ]; then
    awk '
        BEGIN { RS=""; ORS="\n\n"; IGNORECASE=1 }
        /0EEF|eGalax|EETI|eGTouch|Touch/ { print }
    ' /proc/bus/input/devices
else
    echo "/proc/bus/input/devices is not readable."
fi

section "xinput touch devices"
if have_cmd xinput; then
    xinput list | grep -Ei 'egalax|eeti|egtouch|touch|0eef' || echo "No matching xinput device names found."
else
    echo "xinput not found."
fi

section "Current eGTouchL.ini mapping lines"
if [ -r "$CONFIG_FILE" ]; then
    awk '
        /^\[Device_No\.[0-9]+\]/ { device=$0; print device; next }
        /^(DeviceNums|ScanInterface|Physical_Address|MonitorName|DetectRotation)[[:space:]]*/ { print }
    ' "$CONFIG_FILE"
else
    printf '%s is not readable. Pass another path as the first argument if needed.\n' "$CONFIG_FILE"
fi

section "What to copy into eGTouchL.ini"
cat <<'EOF'
Use the exact xrandr output names for MonitorName, for example:
  MonitorName            HDMI-1
  MonitorName            DP-1

For two USB controllers, use one [Device_No.X] block per controller:
  DeviceNums             2
  ScanInterface          1

Then set Physical_Address from the USB controller candidate shown above.
After editing /etc/eGTouchL.ini, restart the eGTouch service or reboot.
EOF
