#!/bin/ash

[ -z "`pidof -s Xorg`" ] && exit 0
grep -qm 1 "Using input driver 'libinput'" "/var/log/Xorg.${DISPLAY#:}.log" || exit 0

. /root/.libinputrc

export DISPLAY=:0

xinput list --id-only | while read NAME; do
	case "$LIBINPUT_DEFAULT_TAP" in
	0) xinput set-prop "$NAME" "libinput Tapping Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Tapping Enabled" 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_DRAG" in
	0) xinput set-prop "$NAME" "libinput Tapping Drag Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Tapping Drag Enabled" 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_NATURAL_SCROLL" in
	0) xinput set-prop "$NAME" "libinput Natural Scrolling Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Natural Scrolling Enabled" 1 ;;
	esac

	[ -n "$LIBINPUT_DEFAULT_ACCELERATION" ] && xinput set-prop "$NAME" "libinput Accel Speed" "$LIBINPUT_DEFAULT_ACCELERATION"

	case "$LIBINPUT_DEFAULT_ACCELERATION_PROFILE" in
	1) xinput set-prop "$NAME" "libinput Accel Profile Enabled" 0 1 ;;
	2) xinput set-prop "$NAME" "libinput Accel Profile Enabled" 1 0 ;;
	esac

	case "$LIBINPUT_DEFAULT_SCROLL_METHOD" in
	0) xinput set-prop "$NAME" "libinput Scroll Method Enabled" 0 0 0 ;;
	1) xinput set-prop "$NAME" "libinput Scroll Method Enabled" 1 0 0 ;;
	2) xinput set-prop "$NAME" "libinput Scroll Method Enabled" 0 1 0 ;;
	4) xinput set-prop "$NAME" "libinput Scroll Method Enabled" 0 0 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_CLICK_METHOD" in
	0) xinput set-prop "$NAME" "libinput Click Method Enabled" 0 0 ;;
	1) xinput set-prop "$NAME" "libinput Click Method Enabled" 1 0 ;;
	2) xinput set-prop "$NAME" "libinput Click Method Enabled" 0 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_DISABLE_WHILE_TYPING" in
	0) xinput set-prop "$NAME" "libinput Disable While Typing Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Disable While Typing Enabled" 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_MIDDLE_EMULATION" in
	0) xinput set-prop "$NAME" "libinput Middle Emulation Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Middle Emulation Enabled" 1 ;;
	esac

	case "$LIBINPUT_DEFAULT_LEFT_HANDED" in
	0) xinput set-prop "$NAME" "libinput Left Handed Enabled" 0 ;;
	1) xinput set-prop "$NAME" "libinput Left Handed Enabled" 1 ;;
	esac
done

[ -d /usr/lib64/xorg ] && LIB=lib64 || LIB=lib
if [ ! -f /usr/$LIB/xorg/modules/input/synaptics_drv.so ]; then
	TOUCHPAD="$(xinput list | grep -P '(?<= )[\/\w\s:]*(?i)(touchpad|trackpad|synaptics)(?-i).*?(?=\s*id)' -o | head -n1)"
	if [ "$TOUCHPAD" ]; then
		if [ "$LIBINPUT_DEFAULT_TOUCHPAD_ON" = "1" ]; then
			xinput enable "$TOUCHPAD"
		else
			xinput disable "$TOUCHPAD"
		fi
	fi
fi
