#!/usr/bin/perl

$file = "sdk_rente.txt";
open(F, "<$file") or die("ERROR: cannot read file $file: $!");
while(<F>){
	$line = $_;
	chomp($line);
	$caption = $line if($line=~/Datum/);
	next if($line!~/^\d{4}\/\d\d/ or $line!~/\S/);
	$line =~ s/\s+/ /g;
	@words = split(/\s+/, $line);
	$lines{$words[0]} = $line;
}
close(F);
($date, $rateM, $rateY, $invest, $ret, $loss, $death, $end, $win, $pension) = split(/\s+/, $line);
$totalcost = $invest;

println("$file: $totalcost");
println($caption);
@nkeys = sort(keys(%lines));
for($n=0;$n<$#nkeys+1;$n++){
	($date, $rateM, $rateY, $invest, $ret, $loss, $death, $end, $win, $pension) = split(/\s+/, $lines{$nkeys[$n]});
	if($ret=~/\S/){
		$loss = $invest-$ret;
		$win = $end - $totalcost;
	}
	printf("$date	$rateM	%7s	%8.2f	%8.2f	%8.2f	%8.2f	%8.2f	%8.2f %6.2f\n", $rateY, $invest, $ret, $loss, $death, $end, $win, $pension);
}


sub println{
	my $msg = shift;
	print $msg ."\n";
}
