#!/bin/bash

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_payment.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

GBI=$1
TOKEN=$2
RFID_CLOUD_SYNC=$3
MACHINE_LABEL=$4
AREA=$5
CHECK=$6
TOTAL=$7
MODE=$8
CLERK=$9
STATION=${10}
HARDLOCK=${11}
BIZ_DATE=${12}
CREDIT_VALID_DATE=${13}

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

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

if [ $LOG -eq 1 ]; then
  echo "$LOG_PREFIX, 1,TOKEN: $TOKEN, RFID_CLOUD_SYNC: $RFID_CLOUD_SYNC, MACHINE_LABEL: $MACHINE_LABEL, AREA: $AREA, CHECK: $CHECK, TOTAL: $TOTAL, MODE: $MODE, CLERK: $CLERK, STATION: $STATION, HARDLOCK: $HARDLOCK, BIZ_DATE: $BIZ_DATE, CREDIT_VALID_DATE: $CREDIT_VALID_DATE" >> $LOG_FILE
fi

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

#TOTAL="0"
#MODE="info"

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

IS_RUNNING=`ps xa | grep $APP | grep -v grep`

#if [ $LOG -eq 1 ]; then
#  echo "$LOG_PREFIX, 2,IS_RUNNING: $IS_RUNNING" >> $LOG_FILE
#fi

if [ "$IS_RUNNING" = "" ]; then
  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 3,ERROR=NOT RUNNING" >> $LOG_FILE
  fi

  echo "ERROR=NOT RUNNING"
  exit 0
  #exec $APP_DIR/$APP $MODE $TOTAL $TOKEN
else
  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 4,send $MODE#$TOTAL#$TOKEN#$CREDIT_VALID_DATE -> /opt/pos/common/tmp/rfid_activate.txt" >> $LOG_FILE
  fi

  echo "$MODE#$TOTAL#$TOKEN#$CREDIT_VALID_DATE" > /opt/pos/common/tmp/rfid_activate.txt
fi

# timeout
wait_file "/opt/pos/common/tmp/rfid_result.txt" $TIMEOUT || {
  echo 1 > /opt/pos/common/tmp/rfid_timeout.txt
  exit 0
}

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

# has error, sent it back to ui
if [ "$HAS_ERROR" != "" ]; then
  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 5,HAS_ERROR,Will return ERROR=$HAS_ERROR" >> $LOG_FILE
  fi

  if [[ $HAS_ERROR != *"IGNORE"* ]]; then
    echo "ERROR=$HAS_ERROR"
  fi

# dont have error
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`

  SPENT_TODAY=""

  SYNCED="0"

  if [ "$RFID_CLOUD_SYNC" = "0" ]; then
    SYNCED="1"
  fi

  if [ "$DAILY" != "" ]; then
    SPENT_TODAY=`echo $DAILY | cut -d"," -f1`
  fi

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

  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 8,Will insert record in rfid_operations" >> $LOG_FILE

    if [ "$SPENT_TODAY" != "" ]; then
      # log operation
      psql -U root -d pos -c "INSERT INTO rfid_operations (synced, hardlock, business_date, token_id, card_id, operation_type, amount, check_num, clerk, area, station, station_name, card_balance, obs, gbi) VALUES ($SYNCED, '$HARDLOCK', '$BIZ_DATE', '$TOKEN',  '$CARD_ID', '$MODE', '$TOTAL', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', 'total spent today: $SPENT_TODAY', '$GBI');" >> $LOG_FILE
    else
      # log operation
      psql -U root -d pos -c "INSERT INTO rfid_operations (synced, hardlock, business_date, token_id, card_id, operation_type, amount, check_num, clerk, area, station, station_name, card_balance, gbi) VALUES ($SYNCED, '$HARDLOCK', '$BIZ_DATE', '$TOKEN',  '$CARD_ID', '$MODE', '$TOTAL', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', '$GBI');" >> $LOG_FILE
    fi
  else

    if [ "$SPENT_TODAY" != "" ]; then
      # log operation
      psql -U root -d pos -c "INSERT INTO rfid_operations (synced, hardlock, business_date, token_id, card_id, operation_type, amount, check_num, clerk, area, station, station_name, card_balance, obs, gbi) VALUES ($SYNCED, '$HARDLOCK', '$BIZ_DATE', '$TOKEN',  '$CARD_ID', '$MODE', '$TOTAL', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', 'total spent today: $SPENT_TODAY', '$GBI');" > /dev/null
    else
      # log operation
      psql -U root -d pos -c "INSERT INTO rfid_operations (synced, hardlock, business_date, token_id, card_id, operation_type, amount, check_num, clerk, area, station, station_name, card_balance, gbi) VALUES ($SYNCED, '$HARDLOCK', '$BIZ_DATE', '$TOKEN',  '$CARD_ID', '$MODE', '$TOTAL', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', '$GBI');" > /dev/null
    fi
  fi

  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 9,returned OK<enter>$AMOUNT,$CARD_ID" >> $LOG_FILE
  fi

  if [ "$DAILY" != "" ]; then
    printf "OK\n$AMOUNT,$CARD_ID,$DAILY"
  else
    printf "OK\n$AMOUNT,$CARD_ID"
  fi
fi

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

exit 0

