#!/bin/sh
#
# Initiate touchscreen calibration if needed and possible
# Afterwards, restart system.
#
# needed: no calib found on sdcard
# possible: SEG_SDCONF_PATH exists

set -e

SEG_SDCONF_PATH="/mnt/sdcard/.seg5/touchscreen"
SEG_POINTERCAL="${SEG_SDCONF_PATH}/pointercal"

export TSLIB_TSDEVICE=/dev/input/touchscreen0
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_PLUGINDIR=/usr/lib/ts

calibIfNeeded()
{
  if [ ! -f $SEG_POINTERCAL ] && [ -d $SEG_SDCONF_PATH ]
  then
    echo "Found seg5 sdcard but no touch calibration"
    echo "Starting calibration - system will restart afterwards"
    TSLIB_CALIBFILE=${SEG_POINTERCAL} ts_calibrate
    sync
    reboot
  fi
}

case "$1" in
  start)
		echo -n "Verifying touchscreen calibration..."
		calibIfNeeded
		echo "done."
		;;
	stop)
		;;
	restart)
	  ;;
	status)
		;;
	*)
		echo "Usage: /etc/init.d/touchscreen start"
	  exit 1
esac

exit 0
