#!/bin/sh
#
# Utility for touchpads. Written by Nightflier for VLocity Linux 6.0

# look for touchpad
if [ "$(grep -i touchpad /proc/bus/input/devices)" ];then checkpad=exists;fi
if [ "$(grep -i glidepad /proc/bus/input/devices)" ];then checkpad=exists;fi
# check xorg.conf touchpad Section entry
if [ "$(grep 'Touchpad' /etc/X11/xorg.conf | grep Identifier)" ];then checkxorg=configured;fi
# check xorg.conf ServerLayout entry
if [ "$(grep 'Touchpad' /etc/X11/xorg.conf | grep InputDevice)" ];then checkdriver=loaded;fi

if [ -z $checkpad ];then 
 echo NO TOUCHPAD FOUND
 exit
 else echo Found device: $(grep -i pad /proc/bus/input/devices)
fi

if [ -z $checkxorg ];then
read -n 1 -p "Write section to xorg.conf? Y/n " YesNo
 if [ "$YesNo" == "y" ] || [ "$YesNo" == "Y" ];then
  # add touchpad section in xorg.conf right above the Monitor section
  sed -i '/Section "Monitor"/ i\Section "InputDevice"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Identifier    "Touchpad"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Driver        "synaptics"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "SendCoreEvents"    "true"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "Device"            "/dev/psaux"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "Protocol"          "auto-dev"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "SHMconfig"         "true"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "HorizScrollDelta"  "0"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\EndSection' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/{x;p;x;}' /etc/X11/xorg.conf
  echo 'Added InputDevice section'
 fi
fi

if [ -z $checkdriver ];then
 read -n 1 -p "Activate Synaptics driver? Y/n " YesNo
 if [ "$YesNo" == "y" ] || [ "$YesNo" == "Y" ];then
  # add line in section ServerLayout
  sed -i '/X.org Configured/ a\InputDevice    "Touchpad"' /etc/X11/xorg.conf
  echo 'Added InputDevice Touchpad'
 fi
fi

if [ $checkdriver ];then
 read -n 1 -p "Deactivate Synaptics driver? Y/n " YesNo
 if [ "$YesNo" == "y" ] || [ "$YesNo" == "Y" ];then
  # remove line from section ServerLayout
  sed -i '/InputDevice    "Touchpad"/d' /etc/X11/xorg.conf
  echo 'Removed InputDevice Touchpad'
 fi
fi
