#!/bin/bash
# Set the touchpad state.

# Usage
usage () { echo "${0##*/} [--off] [--on] — set the touchpad state" ; }
#[ $# -eq 0 -o $# -gt 1 ] || [ "$1" != --off -o "$1" != --on ] && usage && exit 1
[ $# -eq 1 -a "$1" = --off -o "$1" = --on ] || { usage ; exit 1 ; }

# X.org server process name
xorgprc=Xorg
xorgpid=$(pgrep -x $xorgprc)

# X.org server test if running
if [ ! "$xorgpid" ]; then
  echo "X.org server is not running."
  exit 1
fi

# Display variable (makes assumption that only one DISPLAY is running)
display=$(xargs -0 < /proc/$xorgpid/cmdline | grep " :[0-9] "| grep -o ":[0-9]")
[ "$display" ] || { echo "Display variable un-obtainable."; exit 1; }

# Username variable (extremely guessy)
user=$(w | grep " $display " | awk '{ print $1 }')
[ "$user" ] || { echo "User variable un-obtainable."; exit 1; }

# Xauthority location
if pgrep -x gdm > /dev/null; then
  xauthority=$(find /var/run/gdm -name $user -print -quit)
else
  xauthority=/home/$user/.Xauthority
fi
[ -f "$xauthority" ] || { echo "Xauthority variable un-obtainable."; exit 1; }

syndcmdfile=/tmp/touchpad-state-syndaemon.cmdline
case $1 in
  --off )
    # syndaemon disable if running
    if syndpid=$(pgrep -x syndaemon | head -n 1); then
      # record command line options and arguments.
      sed 's/\x00/ /g' /proc/$syndpid/cmdline > "$syndcmdfile"
      chmod a+w "$syndcmdfile"
      killall syndaemon
    fi

   # touchpad disable
    if [ "$user" = $USER ]; then
        DISPLAY=$display XAUTHORITY=$xauthority bash -c '\
        if [ $(gsettings get org.ukui.peripherals-touchpad disable-on-external-mouse) = false ];then exit 1; fi
        gsettings set org.ukui.peripherals-touchpad touchpad-enabled false
        notify-send --hint=int:transient:1 "Touchpad disabled."' &
    else
      DISPLAY=$display XAUTHORITY=$xauthority su $user - -c '\
        if [ $(gsettings get org.ukui.peripherals-touchpad disable-on-external-mouse) = false ];then exit 1; fi
        gsettings set org.ukui.peripherals-touchpad touchpad-enabled false
        notify-send --hint=int:transient:1 "Touchpad disabled."' &
    fi ;;
  --on )
    # syndaemon run if run previously and not running now
      if [ -f "$syndcmdfile" ] && ! pgrep -x syndaemon > /dev/null; then
        $(< "$syndcmdfile") -d
        rm -f "$syndcmdfile"
      fi

    # touchpad enable
    if [ "$user" = $USER ]; then
        DISPLAY=$display XAUTHORITY=$xauthority bash -c '\
        if [ $(gsettings get org.ukui.peripherals-touchpad disable-on-external-mouse) = false ];then exit 1; fi
        gsettings set org.ukui.peripherals-touchpad touchpad-enabled true
        notify-send --hint=int:transient:1 "Touchpad enabled."' &
    else
        DISPLAY=$display XAUTHORITY=$xauthority su $user - -c '\
        if [ $(gsettings get org.ukui.peripherals-touchpad disable-on-external-mouse) = false ];then exit 1; fi
        gsettings set org.ukui.peripherals-touchpad touchpad-enabled true
        notify-send --hint=int:transient:1 "Touchpad enabled."' &
    fi ;;
  * ) usage && exit
esac

# vim: set ft=sh ts=2 sw=2 et:
