#!/bin/bash
#
# Wonderfully hacky script to re-apply mouse settings when mouse is
# hotplugged after switching away the KVM or simply pulling usb cable
# and putting it back.
#
# (It has been said that when faced with multiple possible solutions to a
# problem it is often wise to pick the worst one so as to encourage future
# improvements :-).
#

function set_mouse_props () {
   xinput --set-button-map \
          'Kensington      Kensington Expert Mouse' \
          1 8 3 4 5 6 7 2
   xinput --set-int-prop \
          'Kensington      Kensington Expert Mouse' \
          'Evdev Drag Lock Buttons' 8 2 1
}

set_mouse_props
saw_add="no"
dbus-monitor --system | while read
do
   case "$REPLY" in
   signal*org.freedesktop.Hal.Manager*member=DeviceAdded) saw_add="yes";;
   *string*usb_device_47d_1020_noserial_if0_logicaldev_input*) \
      if [ "$saw_add" = "yes" ]
      then
         set_mouse_props
      fi
      saw_add="no";;
   *) saw_add="no";;
   esac
done
