#!/bin/bash
# change touchpad manager (synaptics or libinput)

# define current synaptics status
[ -d /usr/lib64/xorg ] && LIB=lib64 || LIB=lib
export LIB=$LIB
[ -f /usr/$LIB/xorg/modules/input/synaptics_drv.so ] && SYNAPTICS_ACTIVE="true" || SYNAPTICS_ACTIVE=""

# select touchpad manager
if [ "$SYNAPTICS_ACTIVE" = "true" ]; then
 CURRENT_TOUCHPAD_MANAGER="Synaptics"
 REPLY=`Xdialog --stdout --title " $(gettext 'Touchpad Manager') " --backtitle " $(gettext 'Changing the touchpad manager requires rebooting!') \n\n $(gettext 'Close all applications now, select a touchpad manager') \n $(gettext 'and click the Reboot button below (Save if prompted)') " --no-tags --cancel-label "$(gettext 'Cancel')" --ok-label "$(gettext 'Reboot') " --radiolist "$(gettext 'Active:') $CURRENT_TOUCHPAD_MANAGER" 0 0 4 "1" "$(gettext 'Synaptics Touchpad Manager')" "off"  "2" "$(gettext 'Libinput Touchpad Manager')" "off" `
 if [ "$REPLY" = "1" ]; then # Synaptics Touchpad Manager
		if [ "$(which psynclient)" ]; then
			psynclient -l
			#psynclient autostart
			echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=psynclient
Exec=psynclient -l' > $HOME/.config/autostart/psynclient.desktop
			#psynclient menu entry
			echo '[Desktop Entry]
Encoding=UTF-8
Name=Touchpad manager
Icon=/usr/share/pixmaps/psynclient.svg
Comment=Configure your synaptics touchpad
Exec=psynclient
Terminal=false
Type=Application
Categories=X-Setup-wizard
GenericName=psynclient' > /usr/share/applications/psynclient.desktop
			#libinput mouse control (xset mouse control disabled by libinput)
			echo 'LIBINPUT_DEFAULT_TAP=1
LIBINPUT_DEFAULT_NATURAL_SCROLL=0
LIBINPUT_DEFAULT_ACCELERATION=0.5
LIBINPUT_DEFAULT_ACCELERATION_PROFILE=2
LIBINPUT_DEFAULT_SCROLL_METHOD=0
LIBINPUT_DEFAULT_CLICK_METHOD=0
LIBINPUT_DEFAULT_DRAG=0
LIBINPUT_DEFAULT_DISABLE_WHILE_TYPING=0
LIBINPUT_DEFAULT_MIDDLE_EMULATION=0
LIBINPUT_DEFAULT_LEFT_HANDED=0
LIBINPUT_DEFAULT_TOUCHPAD_ON=0' > $HOME/.libinputrc
			/usr/sbin/xinput-apply 2>/dev/null
			[ ! -f /usr/$LIB/xorg/modules/input/synaptics_drv.so ] && cp -af $HOME/.config/synaptics/synaptics_drv.so_bak /usr/$LIB/xorg/modules/input/synaptics_drv.so
			sleep 2
			Xdialog --title "$(gettext 'Rebooting')" --msgbox "   $(gettext 'Pausing before reboot... \n\n   Please confirm all desktop applications are closed !')   " 0 0
			wmreboot
		else
			Xdialog --title "$(gettext 'psynclient')" --msgbox "$(gettext 'psynclient not found !') " 0 0
			input-wizard &
			exit 1
		fi
 elif [ "$REPLY" = "2" ]; then # Libinput Touchpad Manager
		gtkdialog-splash -close never -timeout 2 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing, please wait...') " &
		rm -f $HOME/.config/autostart/psynclient.desktop /usr/share/applications/psynclient.desktop 2>/dev/null
		rm -f /usr/$LIB/xorg/modules/input/synaptics_drv.so 2>/dev/null
		#libinput base settings
		echo 'LIBINPUT_DEFAULT_TAP=1
LIBINPUT_DEFAULT_NATURAL_SCROLL=0
LIBINPUT_DEFAULT_ACCELERATION=0.5
LIBINPUT_DEFAULT_ACCELERATION_PROFILE=2
LIBINPUT_DEFAULT_SCROLL_METHOD=0
LIBINPUT_DEFAULT_CLICK_METHOD=0
LIBINPUT_DEFAULT_DRAG=0
LIBINPUT_DEFAULT_DISABLE_WHILE_TYPING=0
LIBINPUT_DEFAULT_MIDDLE_EMULATION=0
LIBINPUT_DEFAULT_LEFT_HANDED=0
LIBINPUT_DEFAULT_TOUCHPAD_ON=1' > $HOME/.libinputrc
		/usr/sbin/xinput-apply 2>/dev/null
		sleep 2
		Xdialog --title "$(gettext 'Rebooting')" --msgbox "   $(gettext 'Pausing before reboot... \n\n   Please confirm all desktop applications are closed !')   " 0 0
		wmreboot
 else
	input-wizard &
	exit 0
 fi
else
 CURRENT_TOUCHPAD_MANAGER="Libinput"
 REPLY=`Xdialog --stdout --title " $(gettext 'Touchpad Manager') " --backtitle " $(gettext 'Changing the touchpad manager requires rebooting!') \n\n $(gettext 'Close all applications now, select a touchpad manager') \n $(gettext 'and click the Reboot button below (Save if prompted)') " --no-tags --cancel-label "$(gettext 'Cancel')" --ok-label "$(gettext 'Reboot') " --radiolist "$(gettext 'Active:') $CURRENT_TOUCHPAD_MANAGER" 0 0 4 "1" "$(gettext 'Libinput Touchpad Manager')" "off"  "2" "$(gettext 'Synaptics Touchpad Manager')" "off" `
 if [ "$REPLY" = "1" ]; then # Libinput Touchpad Manager
		gtkdialog-splash -close never -timeout 2 -margin 10 -placement center -fontsize large -bg goldenrod -text " $(gettext 'Processing, please wait...') " &
		rm -f $HOME/.config/autostart/psynclient.desktop /usr/share/applications/psynclient.desktop 2>/dev/null
		rm -f /usr/$LIB/xorg/modules/input/synaptics_drv.so 2>/dev/null
		#libinput base settings
		echo 'LIBINPUT_DEFAULT_TAP=1
LIBINPUT_DEFAULT_NATURAL_SCROLL=0
LIBINPUT_DEFAULT_ACCELERATION=0.5
LIBINPUT_DEFAULT_ACCELERATION_PROFILE=2
LIBINPUT_DEFAULT_SCROLL_METHOD=0
LIBINPUT_DEFAULT_CLICK_METHOD=0
LIBINPUT_DEFAULT_DRAG=0
LIBINPUT_DEFAULT_DISABLE_WHILE_TYPING=0
LIBINPUT_DEFAULT_MIDDLE_EMULATION=0
LIBINPUT_DEFAULT_LEFT_HANDED=0
LIBINPUT_DEFAULT_TOUCHPAD_ON=1' > $HOME/.libinputrc
		/usr/sbin/xinput-apply 2>/dev/null
		sleep 2
		Xdialog --title "$(gettext 'Rebooting')" --msgbox "   $(gettext 'Pausing before reboot... \n\n   Please confirm all desktop applications are closed !')   " 0 0
		wmreboot
 elif [ "$REPLY" = "2" ]; then # Synaptics Touchpad Manager
		if [ ! -f /usr/$LIB/xorg/modules/input/synaptics_drv.so ]; then
			if [ -f $HOME/.config/synaptics/synaptics_drv.so_bak ]; then
				cp -af $HOME/.config/synaptics/synaptics_drv.so_bak /usr/$LIB/xorg/modules/input/synaptics_drv.so
			else
				Xdialog --title "$(gettext 'synaptics')" --msgbox "$(gettext 'synaptics driver not found !') " 0 0
				input-wizard &
				exit 1
			fi
		fi
		if [ "$(which psynclient)" ]; then
			#psynclient autostart
			echo '[Desktop Entry]
Encoding=UTF-8
Type=Application
NoDisplay=true
Name=psynclient
Exec=psynclient -l' > $HOME/.config/autostart/psynclient.desktop
			#psynclient menu entry
			echo '[Desktop Entry]
Encoding=UTF-8
Name=Touchpad manager
Icon=/usr/share/pixmaps/psynclient.svg
Comment=Configure your synaptics touchpad
Exec=psynclient
Terminal=false
Type=Application
Categories=X-Setup-wizard
GenericName=psynclient' > /usr/share/applications/psynclient.desktop
			#libinput mouse control (xset mouse control disabled by libinput)
			echo 'LIBINPUT_DEFAULT_TAP=1
LIBINPUT_DEFAULT_NATURAL_SCROLL=0
LIBINPUT_DEFAULT_ACCELERATION=0.5
LIBINPUT_DEFAULT_ACCELERATION_PROFILE=2
LIBINPUT_DEFAULT_SCROLL_METHOD=0
LIBINPUT_DEFAULT_CLICK_METHOD=0
LIBINPUT_DEFAULT_DRAG=0
LIBINPUT_DEFAULT_DISABLE_WHILE_TYPING=0
LIBINPUT_DEFAULT_MIDDLE_EMULATION=0
LIBINPUT_DEFAULT_LEFT_HANDED=0
LIBINPUT_DEFAULT_TOUCHPAD_ON=0' > $HOME/.libinputrc
			/usr/sbin/xinput-apply 2>/dev/null
			sleep 2
			Xdialog --title "$(gettext 'Rebooting')" --msgbox "   $(gettext 'Pausing before reboot... \n\n   Please confirm all desktop applications are closed !')   " 0 0
			wmreboot
		else
			Xdialog --title "$(gettext 'psynclient')" --msgbox "$(gettext 'psynclient not found !') " 0 0
			input-wizard &
			exit 1
		fi
 else
	input-wizard &
	exit 0
 fi
fi

### END ###
