#!/bin/bash
# AQ
# 20150827

killall GT_service

DEVICE=`xinput_calibrator --list | sed "s/Device \"//;s/\" id.*//"`
if [ "$DEVICE" == "No calibratable devices found." ];then
    ports=`sudo /bin/cat /proc/tty/driver/serial | grep " uart:[A-Z0-9][^\s]* " | awk '{print $1}' | sed -n 's/\([0-9]\)\+.*/COM\1/p'`
    ports=`echo $ports | sed 's/COM/false COM/g'`
    port=$(zenity --title='Caalbration of GeneralTouch' --text='Choose COM port of touchscreen' --list --checklist --column='' --column='' $ports)
    port=`echo $port | sed 's/COM//'`

    cp -f /usr/local/Gentouch_S/GentouchS.bak /usr/local/Gentouch_S/GentouchS.conf
    sed -i "s/COMPort.*/COMPort $port/" /usr/local/Gentouch_S/GentouchS.conf

    file="$HOME/.config/autostart/GT_service.desktop"
    echo "[Desktop Entry]" > $file
    echo "Type=Application" >> $file
    echo "Name=GentralTouch Service" >> $file
    echo "Exec=/usr/local/Gentouch_S/GT_service restart" >> $file
    chmod 644 $file
    
    echo "/dev/ttyS$port" > $HOME/atm/resources/ports.blacklist
    
    /usr/local/Gentouch_S/GTscal /dev/ttyS$port
else
    zenity --info --text="The USB TouchScreen was installed"
fi
