#!/bin/bash

function tdebug () {
  msg=$1
  #local datestr=`date +%Y%m%d_%H%M%S`
  #local datestr=$(($(date +%s%N)/1000000))
  #echo "$datestr $msg" >> /tmp/start.txt
}

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

export DISPLAY=:0.0

if [ "$STATION_NUMBER" = "0" ]; then
  # fix db indexes
  tdebug "BEFORE - fix_indexes"
  /opt/pos/server/apps/fix_indexes &
fi

# Disable screen blanking
xhost +
xset s off
xset s off -dpms
xset -dpms

/opt/pos/common/bin/ping_server.sh &

while [ 1 -gt 0 ]; do
  tdebug "###########################################"

  STATION_NUMBER=`cat $CONFIG | grep STATION_NUMBER | cut -f2 -d=`
  SCREEN_ROTATE_ON_START=`cat $CONFIG | grep SCREEN_ROTATE_ON_START | cut -f2 -d=`
  SORT_CLERKS_BASED_ON_SALES=`cat $CONFIG | grep SORT_CLERKS_BASED_ON_SALES | cut -f2 -d=`
  HAS_3G=`cat $CONFIG | grep 3G_ACTIVE | cut -f2 -d=`
  USE_TICKET_SCALE=`cat $CONFIG | grep USE_TICKET_SCALE | cut -f2 -d=`
  USE_NFC_CARD_READER=`cat $CONFIG | grep USE_NFC_CARD_READER | cut -f2 -d=`
  SCREEN_BRIGHTNESS=`cat /opt/pos/etc/config.ini | grep SCREEN_BRIGHTNESS= | 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

  tdebug "0"

  # 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

  tdebug "1"

  POSTGRES_RUNNUNG=`ps xa | grep postgresql | grep postgresql.conf | grep -v grep`

  if [ "$POSTGRES_RUNNUNG" = "" ]; then
    /etc/init.d/postgresql restart
  fi

  tdebug "1.1"

  SAMBA_RUNNING=`ps xa | grep smbd | grep -v grep`

  if [ "$SAMBA_RUNNING" = "" ]; then
    /etc/init.d/smbd restart
  fi

  tdebug "2"

  if [ ! -e "/.configured" ]; then
    if [ -e "/opt/bin/machine_selector" ]; then
      HAS_MACHINES=`find /media/ | grep Machines_U17 | grep -v Trash -i | head -1`

      if [ "$HAS_MACHINES" != "" ]; then
        /opt/bin/machine_selector Machines_U17
      fi
    fi
  fi

  tdebug "3"

  if [ "$SCREEN_BRIGHTNESS" != "" ]; then
    SCREEN_DEVICE=`/opt/bin/xrandr_devices | head -1 | cut -f1 -d,`
    
    if [ "$SCREEN_DEVICE" != "" ]; then
      xrandr --output $SCREEN_DEVICE --brightness $SCREEN_BRIGHTNESS
    fi
  fi

  tdebug "4"

  if [ "$STATION_NUMBER" = "0" ]; then
    HAS_POS_API=`cat /opt/pos/.hardlock | xargs cat | grep POS_API`
    HAS_KIOSK=`cat /opt/pos/.hardlock | xargs cat | grep KIOSK`
    HAS_AIRMENU=`cat /opt/pos/.hardlock | xargs cat | grep AIRMENU`

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

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

    if [ "$HAS_POS_API" != "" ]; then
      ps xa | grep http_api | grep -v grep | grep -v http_api_checker | awk '{ print $1 }' | xargs kill -9 2> /dev/null
      /opt/pos/server/http_api/run.sh &
    fi

    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

    if [ -e "/tmp/server_error.txt" ] || [ -e "/tmp/server_db_error.txt" ]; then
      sleep 1
      continue
    fi
  fi

  tdebug "5"

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

  tdebug "6"

  # 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

  tdebug "7"

  # 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

  tdebug "8"

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

  tdebug "9"

  # 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
      PRINT_CHECK_PRODUCTS_MODE=`cat $CONFIG | 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
    fi
  fi

  tdebug "10"

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

  if [ "$STATION_NUMBER" = "0" ]; then
    BACKUP_HOURS=`cat /opt/pos/etc/config.ini | grep BACKUP_HOURS= | grep -v "#" | cut -f2 -d"="`

    if [ "$BACKUP_HOURS" != "" ]; then
      HAS=`crontab -l | grep hardlock_backup | grep "$BACKUP_HOURS"`
      if [ "$HAS" = "" ]; then
        # echo "will install $BACKUP_HOURS" >> /root/cron.log
        crontab -l | grep -v hardlock_backup > /tmp/$$.mycron
        echo "0 $BACKUP_HOURS * * * /opt/pos/common/bin/hardlock_backup \"\" cron" >> /tmp/$$.mycron
        crontab /tmp/$$.mycron
        rm /tmp/$$.mycron
      fi
    else
      HAS=`crontab -l | grep hardlock_backup | grep "*\/3"`
      if [ "$HAS" = "" ]; then
        # echo "will install */3" >> /root/cron.log
        crontab -l | grep -v hardlock_backup > /tmp/$$.mycron
        echo "0 */3 * * * /opt/pos/common/bin/hardlock_backup \"\" cron" >> /tmp/$$.mycron
        crontab /tmp/$$.mycron
        rm /tmp/$$.mycron
      fi
    fi
  fi

  # if relocate windows is running, restart it
  RELOCATE_WINDOWS_RUNNING=`ps xa | grep relocate_windows | grep -v grep`

  if [ "$RELOCATE_WINDOWS_RUNNING" != "" ]; then
    SCRIPT=`ps xa | grep relocate_windows | grep -v grep | awk '{ print $6 }'`
    WIDTH=`ps xa | grep relocate_windows | grep -v grep | awk '{ print $7 }'`
    HEIGHT=`ps xa | grep relocate_windows | grep -v grep | awk '{ print $8 }'`

    ps xa | grep relocate_windows | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null

    if [ "$WIDTH" != "" ] && [ "$HEIGHT" != "" ]; then
      $SCRIPT $WIDTH $HEIGHT &
    else
      $SCRIPT &
    fi
  fi

  tdebug "11"

  # if the fast_mount.sh script didn't create this file, start handlers
  if [ ! -e "/tmp/.did_fast_mount.txt" ]; then
    tdebug "11.1"

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

    if [ "$STATION_NUMBER" != "0" ]; then
      if [ -e "/tmp/server_error.txt" ]; then
        sleep 10
      #else
      #  sleep 5
      fi
    fi
  else
    rm -rf /tmp/.did_fast_mount.txt
  fi

  tdebug "12"

  if [ "$USE_NFC_CARD_READER" = "1" ]; then
    tdebug "12.1"

    ps xa | grep nfc_card_read | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    /opt/pos/common/bin/nfc_card_read &

    if [ "$STATION_NUMBER" = "0" ]; then
      if [ ! -e "/opt/pos/server/data/nfc_to_table.txt" ]; then
        touch /opt/pos/server/data/nfc_to_table.txt
      fi
    fi
  else
    tdebug "12.2"

    /etc/init.d/pcscd stop &

    if [ "$STATION_NUMBER" = "0" ]; then
      if [ -e "/opt/pos/server/data/nfc_to_table.txt" ]; then
        rm -f /opt/pos/server/data/nfc_to_table.txt
      fi
    fi
  fi

  tdebug "13"

  # 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

  tdebug "14"

  # start custom usb keys read
  if [ -e "/opt/pos/common/addons/read_usb_keys/etc/device" ]; then
    tdebug "14.1"
    ps xa | grep read_keys | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    /opt/pos/common/addons/read_usb_keys/bin/read_keys.sh &
  elif [ -e "/opt/pos/common/addons/read_usb_keys/etc/device2" ]; then
    tdebug "14.2"
    ps xa | grep read_keys | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
    /opt/pos/common/addons/read_usb_keys/bin/read_keys2.sh &
  fi

  tdebug "before pos start"

  if [ ! -e "/tmp/stop_pos.txt" ]; then
    /opt/pos/common/bin/pos /opt/pos
  fi

  tdebug "after pos start"

  sleep 0.1
done


