#!/usr/bin/perl -w # Please run with -h for help # Please run with -c for configuration (first time) use strict; # use Date::Calc; -- for later my ($i, %conf); CheckArg(@ARGV) if @ARGV; die "file simplemoney.conf does not exist, please run -c to configure\n" unless (-e "simplemoney.conf"); #check for existance of configuration file open (RCONF, "simplemoney.conf") || die "Could not open simplemoney.conf $!"; foreach ("rt", "balance", "transaction"){ #load config variables into a hash chomp ($conf{$_} = ); } close (RCONF) || die "Could not close simplemoney.conf $!"; unless (-e $conf{rt} && -e $conf{balance} && -e $conf{transaction}) { die "One of the required files is missing, please run with -c to configure\n"; } while (1) { my ($command, $arg1, $arg2); print "Your command (help for list of commands):\n"; chomp ($command = ); if ($command =~ /^balance/) { print CalcBalance($conf{balance}); } elsif ($command =~ /^help/i) { PrintHelp(); } elsif ($command =~ /^deposit/i) { (undef, $arg1, $arg2) = split (/\s+/, $command); CalcBalance($conf{balance},$arg1); AddTransaction($conf{transaction},$arg2); } elsif ($command =~ /^withdraw/i) { (undef, $arg1, $arg2) = split (/\s+/, $command); $arg1 = -$arg1; CalcBalance($conf{balance},$arg1); AddTransaction($conf{transaction},$arg2); } elsif ($command =~ /^rt/i) { print "RT is not yet supported."; } elsif ($command =~ /^exit/i || $command =~ /^quit/i || $command =~ /^e/i || $command =~ /^q/i) { exit 0; } else { #must be undefined command, try again. print "\nBad Command\n"; next; } } ###Subs### sub PrintHelp { print "This program calculates your money balance, please run with the -c flag to configure.\n"; print "Flags:\n"; print " -c -- Configure\n"; print " -h -- This file\n"; print "Default Files:\n"; print " rt.log -- Reocuring transaction configuration\n"; print " balance.log -- Backlog of user's balance\n"; print " transaction.log -- Backlog of transaction descriptions\n"; print " Configuration File: simplemoney.conf\n"; print "Commands:\n"; print " help -- show this file.\n"; print " deposit -- deposit dollars with description in the log.\n"; print " withdraw -- same as above, but withdraw.\n"; print " rt -- not yet supported.\n"; print " exit, quit, e, or q -- quit the program.\n"; print "Note: running deposit or withdraw with no arguments will do nothing, running with only a value will print 'this description is intentionaly left blank' in balance.log\n" } sub CheckArg { my $rargs = (shift @_); my $arg; foreach ($rargs) { if ($_ =~ /^-h/) { PrintHelp(); exit 0; } elsif ($_ =~ /^-c/) { Configure(); } else { #must be an undefined option die"Undefined Option: Please run the program with the -h flag for help\n"; } } } sub Configure { my ($rt, $balance, $transaction); print "Welcome, please follow the instructions to configure this program (this will overwrite your current configuration)\n"; print "Reoccuring transaction file (rt.log by default)?"; chomp ($rt = ); $rt = "rt.log" unless ($rt); #assumes default if user gave no input. open (WRT, ">$rt"); close (WRT); print "Balance file (balance.log by default)"; chomp ($balance = ); $balance = "balance.log" unless ($balance); #assumes default if user gave no input. open (WBALANCE, ">$balance") || die "could not open balance.log $!"; print WBALANCE "0\n"; close (WBALANCE) || die "could not close balance.log $!"; print "Transaction file (transaction.log by default)"; chomp ($transaction = ); $transaction = "transaction.log" unless ($transaction); #assumes default if user gave no input. open (WTRANSACTION, ">$transaction") || die "could not open transaction.log $!"; print WTRANSACTION "start\n"; close (WTRANSACTION) || die "could not close transaction.log $!"; #write everything into configuration file open (WCONF, ">simplemoney.conf") || die "could not open simplemoeny.conf $!"; print WCONF "$rt\n$balance\n$transaction"; close (WCONF) || die "could not close simplemoney.conf $!"; } sub CalcBalance { my ($balance, @log); my $location = shift(@_); open (RBALANCE, $location) || die "Can't open $location $!"; @log = ; close (RBALANCE) || die "Can't close $location $!"; $balance = $log[-1]; if ($_[0]) { # we check to see if there is another # argument (the amount to change) and # it is not 0. my $toChange = shift(@_); # we only declare this if another # argument exists to make the test # above work. $balance += $toChange; open (WBALANCE, ">>$location") || die "Can't open $location $!"; print WBALANCE "$balance\n"; close (WBALANCE) || die "Can't close $location $!"; } return $balance; } sub AddTransaction { my ($location) = shift(@_); my ($description) = shift(@_) || "This description is intentionaly left blank"; open (WTRANSACTION, ">>$location") || die "Can't open $location $!"; print WTRANSACTION "$description\n"; close (WTRANSACTION) || die "Can't close $location $!"; }