#! /usr/bin/perl
use strict;
use warnings;
require './autoeconomy.pm';

my $dbh = autoeconomy::db_connect();
my $reference = $ARGV[0];
my $cssfile = $ARGV[1];

my $ref = $dbh->selectrow_hashref('SELECT * FROM transaction WHERE reference=?', undef,
	$reference);
if (!defined($ref)) {
	die "Couldn't find row '$reference'";
}
	
my @parms = (
	"--account=" . $ref->{'account'},
	"--usedate=" . $ref->{'use_date'},
	"--reference=" . $ref->{'reference'},
	"--type=" . $ref->{'trans_type'},
	"--text=" . $ref->{'text'},
	"--direction=" . (defined($ref->{'amount_in'}) ? 'in' : 'out'),
	"--amount=" . ($ref->{'amount_in'} || $ref->{'amount_out'})
);

if (defined($cssfile)) {
	system("crm", "learn.crm",
		@parms,
		"--cssfile=" . $cssfile
	);
} else {
	system("crm", "classify.crm",
		@parms,
		"--cssfiles=" . join(' ', <*.css>)
	);
}
