#!/usr/bin/env perl
#
# Copyright © 2022 Nick Bowler
#
# Simple script to help convert the CSV dumps of daily exchange rates from
# the Bank of Canada into ledger price databases.
#
# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
# This is free software: you are free to do what the fuck you want to.
# There is NO WARRANTY, to the extent permitted by law.

use strict;
use Text::CSV;

my ($file) = (@ARGV, "/dev/stdin");
my $csv = Text::CSV->new({ binary => 1 });

open my $fh, "<", $file or die "$file: $!";

while (<$fh>) {
	last if /OBSERVATIONS/;
}
$csv->header($fh);

while (my $row = $csv->getline_hr($fh)) {
	next unless $row->{date} =~ /....-..-../;

	foreach (sort keys %$row) {
		my $fx = uc $_;
		if ($fx =~ /^FX([[:upper:]]{3})CAD$/) {
			print "P $row->{date} $1 $row->{$_} CAD\n"
		}
	}

}
