#!/usr/bin/perl

BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }

use Fcntl qw(:flock);
use CGI::Carp qw(fatalsToBrowser);
use JSON;
use LWP::Simple qw($ua get);

#-------------------------------------------------------------------------------

my  @ARGV     = split(/\&/, $ENV{'QUERY_STRING'});
our $debug    = $ARGV[0] ;

# https://itvadmin.co.za/cgi-bin/scripts/cron/get_exchange_rates.pl?1

if ($debug) { our $username = 'rory' ; print "Content-type: text/html\n\n"; }

exit;

#------------------------------------------------------------------------------------------

&fetch_exchange_rates ;
&save_info ;
exit ;

#-------------------------------------------------------------------------------

sub fetch_exchange_rates  {

&fetch_exchange_rates_using_json_send_post_api_1('USD','ZAR') ;
&fetch_exchange_rates_using_json_send_post_api_1('USD','GBP') ;
&fetch_exchange_rates_using_json_send_post_api_1('USD','EUR') ;

&fetch_exchange_rates_using_json_send_post_api_2('USD','ZAR') ;
&fetch_exchange_rates_using_json_send_post_api_2('USD','GBP') ;
&fetch_exchange_rates_using_json_send_post_api_2('USD','EUR') ;

} #------------------------------------------------------------------------------------------

sub fetch_exchange_rates_using_json_send_post_api_1 {

my ($from_curr,$to_curr) = @_ ;

my $url = "http://currency-api.appspot.com/api/$from_curr/$to_curr.json" ;

my $json = get $url ;
print "Couldn't get $url ! $!" unless defined $json ;

&common_debug($json);
# exit;

my $perl_scalar = decode_json $json ;

# my $rate    =  $perl_scalar->{'rate'} ;
my $rate  =  $perl_scalar->{'amount'} ; # rounded to 2 decimal places

$exchng_rate{$from_curr}{$to_curr} = $rate ;
   
} #-------------------------------------------------------------------------------

sub fetch_exchange_rates_using_json_send_post_api_2 {

my ($from_curr,$to_curr) = @_ ;

my $cft = $from_curr . '_' . $to_curr ;

my $url = "http://free.currencyconverterapi.com/api/v5/convert?q=$cft&compact=y&apiKey=1ada7dd66cf88a0211cd" ; # rory@kre8it.co.za API key
# my $url = "http://free.currencyconverterapi.com/api/v5/convert?q=$cft&compact=y&apiKey=1a0ae2c44dc4a60c7539" ; # vivian@filmfreight.co.za API key

my $json = get $url ;
die "Couldn't get $url ! $!" unless defined $json ;

my $decoded_json = decode_json $json ;

if ($debug) { print "json=$json \n" ; }

my $ccapirate  =  $decoded_json->{$cft}->{'val'} ; # rounded to 2 decimal places

$exchng_rate_ccapi{$from_curr}{$to_curr} = $ccapirate ;
   
} #-------------------------------------------------------------------------------

sub save_info {

unless (%exchng_rate) { return ; }

$exchgfile = "/usr/lib/cgi-bin/scripts/cron/feed.dat";

open      (IFILE, "+>>$exchgfile")      or die "can't open $exchgfile : $!" ;
flock     (IFILE, LOCK_EX)              or die "can't lock $exchgfile : $!" ;
seek      (IFILE, 0, 0)                 or die "can't rewind $exchgfile : $!" ;
@exchng = <IFILE> ;
seek      (IFILE, 0, 0)                 or die "can't rewind $exchgfile : $!" ;
truncate  (IFILE, 0)                    or die "can't truncate $exchgfile : $!" ;
   
foreach $line (@exchng) {
    chomp $line;
    @linex = split(/\|/,$line);
    
    my $from      =  $linex[0] ;
    my $to        =  $linex[1] ;
    my $rate      =  $linex[2] ;
    my $prate     =  $linex[3] ;
    my $ccapirate =  $linex[4] ;
    
    if ($exchng_rate{$from}{$to}) {
        $prate = $rate ;
        $rate  = $exchng_rate{$from}{$to} ;
        }
        
    if ($exchng_rate_ccapi{$from}{$to}) {
        $ccapirate = $exchng_rate_ccapi{$from}{$to} ;
        }
        
    $done{$from}{$to} = 1 ;
    
    print IFILE "$from|$to|$rate|$prate|$ccapirate|\n";
    }
    
foreach my $from (keys %exchng_rate) {
    foreach my $to (keys %{$exchng_rate{$from}}) {
        if ($done{$from}{$to}) { next ; }
        print IFILE "$from|$to|$exchng_rate{$from}{$to}||$exchng_rate_ccapi{$from}{$to}|\n";
        }
    }

close   (IFILE) ;
  
} #----------------------------------------------------------------------------------------  

use common ;

1;

