/* --------------------------------------------------------------- */
/* exchange -- get exchange rate and offer conversions             */
/* --------------------------------------------------------------- */
/*                                                                 */
/* Copyright (c) Mike Cowlishaw, 2012.  All rights reserved.       */
/*                                                                 */
/* Permission to use, copy, modify, and/or distribute this         */
/* software for any purpose with or without fee is hereby granted, */
/* provided that the above copyright notice and this permission    */
/* notice appear in all copies, and that notice and the date of    */
/* any modifications be added to the software.                     */
/*                                                                 */
/* This software is provided "as is".  No warranties, whether      */
/* express, implied, or statutory, including, but not limited to,  */
/* implied warranties of merchantability and fitness for a         */
/* particular purpose apply to this software.  The author shall    */
/* not, in any circumstances, be liable for special, incidental,   */
/* or consequential damages, for any reason whatsoever.            */
/*                                                                 */
/* --------------------------------------------------------------- */
/* Arguments are:                                                  */
/*                                                                 */
/*   from currency      (default GBP)                              */
/*   to   currency      (default USD)                              */
/*   amount to convert  (default 1)                                */
/*                                                                 */
/* Returns the rate of exchange (not converted amount) or '' if    */
/* any error.                                                      */
/*                                                                 */
/* For example:                                                    */
/*                                                                 */
/*   exchange('GBP USD 100')                                       */
/*                                                                 */
/* might return 1.53068323 (if called as a function).              */
/*                                                                 */
/* Displays the result of conversions in both directions whether   */
/* called as a  command or a function, for example:                */
/*                                                                 */
/*   EXCHANGE GBP USD  100                                         */
/*   Rate: 100 GBP = 153.06800 USD  [on 2012-06-01]                */
/*         100 USD =  65.33030 GBP                                 */
/*                                                                 */
/* Currencies must be listed at the URL initialized below.         */
/*                                                                 */
/* Requires geturl.rex.                                            */
/* --------------------------------------------------------------- */
/* 2012.02.04 MFC new rewritten version                            */
/* 2012.05.21 Change to use European Central Bank                  */
/*            [Yahoo JPY-GBP gives 0.0080]                         */
/* 2013.08.21 New URL for ECB                                      */

erase=1   -- 0 to keep body file

parse arg from to amount .
if from='' then from='GBP'; else from=translate(from)
if to  ='' then to  ='USD'; else to  =translate(to)
if amount='' then amount=1
amount=changestr(',', amount, '')  -- drop commas

if \datatype(amount, 'n') then do
  say 'Sorry, amount must be a number, not:' amount
  return ''
  end

-- url='http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml'
-- Moved 2013.08 to:
url='http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'

-- say url
-- say 'Getting exchange rate (from' from 'to' to')...'

say 'Getting latest European Central Bank rates ...'

file='gotbody.$$$'
body=geturl(url)
if length(body)<3 then say '[Empty response.]'
call charout file, body
call charout file

/* Should now have an XML file of key rates for Euro; read all     */
/* of them and then calculate the rate requested.                  */
date=''
rate.='?'
rate.eur=1.0000
do while lines(file)>0
  line=linein(file)
  -- quotes might be single or double
  line=changestr('''', line, '"')
  parse var line '<Cube time="' d '"'
  if d\='' then do
    date=d
    iterate
    end
  -- currency and rate can be swapped depending on browser/request(!)
  parse var line '<Cube rate="' r1 '"' 'currency="' c1 '"'
  parse var line '<Cube currency="' c2 '"' 'rate="' r2 '"'
  r=r1||r2
  if r='' then iterate
  c=c1||c2
  rate.c=r
  -- say ''''c'''' ''''r''''
  end

call lineout file
if \erase then 'copy' file 'gotbody.html 1>nul'
call sysfiledelete file

-- say 'from' from rate.from
-- say '  to' to   rate.to

f=rate.from
t=rate.to

if f='?' then do
  say 'Sorry, "from" currency' from 'is not in table'
  rate=''
  end
else if t='?' then do
  say 'Sorry, "to" currency' to 'is not in table'
  rate=''
  end
else rate=t/f


if rate='' then say 'No rate found.'
 else do
  numeric digits 6
  r1=format(rate*amount,,5)             -- decimal places
  r2=format(amount/rate,,5)
  len=max(length(r1), length(r2))       -- align ...
  r1=right(r1, len)
  r2=right(r2, len)

  say ' ' amount from '=' r1 to     ' [on' date']'
  say ' ' amount to   '=' r2 from
  end

parse source . how .
if how='FUNCTION' then return rate