#!/bin/bash

################################################################################
#
# gnome-toggle-touchpad-lock
# Requires notify-send to be installed
#
################################################################################

################################################################################
# User configuration
################################################################################

title="Touchpad Lock"
body_enabled="Touchpad will be locked while typing."
body_disabled="Touchpad will be unlocked while typing."
icon_enabled="touchpad-disabled"
icon_disabled="input-touchpad"
key="org.gnome.desktop.peripherals.touchpad disable-while-typing"

################################################################################
# gnome-toggle code
################################################################################

value=$(gsettings get $key)

notif_id_file_name=/tmp/tmp.$(echo $key | tr -d ' ').nid
{ prev_notif_id=$(<$notif_id_file_name); } 2> /dev/null

replace_flag=""
case $prev_notif_id in
    ''|*[!0-9]*) ;;
    *) replace_flag="--replace-id=$prev_notif_id" ;;
esac

if [[ $value == "true" ]]; then
    gsettings set $key false
    notify-send "$title Disabled" "$body_disabled" --icon $icon_disabled $replace_flag --transient --expire-time=0 --print-id > $notif_id_file_name
else
    gsettings set $key true
    notify-send "$title Enabled" "$body_enabled" --icon $icon_enabled $replace_flag --transient --expire-time=0 --print-id > $notif_id_file_name
fi