#!/usr/bin/perl -w

BEGIN {
    push @INC,"/var/www/pimengest/gestion/lib";
}

use strict;
use PgCharge;

sub usage {
    print "usage:\n
make a pdf file containing a list of bills.
listFact.pl -listid=id1,id2,... or listeFact.pl -listuid=uid1,uid2,...
";
} 

sub getArgs {
    my @args=@_;
    my $oldKey="";
    my %opts=();
    my $el;
    for $el (@args) {
		if ($el=~/^-/) {
			$el=~s/-+//g;
			$opts{$el}="";
			$oldKey=$el;
		}
		else {
			$opts{$oldKey}=$el;
		}
    }
    return %opts;
}

my %opts;
my $debug=0;
my @listId;
my @listUid;
my ($entete,$call);

%opts=getArgs(@ARGV);

if (exists $opts{help}) {
    &usage();
    exit;
}
if (exists $opts{d}) {
    $debug=1;
}

if (exists $opts{listid}) {
	#print STDERR "Assuming a list of id...\n";
	@listId=split(',',$opts{listid});
}

if (exists $opts{listuid}) {
	#print STDERR "Assuming a list of uid...\n";
	@listUid=split(',',$opts{listuid});
}

if (exists $opts{entete}) {
	#print STDERR "Assuming a list of uid...\n";
	$entete="-entete";
}

if (exists $opts{call}) {
	$call=$opts{call};
} else {
	&usage();
}


#============ Main ===============#
my $i=0;
my @listName;

# on commence par les id :
for my $id (@listId) {
	my $name='FACT-ID'.$id;
	my @latex=`$call $entete -id '$id' > /tmp/$name.tex`;
	push @listName,$name.".pdf";
	print STDERR "Working on $name.tex...\n";
	my $err=`cd /tmp/ ; pdflatex --interaction nonstopmode /tmp/$name.tex`;
}
print join(' ',@listName);

