#!/bin/bash

precalib () {
	# screen 0
	setparams -d 0
	# HID mode: Digitizer
	setparams -h 3
	# Touch mode: Drawing
	setparams -t 0
	# Autoright click: ON
	setparams -a 1
	# Autoright click count: 150
	setparams -c 150
	# Firstmove: ON
	setparams -f 1
}


found=0
lsusb | grep -q "1ddb:0001" 	
if [ $? -eq 0 ]; then
	found=1
fi
lsusb | grep -q "1bfd:1568" 	
if [ $? -eq 0 ]; then
	found=1
fi
if [ $found -eq 0 ]; then
	zenity --error --title="Touch calibration" --text="Risin touch controller not found!"
	echo 'Risin touch controller not found!'
	echo ' '
	exit 1
fi

account=`whoami`
if [ ${account} != "root" ]; then
	zenity --error --title="Touch calibration" --text="You must execute this script with sudo."
	echo " You must execute this script with sudo"
	echo ' '
	exit 1
fi

precalib | zenity --progress --pulsate --auto-close --title="Touch calibration" --text="Initializing controller..."

#Calibrate
LinearAp 4 | grep "PermanentParameter==TRUE"
if [ $? -ne 0 ]; then
	zenity --error --title="Touch calibration" --text="Pre-calibration failed!"
	echo 'Pre-calibration failed!'
	echo ' '
	exit 1
fi

rm -f /usr/share/X11/xorg.conf.d/99-calibration.conf

zenity --info --title="Touch calibration" --text="Risin controller initialized.\nYou must restart the scale before calibrate."
echo " You must restart the scale before a new calibration"
echo ' '

