#!/bin/bash

DIR=`dirname $0`
. $DIR/config

HAS_MAIN_DISPLAY=`xrandr | grep $MAIN_DISPLAY`
HAS_CUSTOMER_DISPLAY=`xrandr | grep $CUSTOMER_DISPLAY`
HAS_TOUCH_ID=`xinput --list | grep id=$TOUCH_ID`

if [ "$HAS_MAIN_DISPLAY" == "" ]; then
  echo "Can't find MAIN_DISPLAY - $MAIN_DISPLAY (check 'xrandr' command)"
  exit
fi

if [ "$HAS_CUSTOMER_DISPLAY" == "" ]; then
  echo "Can't find CUSTOMER_DISPLAY - $CUSTOMER_DISPLAY (check 'xrandr' command)"
  exit
fi

if [ "$HAS_TOUCH_ID" == "" ]; then
  echo "Can't find TOUCH_ID - $TOUCH_ID (check 'xinput --list' command)"
  exit
fi

# apply correct properties to config.ini
C1="/tmp/config.ini.1.$$"
C2="/tmp/config.ini.2.$$"

cat /opt/pos/etc/config.ini | grep -v DISABLE_SCREEN_RESCALE | grep -v AUTO_RELOCATE_WINDOWS | grep -v SCREEN_RESCALE_W | grep -v SCREEN_RESCALE_H | grep -v USE_GRAPHICAL_CUSTOMER_DISPLAY > $C1

echo "DISABLE_SCREEN_RESCALE=0" > $C2
echo "AUTO_RELOCATE_WINDOWS=1" >> $C2
echo "USE_GRAPHICAL_CUSTOMER_DISPLAY=1" >> $C2
echo "SCREEN_RESCALE_W=$MAIN_DISPLAY_W" >> $C2
echo "SCREEN_RESCALE_H=$MAIN_DISPLAY_H" >> $C2
cat $C1 >> $C2

cp /opt/pos/etc/config.ini /opt/pos/etc/config.ini.customer_display
mv $C2 /opt/pos/etc/config.ini
rm -rf $C1

# graphical customer display app setup
$DIR/app_config_replace.pl $MAIN_DISPLAY_W

reboot

