#!/usr/bin/perl

$| =1;

use Image::Magick;

$image=Image::Magick->new(size=>'500x500');
$image->Read("xc:white");

@nb = (0,31,59,90,120,151,181,212,243,273,304,334,365);

open(DEP,"depenses/courant.txt");
$ligne = <DEP>;

$ligne =~ /^(\d+)\/(\d+)\/(\d+)\./;
$debut = $1+$nb[$2];

@amount = ();
@when = ();

$nombre = 0;
while($ligne=<DEP>)
{
	$ligne =~ /^(\d+)\/(\d+)\/(\d+):(.*):(.*)$/;
	($jour, $mois, $annee, $montant, $quoi) = ($1,$2,$3,$4,$5);
	$courant = $jour+$nb[$mois];
	$ecart = $courant - $debut;

	push @u_when,$ecart*15-1;

	$nombre++;

	$u_amount{$montant} = $ecart*15-1;
}

@amount = sort { $u_amount{$a} <=> $u_amount{$b}; } keys %u_amount;
@when = sort { $a <=> $b; } @u_when;

$total = 0;
@tout = ();
foreach $mont (@amount)
{
	$total += $mont;
	push @tout, 499-$total/2;
}

$image->Draw(primitive=>"line", points=>"0,500,450,75", stroke=>"red"); #850 euro
$image->Draw(primitive=>"line", points=>"0,500,450,175", stroke=>"green"); #650 euro

for ($i=0;$i<$nombre;$i++)
{
	if ($i==0)
	{
		$ligne=sprintf("%i,%i,%i,%i", 0,499, $when[$i],$tout[$i]);
	} else {
		$ligne=sprintf("%i,%i,%i,%i", $when[$i-1],$tout[$i-1], $when[$i],$tout[$i]);
	}
	$image->Draw(primitive=>"line", points=>$ligne);
}

print "Content-type: image/gif\n\n";
$image->Write('gif:-');

