#!/usr/bin/perl -w
# $Id: deltxn,v 1.5 1994/11/14 06:53:47 cbbrowne Exp cbbrowne $
#  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.
require "getopts.pl";
require "cbbshutils.pl";
$name = shift(@ARGV); $name =~ tr/A-Z/a-z/;
&need_txn($name);
&need_os($name);
&need_bal($name);

@LIST = &construct_sorted_os_list();

foreach $tag (@ARGV) {
    if ($tag > $#LIST) {
	print "Tag $tag out of range 0..$#LIST\n";
    } else {
	$key = $OSLIST{$LIST[$tag]};
	$value = &get_TXNS($key);
	($date, $check, $desc, $debit, $credit, $cat, $com, $cleared, $total) =
	    split(/:/, $value);
	$nicedate = &nice_date($date);
	if (($cleared eq "*") || ($cleared eq "")) {
	    $success = "TRUE";
        } elsif ($cleared eq "D") {
            print "Transaction $tag has already been marked for deletion.\n";
	    $success = "FAIL";
	} elsif ($cleared eq "X") {
	    print "I'm not sure quite how this worked out, but you seem to be\n";
	    print "trying to delete a reconciled transaction.  You can't.\n";
	    $success = "FAIL";
	}
	$value = "$date:$check:$desc:$debit:$credit:$cat:$com:D:$total";
	if ($success eq "TRUE") {
            &put_TXNS($key, $value, "Tag to Delete", $cleared, "D", $debit-$credit);
	    print "Tagged item $tag for deletion\n";
	} else {
	    print "Failed to delete item $tag\n";
	}
	&listtxn($tag, $value);
    }
}
&list_bals();
