#!/bin/bash

export DISPLAY=:0.0

xinput_calibrator --output-type xinput > /tmp/cal
cat /tmp/cal | grep "xinput set-int-prop" > /tmp/cal2
cat /tmp/cal | grep "xinput set-int-prop" >> /tmp/cal2

TOUCH_NAME=`cat /tmp/cal2 | cut -f2 -d"\"" | tail -1`

CALIB="xinput_calibrator"

chmod 755 /tmp/cal2
/tmp/cal2
/tmp/cal2

xmessage -buttons "       YES         ":1,"        NO        ":2 "TOUCH OK?"
RES=$?

if [ "$RES" = "2" ]; then
  COMB1_1="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axis Inversion\" 8 1 0"
  COMB1_2="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axes Swap\" 8 1"

  echo $COMB1_1 > /tmp/cmd.sh
  echo $COMB1_2 >> /tmp/cmd.sh
  chmod 755 /tmp/cmd.sh
  /tmp/cmd.sh

  xmessage -buttons "       YES         ":1,"        NO        ":2 "TOUCH OK?"
  RES=$?

  if [ "$RES" = "2" ]; then
    COMB2_1="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axis Inversion\" 8 1 1"
    COMB2_2="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axes Swap\" 8 1"

    echo $COMB2_1 > /tmp/cmd.sh
    echo $COMB2_2 >> /tmp/cmd.sh
    chmod 755 /tmp/cmd.sh
    /tmp/cmd.sh

    xmessage -buttons "       YES         ":1,"        NO        ":2 "TOUCH OK?"
    RES=$?

    if [ "$RES" = "2" ]; then
      COMB3_1="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axis Inversion\" 8 1 0"
      COMB3_2="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axes Swap\" 8 1"

      echo $COMB3_1 > /tmp/cmd.sh
      echo $COMB3_2 >> /tmp/cmd.sh
      chmod 755 /tmp/cmd.sh
      /tmp/cmd.sh

      xmessage -buttons "       YES         ":1,"        NO        ":2 "TOUCH OK?"
      RES=$?

      if [ "$RES" = "2" ]; then
        COMB4_1="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axis Inversion\" 8 1 0"
        COMB4_2="xinput set-int-prop \"$TOUCH_NAME\" \"Evdev Axes Swap\" 8 0"

        echo $COMB4_1 > /tmp/cmd.sh
        echo $COMB4_2 >> /tmp/cmd.sh
        chmod 755 /tmp/cmd.sh
        /tmp/cmd.sh
        
        xmessage -buttons "       YES         ":1,"        NO        ":2 "TOUCH OK?"
        RES=$?
        
        if [ "$RES" = "2" ]; then
          xmessage "      !!!!!! ERROR CONFIGURING TOUCH !!!!!!     "
        else
          echo "$COMB4_1" >> /tmp/cal2
          echo "$COMB4_2" >> /tmp/cal2
          /opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
        fi
      else
        echo "$COMB3_1" >> /tmp/cal2
        echo "$COMB3_2" >> /tmp/cal2
        /opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
      fi
    else
      echo "$COMB2_1" >> /tmp/cal2
      echo "$COMB2_2" >> /tmp/cal2
      /opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
    fi
  else
    echo "$COMB1_1" >> /tmp/cal2
    echo "$COMB1_2" >> /tmp/cal2
    /opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
  fi
else
  /opt/bin/replace_token /root/.fluxbox/startup.orig "##COORDINATES##" /tmp/cal2 > /root/.fluxbox/startup
fi

