#!/bin/bash

# Gets from the cloud the points of a particular customer

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

ACTION=$1
CARD_NUMBER=$2
CLOUD_TOKEN=$3

TIMEOUT=5

KEY_FIELD="card_number"

if [ "$POINTS_FOR_APP_ONLY" = "1" ]; then
  KEY_FIELD="app_sign_key"
fi

if [ "$CARD_NUMBER" = "" ] || [ "$CLOUD_TOKEN" = "" ]; then
  echo "Usage: $0 <action> <$KEY_FIELD> <cloud_token>"
  exit
fi

if [ "$ACTION" = "points" ]; then
  POINTS=`curl --connect-timeout $TIMEOUT --max-time $TIMEOUT -F "$KEY_FIELD=$CARD_NUMBER" -F "token=$CLOUD_TOKEN" http://apisv1.net-bo.com/shared_points/customer_points 2> /dev/null`

  if [ $? != 0 ]; then
    echo -n "ERROR"
  else
    echo -n "$POINTS"
  fi
elif [ "$ACTION" = "statement" ]; then
  STATEMENT=`curl --connect-timeout $TIMEOUT --max-time $TIMEOUT -F "$KEY_FIELD=$CARD_NUMBER" -F "token=$CLOUD_TOKEN" http://apisv1.net-bo.com/shared_points/customer_statement 2> /dev/null`

  if [ $? != 0 ]; then
    echo -n "ERROR"
  else
    echo -n "$STATEMENT"
  fi

elif [ "$ACTION" = "info" ]; then
  STATEMENT=`curl --connect-timeout $TIMEOUT --max-time $TIMEOUT -F "$KEY_FIELD=$CARD_NUMBER" -F "token=$CLOUD_TOKEN" http://apisv1.net-bo.com/shared_points/customer_info 2> /dev/null`

  if [ $? != 0 ]; then
    echo -n "ERROR"
  else
    echo -n "$STATEMENT"
  fi

elif [ "$ACTION" = "check_server" ]; then

  # not implemented
  echo "OK"

else
  echo "Invalid action - $action"
fi

