#!/usr/bin/env perl
#  cbbdb - a command parsing wrapper for the CBB 'engine'.
#
#  Written by Curtis Olson.  Started August 22, 1994.
#
#  Copyright (C) 1994 - 1999  Curtis L. Olson  - curt@me.umn.edu
#
#  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.

# $Id: cbbdb,v 0.9.0.1 2001/01/17 20:26:02 genemagnier Exp $

# specify the installed location of the necessary pieces.
my($cbbpath) = "/usr/lib/cbb";
@INC = ("$cbbpath/perl", @INC);
package CBB;

require "engine.pl";
require "categories.pl";
require "accounts.pl";
require "groups.pl";
require "file.pl";
require "memorized.pl";
require "import.pl";
require "export.pl";


sub BEGIN {
    $CBB::logging = 1;		# 0 = off,  1 = on
    $CBB::debug = 0;		# 0 = off,  1 = on

    if ( $ENV{"OS"} ne "Windows_NT" ) {
        # get user name
        @CBB::pw = getpwuid($<);

        $CBB::user_name = $CBB::pw[0]
	    || $ENV{LOGNAME} || die "No user name!";
    } else {
	$CBB::user_name = "ruut";
    }

    $CBB::current_file = "";

    $CBB::encrypt = "";
    $CBB::decrypt = "";

    $CBB::duplicate = 2;	# 0 = always insert, 1 = never insert, 2 = ask
    $CBB::compare = 31;		# compare all fields except category

    # set umask to 066 (only owner should be able to read this)
    umask(066);
}

@commands = ("create_trans",		"create_trans",
	     "create_xfer",		"create_xfer",
	     "update_trans",		"update_trans",
	     "update_xfer",		"update_xfer",
	     "delete_trans",		"delete_trans",
	     "delete_xfer",		"delete_xfer",
	     "next_trans",		"next_trans", 
	     "prev_trans",		"prev_trans", 
	     "last_trans",		"last_trans", 
	     "find_trans",		"find_trans",
	     "get_current_index",	"get_current_index", 
	     "first_trans",		"first_trans",
	     "all_trans",		"all_trans", 
	     "part_trans",		"part_trans",
	     "first_uncleared_trans",   "first_uncleared_trans", 
	     "last_uncleared_trans",	"last_uncleared_trans", 
	     "next_uncleared_trans",	"next_uncleared_trans", 
	     "prev_uncleared_trans",	"prev_uncleared_trans", 
	     "select_trans",		"select_trans", 
	     "unselect_trans",	        "unselect_trans",
	     "clear_trans",		"clear_trans", 
	     "get_cleared_bal",	        "get_cleared_bal", 
	     "sort_trans",		"sort_trans",
	     "init_trans",		"init_trans",
	     "current_acct",		"current_acct",
	     "load_trans",		"load_trans",
	     "load_cbb",		"load_cbb",
	     "check_cats",              "check_cats",
	     "list_missing_cats",       "list_missing_cats", 
	     "import_qif_account",	"import_qif_account",
	     "import_qif_group",	"import_qif_group",
	     "import_cbb8",		"import_cbb8",
	     "import_cbb9",		"import_cbb9",
	     "save_cbb",		"save_cbb",
	     "export_qif",		"export_qif",
	     "export_cbb8",		"export_cbb8",
	     "export_cbb9",		"export_cbb9",
	     "save_trans",		"save_trans",
	     "auto_save_trans",	        "auto_save_trans", 
	     "rehash_mems",		"rehash_mems", 
	     "find_mem",		"find_mem",
	     "init_cats",		"init_cats", 
	     "make_cats",		"make_cats", 
	     "edit_cat",		"edit_cat",
	     "insert_cat",		"insert_cat",
	     "delete_cat",		"delete_cat",
	     "all_cats",		"all_cats", 
	     "find_cat",		"find_cat",
	     "get_cat_info",		"get_cat_info",
	     "load_cats",		"load_cats",
	     "def_cats",		"def_cats",
	     "save_cats",		"save_cats",
	     "check_cats_accounts",     "check_cats_accounts",
	     "list_missing_accounts",   "list_missing_accounts",
	     "rep_txn_list",		"rep_txn_list",
	     "rep_txn_by_cat",	        "rep_txn_by_cat",
	     "rep_txn_by_cat_short",	"rep_txn_by_cat_short",
	     "nice_date",		"nice_date",
	     "raw_date",		"raw_date", 
	     "start_date",		"start_date",
	     "century",		        "century", 
	     "get_files",		"get_files",
	     "debug",		        "debug",
	     "cache",		        "cache",
	     "encrypt",		        "encrypt",
	     "decrypt",		        "decrypt",
	     "duplicate",		"duplicate",
	     "compare",		        "compare",
	     "logging",		        "logging",
	     "quit",		        "quit",
	     "make_acct",		"make_acct",       
	     "init_accts",		"init_accts",      
	     "sort_acct",		"sort_acct",       
	     "edit_acct",		"edit_acct",       
	     "rename_acct",		"rename_acct",       
	     "save_accts",		"save_accts",       
	     "add_acct",		"add_acct",        
	     "delete_acct",		"delete_acct",     
	     "all_accts",		"all_accts",       
	     "load_accts",		"load_accts",      
	     "make_group",              "make_group",
	     "find_group",              "find_group",
	     "find_acct",               "find_acct",
	     "find_acct_file",          "find_acct_file",
	     0, 0);

# main command parsing loop
sub main_loop {
    my($arg, $command, $full_command);

    while (<>) {
	chop;

	$full_command = $_;
	print DEBUG "Command string:  '$full_command'\n" if $CBB::debug;

	($command, $arg) = split(/ /, $full_command, 2);

	for ($i = 0; 1; $i++) {
	    $name = $commands[2*$i + 0];
	    $cmd  = $commands[2*$i + 1];
	    # $help = $commands[3*$i + 1];
	    
	    if (!$name) { 
                # unknown command ... return error
		print DEBUG "unknown command\n" if $CBB::debug;
		print "error 0\n";
		last; 
	    }
	    
	    if ($name eq $command) {
		if ($CBB::logging) { &log_txn ($full_command); }

		$value = &$cmd($arg);
		print "$value\n";
		last;
	    }
	}
    }
}


&main_loop();

1;				# need to return a true value
