#!/bin/bash

CONFIG="/opt/pos/etc/config.ini"
USE=`cat $CONFIG | grep USE_GRAPHICAL_CUSTOMER_DISPLAY | grep -v grep | grep 1`
AUTO_RELOCATE_WINDOWS=`cat /opt/pos/etc/config.ini | grep AUTO_RELOCATE_WINDOWS | cut -f2 -d=`

# kills the app and check graphical display devices
KILL_AND_CHECK=$1

if [ "$KILL_AND_CHECK" != "" ]; then
  PID=`ps xa | grep graphical_display | grep -v grep | awk '{print $1}'`
  if [ "$PID" != "" ]; then
    kill -9 $PID 2> /dev/null
  fi

  # only does this when USE_GRAPHICAL_CUSTOMER_DISPLAY is on
  if [ "$USE" != "" ]; then
    # check framebuffer device only when we're not in windows relocate mode
    if [ "$AUTO_RELOCATE_WINDOWS" != "1" ]; then
      # keep the correct device in /etc/X11/xorg.conf
      CUR_SYS_DISPLAY_DEVICE=`dmesg | grep fb | grep "DisplayLink USB device" | cut -d":" -f2 | cut -d" " -f5 | uniq`
      CUR_XORG_DISPLAY_DEVICE=`cat /etc/X11/xorg.conf | grep "Option \"fbdev" | cut -d"\"" -f4`

      if [ "$CUR_SYS_DISPLAY_DEVICE" != "" ] && [ "$CUR_XORG_DISPLAY_DEVICE" != "" ]; then
        if [ "$CUR_SYS_DISPLAY_DEVICE" != "$CUR_XORG_DISPLAY_DEVICE" ]; then
          if [ -e "/etc/X11/xorg.conf" ]; then
            echo "Will change $CUR_XORG_DISPLAY_DEVICE in xorg.conf to $CUR_SYS_DISPLAY_DEVICE"
            perl -pi -e "s|$CUR_XORG_DISPLAY_DEVICE|$CUR_SYS_DISPLAY_DEVICE|gm" /etc/X11/xorg.conf
          fi
        fi
      fi
    fi
  fi

  exit
fi

if [ "$USE" != "" ]; then
  # dual screens
  if [ "$AUTO_RELOCATE_WINDOWS" = "1" ]; then
    if [ -e "/opt/pos/common/bin/graphical_customer_display/activate" ]; then
      /opt/pos/common/bin/graphical_customer_display/activate
    fi

    SCREEN_RESCALE_W=`cat $CONFIG | grep SCREEN_RESCALE_W | grep -v grep | cut -f2 -d=`
    SCREEN_RESCALE_H=`cat $CONFIG | grep SCREEN_RESCALE_H | grep -v grep | cut -f2 -d=`

    if [ "$SCREEN_RESCALE_W" != "" ] && [ "$SCREEN_RESCALE_H" != "" ]; then
      /opt/pos/common/bin/relocate_windows $SCREEN_RESCALE_W $SCREEN_RESCALE_H &
    else
      /opt/pos/common/bin/relocate_windows &
    fi

    export DISPLAY=:0.0

    while [ 1 ]; do
      rm -rf /opt/pos/common/tmp/graphical_display 2> /dev/null
      mkdir /opt/pos/common/tmp/graphical_display

      /opt/pos/common/addons/graphical_display/app
      sleep 1
    done

  # usb screens
  else
    # keep the correct device in /etc/X11/xorg.conf
    CUR_SYS_DISPLAY_DEVICE=`dmesg | grep fb | grep "DisplayLink USB device" | cut -d":" -f2 | cut -d" " -f5 | uniq`
    CUR_XORG_DISPLAY_DEVICE=`cat /etc/X11/xorg.conf | grep "Option \"fbdev" | cut -d"\"" -f4`

    if [ "$CUR_SYS_DISPLAY_DEVICE" != "" ] && [ "$CUR_XORG_DISPLAY_DEVICE" != "" ]; then
      if [ "$CUR_SYS_DISPLAY_DEVICE" != "$CUR_XORG_DISPLAY_DEVICE" ]; then
        /opt/pos/common/addons/graphical_display/xorg_install
        perl -pi -e "s|$CUR_XORG_DISPLAY_DEVICE|$CUR_SYS_DISPLAY_DEVICE|gm" /etc/X11/xorg.conf
      fi
    fi

    export DISPLAY=:0.1

    while [ 1 ]; do
      rm -rf /opt/pos/common/tmp/graphical_display 2> /dev/null
      mkdir /opt/pos/common/tmp/graphical_display

      /opt/pos/common/addons/graphical_display/app
      sleep 1
    done
  fi
fi

