#!/usr/bin/perl -w
# $Id: tf,v 1.5 1994/11/14 07:08:19 cbbrowne Exp cbbrowne $
# Arguments: tf [-t / date], ref#, acct_from, acct_to, amount, comment

#  Written by Christopher B. Browne
#
#  Copyright (C) 1994  Christopher B. Browne cbrowne@io.org
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

if ($#ARGV != 5)
   {
   print "incorrect arguments\n";
   print "tf  [Date]  [Ref#]  [From_acct] [To_acct]  [Amount]  [Comment]\n\n";
   print "    Adds a transfer (2 transactions) to a cbb file\n";
   print "    use '-t' to fill in today's date\n\n";
   print "Example:\n  dantzig[90]> tf -t 'n/a' Chequing Cash 80.00 'Money from ATM Machine'\n";
   die(-1);
   }
# parse args
   ($date, $ref, $from, $to, $amount, $comment) = @ARGV;
 	$from =~ tr/A-Z/a-z/;
	$to =~ tr/A-Z/a-z/;

require "cbbshutils.pl";

# verify that [from] and [to] exist
if (&need_txn($from) ne "ok") {
	print "$from account does not exist\n";
	die -1;
}
&need_txn($to);
if (&need_txn($to) ne "ok") {
	print "$to account does not exist\n";
}

# run txn with the now verified arguments
$fromamt = (-$amount);
$txn1="txn '$from' $date '$ref' 'Funds Transfer' '[$to]' $amount '$comment'";
print $txn1, "\n";
$txn2="txn '$to' '$date' '$ref' 'Funds Transfer' '[$from]' $fromamt '$comment'";
print $txn2, "\n";
system ($txn1);
system($txn2);

#txn 'chequing' -t 'n/a' 'Funds Transfer' '[cash]' 0.00 ''
