#!/usr/bin/perl -w
# $Id$
# Dieses Script (latex-titlepage) wurde von Rdiger Beck erstellt
# Es ist freie Software
# Bei Fehlern wenden Sie sich bitte an mich.
# jeffbeck@web.de  oder  jeffbeck@gmx.de

# thist script creates 
# 1) the titlepage 
# 2) the page after, i.e. the licence and contributers page


# Bibliotheken
use strict;

use Getopt::Long;
Getopt::Long::Configure ("bundling");
use File::Find;
use coldformix;
# ==========================================================================


# Choose the Language: de, en
my $lang="de";

# Choose the Unit-System: si_symbol,
my $unit_system = "si_symbol";

my @contributors=&coldformix::read_contributors();
my @error_contributors=&coldformix::read_error_contributors();


# ===========================================================================
# Optionen verarbeiten
# ==========================================================================
# Parsen der Optionen
my $testopt=GetOptions(
           "lang|l=s" => \$lang,
           "unit_system|u=s" => \$unit_system
          );


print "Language: $lang \n";
print "Unit_System: $unit_system \n";

# Prfen, ob Optionen erkannt wurden, sonst Abbruch
&coldformix::check_options($testopt);



## make sure the directorys exist

open(TITLEPAGE, "<../coldformix-source/templates/title-template-$lang.tex");

my $latexout="../coldformix-generated/$lang/latex";

if (not -e $latexout){
    system("install -d $latexout");
}

open(LATEXOUT, ">$latexout/titlepage-$lang.tex") || die "LATEXOUT not opened";


while (<TITLEPAGE>){
    if(/^\%/){next;} # Bei Kommentarzeichen aussteigen
    if(/^CONTRIBUTORS/){
       # Contributors
       my @list=();
       foreach my $name (@contributors){
          my ($surname, $common) = split(/:/, $name);
          my $entry="$common"." "."$surname";
          push @list, $entry;   
       }
       my $line = join ",\n", @list;
       print LATEXOUT "$line"."."."\n";   
    } elsif(/^ERRORCONTRIBUTORS/){
       # Error Contributors
       my @list=();
       foreach my $name (@error_contributors){
          my ($surname, $common) = split(/:/, $name);
          my $entry="$common"." "."$surname";
          push @list, $entry;   
       }
       my $line = join ",\n", @list;
       print LATEXOUT "$line"."."."\n";   
    } else {
       print LATEXOUT $_;
    }
}


close(LATEXOUT);
close(TITLEPAGE); 



