#!/usr/bin/perl
#
# Author: Peter Keel <killer@discordia.ch>
# Virtual Cash Account manipulation. This was written for a LARP game and
# is not secure in any way. 

die "Usage: $0 user amount\n" unless ( scalar @ARGV == 2 );

$amount=$ARGV[0];
$destuser=$ARGV[1];
$infile="./konto";
$outfile="./konto.bak";
$fromuser=$ENV{'USER'};

open(IN,"<$infile") or die "Could not open $infile: $!";
open(OUT,">$outfile") or die "Could not open $outfile: $!";
while(<IN>){
    chomp;
    $line = $_;
	( $user, $konto) = $line =~ /^(\w+):(\w+)/;
	if ( ($user eq $fromuser) && ($amount <= $konto)) {
	    $konto=$konto-$amount; }
	elsif ($user eq $destuser) {
	    $konto=$konto+$amount; }
	print OUT $user,":",$konto,"\n";
}
close(IN);
close(OUT);

rename $outfile, $infile;
