#!/bin/bash

CLERK_ID=$1
STATION=$2
AREA=$3
CHECK=$4
GRID_SEL_LINES=$5


# called from gambas
TO_STDOUT=""

if [ "$STATION" = "TO_STDOUT" ]; then
  TO_STDOUT="1"
fi

wait_file() {
  local file="$1"; shift
  local wait_seconds="${1:-10}"; shift # 10 seconds as default timeout

  until test $((wait_seconds--)) -eq 0 -o -f "$file" ; do sleep 0.5; done

  ((++wait_seconds))
}

LOG=1
NOW=`date +%Y%m%d_%H%M%S`
LOG_PREFIX="rfid_balance.sh,$NOW,$$"
LOG_FILE="/opt/pos/server/data/rfid.log"

# timeout when reading cards
TIMEOUT=60 # this is 30 secs because the sleep above is 0.5

if [ -e "/opt/pos/common/tmp/rfid_result.txt" ]; then
  rm -f /opt/pos/common/tmp/rfid_result.txt
fi

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

if [ "$TOKEN" = "" ]; then
  echo "ERROR#RFID_TOKEN in config.ini missing"
  exit
fi

#if [ ! -e "/dev/ttyUSB0" ] && [ ! -e "/dev/ttyUSB1" ]; then
#  echo "ERROR#Device not found..."
#  exit
#fi

if [ $LOG -eq 1 ]; then
  echo "----------------- BEGIN - $LOG_PREFIX" >> $LOG_FILE
fi

if [ $LOG -eq 1 ]; then
  echo "$LOG_PREFIX, 1,will send - info#0#$TOKEN#" >> $LOG_FILE
fi

echo info#0#$TOKEN# > /opt/pos/common/tmp/rfid_activate.txt

DIR=`dirname $0`
APP_DIR="/opt/pos/common/bin/rfid"
APP="rfid_daemon"

wait_file "/opt/pos/common/tmp/rfid_result.txt" $TIMEOUT || {
  #ps xa | grep $APP | grep -v grep | awk '{ print $1 }' | xargs kill -9 2> /dev/null
  echo 1 > /opt/pos/common/tmp/rfid_timeout.txt
  echo "STOP"
  exit 0
}

IS_ERROR=`cat /opt/pos/common/tmp/rfid_result.txt | grep ERRO`

if [ "$IS_ERROR" != "" ]; then
  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 2,ERROR: $IS_ERROR" >> $LOG_FILE
  fi

  if [ "$TO_STDOUT" != "" ] || [[ $IS_ERROR == *"IGNORE"* ]]; then
    echo "$IS_ERROR"
  else
    echo "#ALERT#$IS_ERROR" | nc -w1 127.0.0.1 8088
  fi
else
  CARD_ID=`cat /opt/pos/common/tmp/rfid_result.txt | cut -d"#" -f1`
  AMOUNT=`cat /opt/pos/common/tmp/rfid_result.txt | cut -d"#" -f2`
  EXP_DATE=`cat /opt/pos/common/tmp/rfid_result.txt | cut -d"#" -f3`
  DAILY=`cat /opt/pos/common/tmp/rfid_result.txt | cut -d"#" -f4`

  DAILY_LIMIT=""
  LIMIT=""
  DAY=""

  if [ "$DAILY" != "" ]; then
    DAILY_LIMIT=`echo $DAILY | cut -d"," -f1`
    LIMIT=`echo $DAILY | cut -d"," -f2`
    DAY=`echo $DAILY | cut -d"," -f3`
  fi

  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 3,CARD_ID: $CARD_ID, AMOUNT: $AMOUNT, EXP_DATE: $EXP_DATE" >> $LOG_FILE
  fi

  if [ "$TO_STDOUT" != "" ]; then
    if [ "$LIMIT" != "" ]; then
      echo "$CARD_ID#$AMOUNT#$EXP_DATE#$LIMIT"
      echo "$CARD_ID#$AMOUNT#$EXP_DATE#$LIMIT" > /opt/pos/common/tmp/rfid_result.txt
    else
      echo "$CARD_ID#$AMOUNT#$EXP_DATE"
      echo "$CARD_ID#$AMOUNT#$EXP_DATE" > /opt/pos/common/tmp/rfid_result.txt
    fi
  else
    MSG_AUX=""
    CUSTOMER=`psql -U root -d pos -c "SELECT name, nif, aux FROM customers WHERE card_number = '$CARD_ID'" | grep -v " row" | grep -v " name " | grep -v "\-\-\-\-\-\-" | head -1`

    if [ "$CUSTOMER" != "" ]; then
      CUSTOMER_NAME=`echo $CUSTOMER | cut -d\| -f1 | cut -c1-30`
      CUSTOMER_AUX=`echo $CUSTOMER | cut -d\| -f3 | cut -c1-30`

      if [ "$CUSTOMER_AUX" != "" ]; then
        MSG_AUX="$CUSTOMER_NAME<BR>$CUSTOMER_AUX<BR>"
      else
        MSG_AUX="$CUSTOMER_NAME<BR>"
      fi

      #CUSTOMER_NIF=`echo $CUSTOMER | cut -d\| -f2`
      #MSG_AUX="$CUSTOMER_NAME<BR>$CUSTOMER_NIF<BR>"

    fi

    if [ "$DAILY_LIMIT" != "" ]; then
      #FREE_TO_SPEND=`echo "$LIMIT - $DAILY_LIMIT" | bc`
      FREE_TO_SPEND="$DAILY_LIMIT"
      #echo "#ALERT#$MSG_AUX$CARD_ID<BR>$EXP_DATE<BR>$AMOUNT<BR>DAILY SPENT $DAILY_LIMIT / DAILY LIMIT $LIMIT / DAY $DAY - $FREE_TO_SPEND" | nc -w1 127.0.0.1 8088

      # print to printer
      echo 1 > "/opt/pos/common/tmp/rfid_card_balance#$CLERK_ID#$CARD_ID#$AMOUNT#$EXP_DATE#$FREE_TO_SPEND#$LIMIT.txt"

      # send to ui
      #echo "#ALERT#$MSG_AUX$CARD_ID<BR>$EXP_DATE<BR><u><b>$AMOUNT</b></u><P>SPENT TODAY<BR><b><u>$FREE_TO_SPEND</b></u> OF <b><u>$LIMIT</b></u>" | nc -w1 127.0.0.1 8088
    else
      # print to printer
      echo 1 > "/opt/pos/common/tmp/rfid_card_balance#$CLERK_ID#$CARD_ID#$AMOUNT#$EXP_DATE.txt"

      # send to ui
      #echo "#ALERT#$MSG_AUX$CARD_ID<BR>$EXP_DATE<BR>$AMOUNT" | nc -w1 127.0.0.1 8088
    fi
  fi
fi

if [ "$TO_STDOUT" = "" ]; then
  echo "STOP"
fi

if [ $LOG -eq 1 ]; then
  echo "----------------- END - $LOG_PREFIX" >> $LOG_FILE
fi

exit 0

