#!/bin/sh

if [ "$(cat /proc/device-tree/model)" = "Anbernic RG ARC-D" ] && [[ ! -f "/usr/lib/udev/rules.d/99-goodix-touchscreen.rules" ]]; then
echo "99-goodix-touchscreen.rules not found! Create one..."
cat > /usr/lib/udev/rules.d/99-goodix-touchscreen.rules << EOF;
ACTION=="add|change", KERNEL=="event*", ATTRS{name}=="gt927", ENV{LIBINPUT_CALIBRATION_MATRIX}="0 -1 1 1 0 0"
EOF
udevadm control --reload-rules && udevadm trigger
elif [ "$(cat /proc/device-tree/model)" != "Anbernic RG ARC-D" ] && [[ -f "/usr/lib/udev/rules.d/99-goodix-touchscreen.rules" ]]; then
echo "99-goodix-touchscreen.rules found on non-compatible device! Removing rule..."
rm -f /usr/lib/udev/rules.d/99-goodix-touchscreen.rules
elif [ "$(cat /proc/device-tree/model)" = "Anbernic RG ARC-D" ] && [[ -f "/usr/lib/udev/rules.d/99-goodix-touchscreen.rules" ]]; then
echo "99-goodix-touchscreen.rules found on compatible device! All OK, exiting"
elif [ "$(cat /sys/devices/virtual/dmi/id/product_name)" = "T8S" ]; then
echo "X86_64 MIG Tablet"
cat > /usr/lib/udev/rules.d/90-goodix-MIG-touchscreen.rules << EOF
ACTION=="add|change", KERNEL=="event*", ATTRS{name}=="GDIX1002:00", ENV{LIBINPUT_CALIBRATION_MATRIX}="-1 0 1 0 -1 1"
EOF
elif [[ -f /usr/lib/udev/rules.d/90-goodix-MIG-touchscreen.rules ]]; then
echo "Udev rule is exist, exiting"
exit 0
fi
exit 0