#!/bin/bash

#
# attaches to one of the available USB devices in the machine
# using evtest
#

if [ ! -e "/usr/bin/evtest" ]; then
  echo "Need to install evtest package..."
  apt-get -y install evtest
fi

USER=`env | grep USER= | cut -f2 -d=`

if [ "$USER" = "" ]; then
  echo "Error, can't find USER env variable..."
  exit
fi

num=0

for device in `ls /dev/input/by-id`; do
  num=`expr $num + 1`
  echo "$num - $device"
done

echo ""
echo "USB keyboard to use? "
read var

num=0

for device in `ls /dev/input/by-id`; do
  num=`expr $num + 1`

  if [ "$num" = "$var" ]; then
    if [ -e "/opt/pos/common/addons/read_usb_keys/etc/device2" ]; then
      rm -rf /opt/pos/common/addons/read_usb_keys/etc/device2
    fi

    ps xa | grep read_keys | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    echo "$device" > /opt/pos/common/addons/read_usb_keys/etc/device
    /opt/pos/common/addons/read_usb_keys/bin/read_keys.sh &
    /opt/bin/kill_pos.sh
  fi
done


