#!/usr/bin/perl

my $N = $ARGV[0];

open(LST,"simo/liste.txt"); my @liste = <LST>; chop(@liste); close(LST);
foreach (@liste) {
    if ($_ =~ /^$N:/) {
        ($null,$fichier,$a,$b,$c,$d,$e,$f,$k,$titre) = split(/:/,$_);
        $fichier =~ s/\s+$//;
        $titre   =~ s/^\s+//;
    }
}

exit(0) if $fichier eq "";

print "Taitement de $fichier ($N)\n";

print " -> construction des patrons...\n";

$orbite   = `./orb2tab.pl simo/$fichier`;
$orbitemp = `./orb2path.pl simo/$fichier`;
$orbitemp =~ s/§/\n/g;

%C = (
    orbite      => $orbite,
    orbitemp    => $orbitemp,
    nom         => sprintf("simo%02d",$N),
    a           => $a,
    b           => $b,
    c           => $c,
    d           => $d,
    e           => $e,
    f           => $f,
    t           => 1,
    nb          => sprintf("%d", $d/$b),
    num         => $N,
    titre       => $titre
);


open(MP,">fond.mp");
print MP &Patron("fond.pat",\%C);
close(MP);

open(MP,">image.mp");
print MP &Patron("image.pat",\%C);
close(MP);

open(SC,">trajectoire.sc");
print SC &Patron("trajectoire.pat",\%C);
close(SC);

open(HTM,">$C{nom}.html");
print HTM &Patron("popup.pat",\%C);
close(HTM);

open(HTM,">$C{nom}w.html");
print HTM &Patron("popupw.pat",\%C);
close(HTM);

print " -> compilations vers swf...\n";
print `. compilations1`;
print " -> compilations vers png...\n";
print `. compilations2`;

if (-e "image.png") {
    print `mv image.png $C{nom}.png`;
}

if (-e "image.mp") {
    print `mv image.mp  $C{nom}.mp`;
}
sub Patron {
    my ($nom_patron,$remplissage) = @_;
    my $texte;
    local $/;
    local *F;
    open (F,"< $nom_patron\0") || return;
    $texte = <F>;
    close(F);
    $texte =~ s{ %%(.*?)%% }
              { exists( $remplissage->{$1} )
                      ? $remplissage->{$1}
                      : ""
              }gsex;
    return $texte;
}
