#!/bin/sh

TOUCH_DEV1=`xinput --list | grep id= | grep IDEACO | head -1 | cut -f2 -d"=" | cut -f1`
TOUCH_DEV2=`xinput --list | grep id= | grep IDEACO | tail -1 | cut -f2 -d"=" | cut -f1`

# from both input devices, try to guess which one is the touchscreen
IS_DEV1=`xinput --list-props $TOUCH_DEV1 | grep "Abs Pressure"`
IS_DEV2=`xinput --list-props $TOUCH_DEV2 | grep "Abs Pressure"`

TOUCH_DEV=0

if [ "$IS_DEV1" != "" ]; then
  TOUCH_DEV=$TOUCH_DEV1
elif [ "$IS_DEV2" != "" ]; then
  TOUCH_DEV=$TOUCH_DEV2
fi

if [ "$TOUCH_DEV" = "0" ]; then
  xmessage "CANT FIND TOUCH DEVICE!"
  exit
fi

xinput_calibrator --device $TOUCH_DEV --output-type xinput > /tmp/cal
cat /tmp/cal | grep "xinput set-int-prop" > /tmp/cal2
cat /tmp/cal | grep "xinput set-int-prop" >> /tmp/cal2
perl -pi -e 's/".*?"/\$TOUCH_DEV/' /tmp/cal2
/opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
killall Xorg

