#!/bin/bash
#
#    Copyright (c) 1999-2013 "Diego Vicentini"
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This script is based on
# https://git.gnome.org/browse/gnome-settings-daemon/tree/plugins/common/input-device-example.sh
#
# Set the script to be used with:
# gsettings set org.gnome.settings-daemon.peripherals.input-devices hotplug-command /path/to/script/mouse-usb-manager
# 
# Uninstall script with:
# gsettings reset org.gnome.settings-daemon.peripherals.input-devices hotplug-command /path/to/script/mouse-usb-manager
#
#
#
MATCH="DualPoint"

args=`getopt "t:i:w:" $*`

set -- $args

while [ $# -gt 0 ]
do
    case $1 in
    -t)
        shift;
        type="$1"
        ;;
     -i)
        shift;
        id="$1"
        ;;
     -w)
        shift;
        wait="$1"
        ;;
     --)
        shift;
        device="$@"
        break;
        ;;
    *)
        echo "Unknown option $1"; >> /tmp/input.log
        exit 1
        ;;
    esac
    shift
done

retval=0

LOG=/dev/null
#LOG=/tmp/input-$(id -u).log

STORE=/tmp/.mouse-$(id -u)

if [ -n "$wait" ]; then
	echo "Wait $wait seconds" >> $LOG
	sleep $wait
fi

case $type in
        added)
                echo "$STORE Device '$device' (ID=$id) was added" >> $LOG
		if [[ "$device" =~ .*USB.* ]]; then
			notify-send -t 2 -i notification-device-usb "$device" "$device ($id) was added\nTouchPad will be disabled"
			for dev in $(cat $STORE); do
				xinput set-int-prop $dev "Device Enabled" 8 0  >> $LOG
			done;
		fi
                ;;
        present)
                echo "Device '$device' (ID=$id) was already present at startup" >> $LOG
		if [[ "$device" =~ .*$MATCH.* ]]; then
			if [ -f $STORE ]; then
				cat $STORE > $STORE.tmp
				echo "$id" >> $STORE.tmp
				sort -u $STORE.tmp > $STORE
				rm -f $STORE.tmp
			else
				echo "$id" > $STORE
			fi
		else
			if [[ -z "$wait" && "$device" =~ .*USB.* ]]; then
				$0 -t added -i "$id" -w 5 "$device" >/dev/null &
			fi
		fi
                ;;
        removed)
                echo "Device '$device' (ID=$id) was removed" >> $LOG
		if [[ "$device" =~ .*USB.* ]]; then
			notify-send -t 2 -i notification-device-usb "$device" "$device ($id) was removed\nTouchPad will be enabled"
			for dev in $(cat $STORE); do
				xinput set-int-prop $dev "Device Enabled" 8 1  >> $LOG
			done;
		fi
                ;;
        *)
                # echo "Unknown operation"
                retval=1
                ;;
esac

# All further processing will be disabled if $retval == 1
exit $retval
