#!/bin/bash

if [ -e "/.resized_partition" ]; then
  RESIZED_PARTITION=`cat /.resized_partition`
  rm -rf /.resized_partition

  if [ "$RESIZED_PARTITION" != "" ]; then
    resize2fs $RESIZED_PARTITION
    reboot
    exit
  fi
fi

# makes sure only display 0 starts the application
if [ "$DISPLAY" != ":0" ] && [ "$DISPLAY" != ":0.0" ]; then
  while [ true ]; do
    sleep 600
  done
fi

# start a new one
/opt/pos/common/bin/vnc_server.sh &

# graphical customer display
/opt/pos/common/bin/graphical_customer_display.sh &

TEAMVIEWER_ALWAYS_RUNNING=`cat /opt/pos/etc/config.ini | grep TEAMVIEWER_ALWAYS_RUNNING | cut -f2 -d=`
ANYDESK_ALWAYS_RUNNING=`cat /opt/pos/etc/config.ini | grep ANYDESK_ALWAYS_RUNNING | cut -f2 -d=`

if [ "$TEAMVIEWER_ALWAYS_RUNNING" = "1" ]; then
  /opt/pos/common/bin/teamviewer.sh 1 &
fi

if [ "$ANYDESK_ALWAYS_RUNNING" = "1" ]; then
  /opt/pos/common/bin/any_desk.sh 1 &
fi

# cleanup done files
find /opt/pos/server/data/areas/ | grep "\.done" | xargs rm 2> /dev/null

#/opt/pos/common/bin/vnc_stations.sh &

STATION_NUMBER=`cat /opt/pos/etc/config.ini | grep STATION_NUMBER | cut -f2 -d=`

if [ "$STATION_NUMBER" = "0" ]; then
  ONLINE_ORDERS_TOKEN=`cat $CONFIG | grep "ONLINE_ORDERS_TOKEN=" | cut -f2 -d=`
  ONLINE_ORDERS_API_PASSWD=`cat $CONFIG | grep "ONLINE_ORDERS_API_PASSWD=" | cut -f2 -d=`

  if [ "$ONLINE_ORDERS_TOKEN" != "" ] && [ "$ONLINE_ORDERS_API_PASSWD" != "" ]; then
    /opt/pos/server/apps/sync_online_orders.sh &
  fi

  INTERNET_SYNC_TOKEN=`cat /opt/pos/etc/config.ini | grep "INTERNET_SYNC_TOKEN=" | cut -f2 -d=`

  if [ "$INTERNET_SYNC_TOKEN" != "" ]; then
    /opt/pos/server/apps/db_sync.sh &
  fi

  RFID_CLOUD_SYNC=`cat /opt/pos/etc/config.ini | grep "RFID_CLOUD_SYNC=" | cut -f2 -d=`
  POINTS_SYNC_TOKEN=`cat /opt/pos/etc/config.ini | grep "POINTS_SYNC_TOKEN=" | cut -f2 -d=`
  VOUCHERS_SYNC_TOKEN=`cat /opt/pos/etc/config.ini | grep "VOUCHERS_SYNC_TOKEN=" | cut -f2 -d=`
  INTERNET_SMS_SYNC_TOKEN=`cat /opt/pos/etc/config.ini | grep "INTERNET_SMS_SYNC_TOKEN=" | cut -f2 -d=`
  QUIZ_QUESTIONS=`cat /opt/pos/etc/config.ini | grep "QUIZ_QUESTIONS=" | cut -f2 -d=`

  if [ "$POINTS_SYNC_TOKEN" != "" ]       || \
     [ "$VOUCHERS_SYNC_TOKEN" != "" ]     || \
     [ "$INTERNET_SMS_SYNC_TOKEN" != "" ] || \
     [ "$QUIZ_QUESTIONS" != "" ]          || \
     [ "$RFID_CLOUD_SYNC" != "" ]
  then
    /opt/pos/server/apps/run_online_api_feeder.sh &
  fi
fi

while [ 1 ]; do
  STATION_NUMBER=`cat /opt/pos/etc/config.ini | grep STATION_NUMBER | cut -f2 -d=`
  SCREEN_ROTATE_ON_START=`cat /opt/pos/etc/config.ini | grep SCREEN_ROTATE_ON_START | cut -f2 -d=`
  BACKGROUND_WEB_BROWSER=`cat /opt/pos/etc/config.ini | grep BACKGROUND_WEB_BROWSER | cut -f2 -d=`
  SORT_CLERKS_BASED_ON_SALES=`cat /opt/pos/etc/config.ini | grep SORT_CLERKS_BASED_ON_SALES | cut -f2 -d=`
  HAS_3G=`cat /opt/pos/etc/config.ini | grep 3G_ACTIVE | cut -f2 -d=`
  USE_TICKET_SCALE=`cat /opt/pos/etc/config.ini | grep USE_TICKET_SCALE | cut -f2 -d=`
  RFID_TOKEN=`cat /opt/pos/etc/config.ini | grep RFID_TOKEN= | cut -f2 -d=`

  CUR_DATE=`date +%Y%m%d_%H%M%S`
  echo "$CUR_DATE >> $0" >> /opt/pos/run.log

  # kill rfid daemon to start it again
  ps xa | grep rfid_daemon | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

  if [ "$RFID_TOKEN" != "" ]; then
    /opt/pos/common/bin/rfid/rfid_daemon.sh &
  fi

  # restart scale ticket software
  if [ "$USE_TICKET_SCALE" = "1" ]; then
    ps xa | grep scale_tickets | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
  fi

  # screen rotate
  if [ "$SCREEN_ROTATE_ON_START" != "" ]; then
    /opt/pos/common/bin/screen_rotate rotate
  fi

  # stop and start hid readers
  if [ -e "/opt/pos/etc/hid.txt" ]; then
    for pid in `ps xa | grep usb_hid_reader | grep -v grep | awk '{ print $1 }'`; do
      kill -9 $pid 2> /dev/null
    done

    /opt/pos/common/bin/usb_hid_reader.pl &
  fi

  # install additional components
  if [ -e "/opt/pos/install/install.sh" ]; then
    /opt/pos/install/install.sh
    rm -rf /opt/pos/install/*
  fi

  if [ "$STATION_NUMBER" = "0" ]; then
    if [ -e "/opt/pos/server/apps/udp_broadcast" ]; then
      ps xa | grep udp_broadcast | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

      if [ -e "/opt/pos/etc/network_autodiscovery.txt" ]; then
        /opt/pos/server/apps/udp_broadcast &
      fi
    fi
  else
    if [ -e "/opt/pos/common/bin/udp_receiver" ]; then
      ps xa | grep udp_receiver | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
      /opt/pos/common/bin/udp_receiver &
    fi
  fi

  # server is available with no errors
  if [ ! -e "/tmp/server_error.txt" ] && [ ! -e "/tmp/server_db_error.txt" ]; then
    # server cleanups
    if [ "$STATION_NUMBER" = "0" ]; then
      API_FEEDER_RUNNING=`ps xa | grep online_api_feeder | grep -v grep`

      if [ "$API_FEEDER_RUNNING" != "" ]; then
        ps xa | grep online_api_feeder | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
        /opt/pos/server/apps/online_api_feeder.pl &
      fi

      PRINT_CHECK_PRODUCTS_MODE=`cat /opt/pos/etc/config.ini | grep PRINT_CHECK_PRODUCTS_MODE | cut -f2 -d=`

      if [ "$PRINT_CHECK_PRODUCTS_MODE" = "1" ]; then
        rm /opt/pos/common/print_templates/include/products.tmpl
        ln -s /opt/pos/common/print_templates/include/products.single.tmpl /opt/pos/common/print_templates/include/products.tmpl
      elif [ "$PRINT_CHECK_PRODUCTS_MODE" = "2" ]; then
        rm /opt/pos/common/print_templates/include/products.tmpl
        ln -s /opt/pos/common/print_templates/include/products.double.tmpl /opt/pos/common/print_templates/include/products.tmpl
      fi

      # sort clerks based on their sales
      if [ "$SORT_CLERKS_BASED_ON_SALES" != "" ]; then
        /opt/pos/server/apps/clerks_set_priority
      fi

      # fix db indexes
      /opt/pos/server/apps/fix_indexes &

      # berkeydb safeness
      rm -rf /opt/pos/server/data/database/__*

      MYLANG=`cat /opt/pos/etc/config.ini | grep LANG= | cut -f2 -d=`

      if [ "$MYLANG" != "" ]; then
        if [ -e "/opt/pos/common/skins/$MYLANG/custom/login.png" ]; then
          fbsetbg -f /opt/pos/common/skins/$MYLANG/custom/login.png
        else
          fbsetbg -f /opt/pos/common/skins/$MYLANG/login.png
        fi
      fi
    else
      if [ -e "/shares/pos/etc/config.ini" ]; then
        MYLANG=`cat /shares/pos/etc/config.ini | grep LANG= | cut -f2 -d=`

        if [ "$MYLANG" != "" ]; then
          if [ -e "/opt/pos/common/skins/$MYLANG/custom/login.png" ]; then
            fbsetbg -f /opt/pos/common/skins/$MYLANG/custom/login.png
          else
            fbsetbg -f /opt/pos/common/skins/$MYLANG/login.png
          fi
        fi
      fi
    fi

    if [ "$BACKGROUND_WEB_BROWSER" != "" ]; then
      CHROME_RUNNING=`ps xa | grep chromium-browser | grep -v grep`

      if [ "$CHROME_RUNNING" = "" ]; then
        /usr/bin/chromium-browser --user-data-dir=/root/.chrome &
      fi
    fi

    # start network manager applet
    #if [ -e "/root/.fluxbox/init.hidden" ] && [ -e "/usr/bin/nm-applet" ]; then
    #  ISRUN=`ps xa | grep "nm\-applet" | grep -v grep`

    #  if [ "$ISRUN" = "" ]; then
    #    nm-applet &
    #    sleep 3
    #    xdotool mousemove 790 1 click 3
    #  fi
    #fi

    # setup ram disks
    /opt/pos/common/bin/setup_ram_disks.sh 2> /dev/null

    # restart hardware handler
    /opt/pos/common/bin/restart_hardware_handler.sh &

    # script to stop services and free some memory. Only is executed
    # if it exists.
    if [ -e "/root/free_memory.sh" ]; then
      /root/free_memory.sh
    fi

    # if defined, auto start 3g
    if [ "$HAS_3G" = "1" ]; then
      /opt/pos/common/bin/3g_activate.sh &
    fi

    # makes sure machine config is killed before pos starts
    ps xa | grep machine_config | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ -e "/opt/bin/keyboard.sh" ]; then
      /opt/bin/keyboard.sh 1
    else
      killall -9 matchbox-keyboard
    fi

    /opt/pos/common/bin/pos /opt/pos
  fi

  sleep 1
done

