#!/bin/sh

TOUCH=`cat /var/log/Xorg.0.log | grep TOUCHSCREEN | head -1 | cut -d, -f2 | sed 's/)//' | cut -d" " -f3`

if [ "$TOUCH" = "" ]; then
  TOUCH=`cat /var/log/Xorg.0.log | grep TABLET | head -1 | cut -d, -f2 | sed 's/)//' | cut -d" " -f3`
fi

VIDEO=`/opt/bin/xrandr_devices | head -1 | cut -d, -f1`

if [ "$TOUCH" != "" ] && [ "$VIDEO" != "" ]; then
  xinput map-to-output "$TOUCH" "$VIDEO"
fi

xinput_calibrator --output-type xinput > /tmp/cal.txt
echo "#!/bin/bash" > /opt/bin/calibrate.sh
echo "sleep 1" >> /opt/bin/calibrate.sh
cat /tmp/cal.txt | grep "xinput set-int-prop" >> /opt/bin/calibrate.sh
echo "xinput map-to-output \"$TOUCH\" \"$VIDEO\"" >> /opt/bin/calibrate.sh
echo "sleep 3" >> /opt/bin/calibrate.sh
cat /tmp/cal.txt | grep "xinput set-int-prop" >> /opt/bin/calibrate.sh
echo "xinput map-to-output \"$TOUCH\" \"$VIDEO\"" >> /opt/bin/calibrate.sh
rm -rf /tmp/cal.txt

chmod 755 /opt/bin/calibrate.sh


