#!/usr/bin/perl

## To use this script, you'd better browse through the comments
## (marked with '##') and check all the values. The default is 
## swedish phone-taxes, with 15% off (an offer from Felia).

## Some definitions:

## How much more expensive is it during the high tax period?
$expensive = 2;

open (LOGGEN,"<loggen");

#while (<LOGGEN>) {
#	print;
#}

@raderna = <LOGGEN>;
close (LOGGEN);

while ($raderna[0]) {
	if (shift @raderna eq "\n") {
		++$entrynr;

## Begin counting in the lines
		
		$upp = shift @raderna;
		$ned = shift @raderna;
		
		($upp{'day'}, $upp{'month'}, $upp{'calday'}, $upp{'time'}, $junk, $upp{'year'}) = split (/ /, $upp);
		($ned{'day'}, $ned{'month'}, $ned{'calday'}, $ned{'time'}, $junk, $ned{'year'}) = split (/ /, $ned);
		($upp{'hour'}, $upp{'minute'}, $upp{'second'}) = split (/:/, $upp{'time'});
		($ned{'hour'}, $ned{'minute'}, $ned{'second'}) = split (/:/, $ned{'time'});
		
## We'll do a simple check to see if the lines are ok.

		for $i ($upp, $ned) {
			$check = split (/ /, $i);
			if ($check != 6) {die "Error while processing line $radnr:\n\n$i";}
		}

## Did we connect during a high tax period? Let's check it.
		if (($upp{'day'} eq "Mon" or $upp{'day'} eq "Tue" or $upp{'day'} eq "Wed" or $upp{'day'} eq "Thu" or $upp{'day'} eq "Fri") and ($upp{'hour'} < 18 and $upp{'hour'} > 8)) {
## Did we then disconnect again after the low tax period had begun?
			if ($ned{'hour'} > 17) {
## Please enter the hour when tax switches to cheap * 3600 on the two following rows.
				$eventsumexp = 64800 * $expensive - $upp{'hour'} * 3600 * $expensive - $upp{'minute'} * 60 * $expensive - $upp{'second'} * $expensive;
				$eventsumche = $ned{'hour'} * 3600 + $ned{'minute'} * 60 + $ned{'second'} - 64800;
				$eventsum = $eventsumche + $eventsumexp;

			}
			else {
				$upp{'seconds'} = $upp{'hour'} * 3600 * $expensive + $upp{'minute'} * 60 * $expensive + $upp{'second'} * $expensive;
				$ned{'seconds'} = $ned{'hour'} * 3600 * $expensive + $ned{'minute'} * 60 * $expensive + $ned{'second'} * $expensive;

				$eventsum = $ned{'seconds'} - $upp{'seconds'};
			}
		}
		else {
			$upp{'seconds'} = $upp{'hour'} * 3600 + $upp{'minute'} * 60 + $upp{'second'};
			$ned{'seconds'} = $ned{'hour'} * 3600 + $ned{'minute'} * 60 + $ned{'second'};

			$eventsum = $ned{'seconds'} - $upp{'seconds'};
			if ($eventsum < 0) {
				$eventsum += 86400;
			}
		}

		$secsum += $eventsum;



#		print $upp{'time'}, "\n";
	}
	else {
		die "Error while processing line $radnr.";
	}
}

## Enter here your cost per second, e.g. your cost per minute / 60
$money = 0.0014375 * $secsum;
## Does it cost you anything to connect? Use this line.
$money += .4 * $entrynr;
$money = int($money);

$sum{'hours'} = int($secsum / 3600);
$secsum -= $sum{'hours'} * 3600;
$sum{'minutes'} = int($secsum / 60);
$secsum -= $sum{'minutes'} * 60;
$sum{'seconds'} = $secsum;
print "Vilket ar ca $sum{'hours'} timmar och $sum{'minutes'} minuter. (raknat pa kvallstaxa)\n";
print "Detta har kostat dig $money kr. Beklagar.\n";



