#!/bin/bash

if [ ! -e "/opt/pos/common/addons/read_usb_keys/etc/device2" ]; then
  echo "Device not configured... run setup2.sh..."
  exit
fi

DEVICE=`cat /opt/pos/common/addons/read_usb_keys/etc/device2`

ps xa | grep process_keys | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

while [ true ]; do
  IFS=$'\n'

  for line in `xinput | sed 's/[^a-zA-Z 0-9\t]//g'`; do
    ID=`echo $line | cut -f2 | sed 's/id//' | sed 's/^[ \t]*//' | sed 's/[ \t]*$//'`
    NAME=`echo $line | cut -f1 | sed 's/^[ \t]*//' | sed 's/[ \t]*$//'`

    if [ "$NAME" = "$DEVICE" ]; then
      xinput test $ID | /opt/pos/common/addons/read_usb_keys/bin/process_keys2.pl
    fi
  done

  sleep 5
done



