#!/bin/bash

# resets a card balance based on data saved online

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_hardcode.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} # used to link rfid_operations to the check so we can put fo_doc_number there
TOKEN=$2
RFID_CLOUD_SYNC=$3
MACHINE_LABEL=$4
AREA=$5
CHECK=$6
TOTAL=$7
CLERK=$8
STATION=$9
HARDLOCK=${10}
BIZ_DATE=${11}
OLD_BALANCE=${12}
EXPIRE_DATE=${13}
ORIGINAL_CARD_ID=${14}
OBS=${15}
DAILY_LIMIT=${16}

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, CLERK: $CLERK, STATION: $STATION, HARDLOCK: $HARDLOCK, BIZ_DATE: $BIZ_DATE, EXPIRE_DATE: $EXPIRE_DATE, ORIGINAL_CARD_ID: $ORIGINAL_CARD_ID, OBS: $OBS, DAILY_LIMIT: $DAILY_LIMIT" >> $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"

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
else
  if [ $LOG -eq 1 ]; then
    echo "$LOG_PREFIX, 4,send balance_hardcode#$TOTAL#$TOKEN#$EXPIRE_DATE~$ORIGINAL_CARD_ID -> /opt/pos/common/tmp/rfid_activate.txt" >> $LOG_FILE
  fi

  if [ "$DAILY_LIMIT" != "" ]; then
    echo "balance_hardcode#$TOTAL#$TOKEN#$EXPIRE_DATE~$ORIGINAL_CARD_ID#$DAILY_LIMIT" > /opt/pos/common/tmp/rfid_activate.txt
  else
    echo "balance_hardcode#$TOTAL#$TOKEN#$EXPIRE_DATE~$ORIGINAL_CARD_ID" > /opt/pos/common/tmp/rfid_activate.txt
  fi
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

  exit 0
# 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`

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

  SYNCED="0"

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

  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', 'debit', '$OLD_BALANCE', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', '$OBS', '$GBI');" > /dev/null
  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', 'credit', '$TOTAL', '$CHECK', '$CLERK', '$AREA', '$STATION', '$MACHINE_LABEL', '$AMOUNT', '$OBS', '$GBI');" > /dev/null

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

  printf "OK\n$AMOUNT,$CARD_ID"
fi

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

exit 0

