#!/bin/bash
# Copyright 2019 Chad D. Lemmen http://www.lemmen.com
#
# UK Making Tax Digital (MTD)
# https://developer.service.hmrc.gov.uk/api-documentation
 

# Put the passed options into an array
passopt=($(echo " $*" | grep -o " -[[:alpha:]]"))


# Show available options
read -d '' showhelp <<EOD

Usage: $0 [options]

  -a <access_token>   OAuth 2.0 Access Token 
  -b <begin_date>     Date from which to return obligations YYYY-MM-DD
  -c <auth_code>      Authorization Code
  -e <end_date>       Date to which to return obligations YYYY-MM-DD
  -f <file>           JSON file to read
  -g <test_scenario>  Gov-Test-Scenario header e.g. QUARTERLY_NONE_MET
  -h                  Show help
  -i <client_id>      Application Client ID
  -o                  Retrieve VAT obligations
  -r                  Submit VAT Return
  -s <client_secret>  Application Client Secret
  -t                  Get OAuth 2.0 access token
  -u                  Get authorization URL
  -v <vrn>            VAT Registration Number

EOD

if [ -z "$1" ]; then # No options passed
  echo "$0: try '$0 -h' for more information"
  exit
fi

# Get runtime options...
while getopts 'a:b:c:e:f:g:hi:ors:tuv:' opt; do
  case "$opt" in
    a)
      accesstoken="$OPTARG"
      ;;
    b)
      begindate="$OPTARG"
      ;;
    c)
      authcode="$OPTARG"
      ;;
    e)
      enddate="$OPTARG"
      ;;
    f)
      jsonfile="$OPTARG"
      ;;
    g)
      gov_test_scenario="$OPTARG"
      ;;
    h)
      echo
      echo "$showhelp"
      echo
      exit
      ;;
    i)
      clientid="$OPTARG"
      ;;
    o)
      obligations=true
      ;;
    r)
      submitreturn=true
      ;;
    s)
      clientsecret="$OPTARG"
      ;;
    t)
      gettoken=true
      ;;
    u)
      getauthurl=true
      ;;
    v)
      vrn="$OPTARG"
      ;;
    *)
      exit # getopts reports the illegal option
      ;;
  esac
done


check_required_options() {
  local option=$1
  local requires=($2)

  if [[ "${passopt[@]}" =~ "$option" ]]; then
    for r in "${requires[@]}"; do
      if [[ ! "${passopt[@]}" =~ "$r" ]]; then
        echo "Option ${option} requires option(s) ${requires[@]}" 
        exit 1
      fi
    done
  fi
}

# Some options require other options
check_required_options -u "-i"
check_required_options -t "-c -i -s"
check_required_options -o "-a -v -b -e"
check_required_options -r "-a -v -f"


# Production base URL
baseurl='https://api.service.hmrc.gov.uk'
# To use the test system export SS_MTD_TEST=1
if [ -n "$SS_MTD_TEST" ]; then
  # Sandbox base URL
  baseurl='https://test-api.service.hmrc.gov.uk'
fi

# function to transfer data from or to a json endpoint
endpoint_transfer() {
case "$1" in
  authorize_url)
      endpoint="/oauth/authorize?response_type=code"
      parameter=("&client_id=$clientid"
                 "&scope=write:vat+read:vat"
                 "&redirect_uri=urn:ietf:wg:oauth:2.0:oob")
      endpoint="$endpoint"$(printf '%s' "${parameter[@]}")
      headers=()
      # using an array because of the spaces
      curlopts=(-ILs -o /dev/null -w '%{url_effective} \n%{http_code}')
      ;;
  token)
      endpoint="/oauth/token"
      parameter="client_secret=${clientsecret}&client_id=${clientid}&grant_type=authorization_code&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=${authcode}"
      header=()
      curlopts=(-X POST -s -w '\n%{http_code}' --data "$parameter")
      ;;
  retrieve_vat_obligations)
      endpoint="/organisations/vat/$vrn/obligations"
      parameter="from=${begindate}&to=${enddate}"
      headers=(-H "Accept: application/vnd.hmrc.1.0+json" \
               -H "Authorization: Bearer $accesstoken" \
               -H "Gov-Test-Scenario: $gov_test_scenario")
      curlopts=(-G -s -w '\n%{http_code}' -d "$parameter")
      ;;
  submit_vat_return)
      endpoint="/organisations/vat/$vrn/returns"
      headers=(-H "Accept: application/vnd.hmrc.1.0+json" \
               -H "Content-Type: application/json" \
               -H "Authorization: Bearer $accesstoken")
      curlopts=(-s -w '\n%{http_code}' -d @"$jsonfile")
      ;;
  *)
      echo "Unknown endpoint type"
      exit
      ;;
esac

# debug
#echo curl "${curlopts[@]}" "${headers[@]}" "${baseurl}${endpoint}"
response=$(curl "${curlopts[@]}" "${headers[@]}" "${baseurl}${endpoint}")
response=(${response[@]}) # convert to array
jsonresp=${response[@]::${#response[@]}-1} # get all elements except last
httpcode=${response[-1]} # get last element (last line)

# The authorize endpoint does not return a JSON response
# for either an HTTP error or the auth url
if [ "$1" = "authorize_url" ]; then
  return
fi

# HTTP status codes used by HMRC
# 200 to 299 if it succeeded
# 400 to 499 if it failed because of a client error by your application
# 500 to 599 if it failed because of an error on our server

jq '.' <<< "$jsonresp"
#echo "$jsonresp"

if [ $httpcode -ge 400 -a $httpcode -le 599 ]; then
  exit 1
fi
if [ $httpcode -ge 200 -a $httpcode -le 299 ]; then
  return
fi
}


# Getting an OAuth 2.0 access token
#
# Send your user to our authorization endpoint
# so they can request authorization. 
# The user will need to copy/paste the returned url
# into their web browser and then copy/paste the
# authorization code back into the application.
# The authorization code, if authorization is successful
# is a single-use token that expires 10 minutes after it's issued.
if [ "$getauthurl" == "true" ]; then
  endpoint_transfer authorize_url

  if [ $httpcode -ge 400 ]; then
    # No auth url was returned, show HTTP error code
    echo HTTP $httpcode
    ret=1
  else
    # Return the authorization URL
    echo "$jsonresp"
    ret=0
  fi

  exit $ret
fi


# Exchange authorization code for access token 
# within 10 minutes before it expires.
# A user's access_token expires after 4 hours.
if [ "$gettoken" == "true" ]; then
  endpoint_transfer token
fi


# Retrieve VAT obligations. This endpoint is one of the two mandatory
# endpoints to use to achieve the minimum level of compliance.
# This endpoint is user-restricted - it requires an Authorization header
# containing an OAuth 2.0 Bearer Token with the read:vat scope.
if [ "$obligations" == "true" ]; then
  endpoint_transfer retrieve_vat_obligations
fi


# Submit VAT return for period. This endpoint is one of the two mandatory
# endpoints to use to achieve the minimum level of compliance. 
# This endpoint is user-restricted - it requires an Authorization header
# containing an OAuth 2.0 Bearer Token with the write:vat scope
if [ "$submitreturn" == "true" ]; then
  endpoint_transfer submit_vat_return
fi

