#!/usr/bin/perl -w
# $Id$
# Dieses Script (latex-substances) 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 reads data from an sql database
# and converts it to a latex-table

# Bibliotheken
use strict;
use DBI;
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";


# ===========================================================================
# 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);

my %substances  = &coldformix::get_substances();
my %symbols  = &coldformix::get_symbols();
my %units  = &coldformix::get_units();
my %trans = &coldformix::get_trans();


my @exp_symbols=();

#
if (not -e "../coldformix-generated/$lang/latex") {
    system("install -d ../coldformix-generated/$lang/latex");
}

# 
my $latex_line_substance="";
my $latex_line_build_mat="";
my $latex_line_cool_good_1="";
my $latex_line_cool_good_2="";
my $latex_line_cool_good_3="";
my $name="";
my $name_lang;
my $comment_lang;
my $type;

my $density;
my $melt_point;
my $vapour_point;
my $heat_capacity_solid;
my $heat_capacity_liquid;
my $heat_capacity_gas;
my $heat_of_fusion;
my $heat_of_vapour;
my $heat_of_breathe;

my $store_temperature;
my $store_temperature_2;
my $store_humidity;
my $store_duration;

my $store_temperature_fresh;
my $store_temperature_fresh_2;
my $store_humidity_fresh;
my $store_duration_fresh;

my $store_temperature_frozen;
my $store_humidity_frozen;
my $store_duration_frozen;

my $heat_conductivity;
my $thermal_expansion;
my $heat_value_lower;
my $heat_value_upper;
my $electrical_conductivity;
my $hashref;

# Keep all the lines in this list, to sort later
my $cool_good_entry="";
my @cool_good_lines=();
my $substance_entry="";
my @substance_lines=();
my $build_mat_entry="";
my @build_mat_lines=();

my $header_height="\\rule[-3.5mm]{0mm}{10mm} ";

my @meat=();         #1
my $meat_header;
my @fish=();         #2
my $fish_header;
my @dairy=();        #3
my $dairy_header;
my @other=();        #4
my $other_header;
my @vegetables=();   #5
my $vegetables_header;
my @fruit=();        #6
my $fruit_header;

my @metals=();       #100
my $metals_header;
my @fluids=();       #110
my $fluids_header;
my @gases=();       #115
my $gases_header;

my @construction=();       #202
my $construction_header;
my @insulation=();   #205
my $insulation_header;

############################################################
# Reading the Database
while (($name, $hashref) = each %substances) {
    #leads to an endless loop, why???
    #$name_lang=&coldformix::translate($name, $lang, \%substances);

    print "### Working on $name, type  $substances{$name}{'Type'}\n";

    if (defined $substances{$name}{"$lang"}) {
        # found
        $name_lang=$substances{$name}{"$lang"};
    } else {
        # not found
        $name_lang="\\textcolor{red}{"."$substances{$name}{'de'}"."(de)}";
    }

    if (defined $substances{$name}{"${lang}_comment"}) {
        # found
        $comment_lang=$substances{$name}{"${lang}_comment"};
    } else {
        # not found
        $comment_lang="";
    }
    $type=$substances{$name}{"Type"};
    
    $density=$substances{$name}{'Density'};
    if (defined $density) {
        $density=&coldformix::strip("$density");
    } else {
        $density="";
    }

    $melt_point=$substances{$name}{'Melting_Point'};
    if (defined $melt_point) {
       $melt_point=&coldformix::strip("$melt_point");
    } else {
       $melt_point="";
    }

    $vapour_point=$substances{$name}{'Vapour_Point'};
    if (defined $vapour_point){
       $vapour_point=&coldformix::strip("$vapour_point");
    } else {
       $vapour_point="";
    }

    $heat_capacity_solid=$substances{$name}{'Heat_Capacity_Solid'};
    if (defined  $heat_capacity_solid) {
        $heat_capacity_solid=&coldformix::strip("$heat_capacity_solid");
    } else {
	$heat_capacity_solid="";
    }

    $heat_capacity_liquid=$substances{$name}{'Heat_Capacity_Liquid'};
    if (defined $heat_capacity_liquid){
        $heat_capacity_liquid=&coldformix::strip("$heat_capacity_liquid");
    } else {
	$heat_capacity_liquid="";
    }

    $heat_capacity_gas=$substances{$name}{'Heat_Capacity_Gas'};
    if (defined $heat_capacity_gas){
        $heat_capacity_gas=&coldformix::strip("$heat_capacity_gas");
    } else {
	$heat_capacity_gas="";
    }

    $heat_of_fusion=$substances{$name}{'Heat_of_Fusion'};
    if (defined $heat_of_fusion){
        $heat_of_fusion=&coldformix::strip("$heat_of_fusion");
    } else {
	$heat_of_fusion="";
    }

    $heat_of_vapour=$substances{$name}{'Heat_of_Vapour'};
    if (defined  $heat_of_vapour){
        $heat_of_vapour=&coldformix::strip("$heat_of_vapour");
    } else {
        $heat_of_vapour="";
    }

    $heat_of_breathe=$substances{$name}{'Heat_of_Breathe'};
    if (defined  $heat_of_breathe){       
        $heat_of_breathe=&coldformix::strip("$heat_of_breathe");
    } else {
        $heat_of_breathe="";
    }

    $store_temperature=$substances{$name}{'Store_Temperature'};
    if (defined $store_temperature){
       $store_temperature=&coldformix::checkdef($store_temperature);
       $store_temperature=&coldformix::sign($store_temperature);
    } else {
       $store_temperature="";
    }

    $store_temperature_2=$substances{$name}{'Store_Temperature_2'};
    if (defined $store_temperature_2){
       $store_temperature_2=&coldformix::strip($store_temperature_2);
       $store_temperature_2=&coldformix::sign($store_temperature_2);
    }

    $store_humidity=$substances{$name}{'Store_Humidity'};
    if (defined $store_humidity){
       $store_humidity=&coldformix::checkdef($store_humidity,"string");
    }

    $store_duration=$substances{$name}{'Store_Duration'};
    if (defined $store_duration){
        $store_duration=&coldformix::checkdef($store_duration);
    } else {
        $store_duration="";
    }

    $store_temperature_fresh=$substances{$name}{'Store_Temperature_Fresh'};
    if (defined $store_temperature_fresh){
        $store_temperature_fresh=&coldformix::strip("$store_temperature_fresh"); 
        $store_temperature_fresh=&coldformix::sign($store_temperature_fresh);
    }

    $store_temperature_fresh_2=$substances{$name}{'Store_Temperature_Fresh_2'};
    if (defined $store_temperature_fresh_2){
      $store_temperature_fresh_2=&coldformix::strip("$store_temperature_fresh_2");
      $store_temperature_fresh_2=&coldformix::sign($store_temperature_fresh_2); 
    }

    $store_humidity_fresh=$substances{$name}{'Store_Humidity_Fresh'};

    $store_duration_fresh=$substances{$name}{'Store_Duration_Fresh'};

    $store_temperature_frozen=$substances{$name}{'Store_Temperature_Frozen'};
    if (defined $store_temperature_frozen){
        $store_temperature_frozen=&coldformix::strip("$store_temperature_frozen");
    }

    $store_humidity_frozen=$substances{$name}{'Store_Humidity_Frozen'};

    $store_duration_frozen=$substances{$name}{'Store_Duration_Frozen'};

    $heat_conductivity=$substances{$name}{'Heat_Conductivity'};
    if (defined $heat_conductivity) {
        $heat_conductivity=&coldformix::strip("$heat_conductivity");
    } else {
        $heat_conductivity="";
    }

    $thermal_expansion=$substances{$name}{'Thermal_Expansion'};
    if (defined $thermal_expansion) {
        $thermal_expansion=&coldformix::strip("$thermal_expansion");
    } else {
        $thermal_expansion="";
    }

    $heat_value_lower=$substances{$name}{'Heat_Value_Lower'};
    if (not defined $heat_value_lower) {
        $heat_value_lower=$substances{$name}{'Heat_Value_Lower_n'};
        if (not defined $heat_value_lower) {
            $heat_value_lower="";
        }
    }

    $heat_value_upper=$substances{$name}{'Heat_Value_Upper'};
    if (not defined $heat_value_upper) {
        $heat_value_upper=$substances{$name}{'Heat_Value_Upper_n'};
        if (not defined $heat_value_upper) {
           $heat_value_upper="";
       }
    }



    $electrical_conductivity=$substances{$name}{'Electrical_Conductivity'};


    ##################################################
    # cool goods
    if ($type<100 && $type>0){
       # Line 1
       if (defined $store_temperature and 
           defined $store_temperature_2){
           $store_temperature="\\multicolumn{1}{c|}{".
           "\$$store_temperature\$".
           "/".
           "\$$store_temperature_2\$".
           "}";
          }

       $latex_line_cool_good_1="$name_lang & ".
                   "$store_temperature & ".
                   "$store_humidity & ".
                   "$store_duration & ".
                   "$melt_point & ".
                   "$heat_capacity_liquid & ".
                   "$heat_capacity_solid& ".
                   "$heat_of_fusion& ".
                   "$heat_of_breathe ";

       # Line 2, if a value is defined
       if (defined $store_temperature_fresh or
           defined $store_humidity_fresh or
           defined $store_duration_fresh) {
          $latex_line_cool_good_1=$latex_line_cool_good_1." \\tabularnewline[-0.7mm] ";
          #check if there are 2 store_temperatures_fresh
          if (defined $store_temperature_fresh and 
              defined $store_temperature_fresh_2){
	      $store_temperature_fresh="\\multicolumn{1}{c|}{".
              "\$$store_temperature_fresh\$".
              "/".
              "\$$store_temperature_fresh_2\$".
              "}";
          }
           
          $latex_line_cool_good_2="\\,\\,--".
                   &coldformix::translate("frisch", $lang, \%trans).
                   " & ".
                   "$store_temperature_fresh & ".
                   "$store_humidity_fresh & ".
                   "$store_duration_fresh& ".
                   "& ".
                   "& ".
                   "& ".
                   "& ".
                   " ".
                   " \\tabularnewline[-0.7mm]  \n";
       } else {
          $latex_line_cool_good_1=$latex_line_cool_good_1." \\tabularnewline ";
          $latex_line_cool_good_2="";
       }

       # Line 3, if a value is defined
       if (defined $store_temperature_frozen or
           defined $store_humidity_frozen or
           defined $store_duration_frozen
          ) {
	  $store_temperature_frozen = "na" if not defined $store_temperature_frozen;
          $store_humidity_frozen = "na" if not defined $store_humidity_frozen;
          $store_duration_frozen = "na" if not defined $store_duration_frozen;

          $latex_line_cool_good_3="\\,\\,--".
                   &coldformix::translate("gefroren", $lang, \%trans).
                   " & ".
                   "$store_temperature_frozen& ".
                   "$store_humidity_frozen  & ".
                   "$store_duration_frozen & ".
                   "& ".
                   "& ".
                   "& ".
                   "& ".
                   " ".
                   " \\\\ \n";
       } else {
          $latex_line_cool_good_3="";
       }

      # create the entry 
      $cool_good_entry="$latex_line_cool_good_1".
                       "$latex_line_cool_good_2".
                       "$latex_line_cool_good_3".
                       "\\hline \n ";
       if ($type==1){
           push @meat, $cool_good_entry;
       } elsif($type==2){
           push @fish, $cool_good_entry;
       } elsif($type==3){
           push @dairy, $cool_good_entry;
       } elsif($type==4){
           push @other, $cool_good_entry;
       } elsif($type==5){
           push @vegetables, $cool_good_entry;
       } elsif($type==6){
           push @fruit, $cool_good_entry;
       }
    }


    ##################################################
    # substances
    if ($type>=100 and $type<200){
       $latex_line_substance="$name_lang & ".
                   "$density & ".
                   "$melt_point & ".
                   "$vapour_point & ".
                   "$heat_capacity_solid & ".
                   "$heat_capacity_liquid & ".
                   "$heat_capacity_gas & ".
                   "$heat_of_fusion & ".
                   "$heat_of_vapour & ".
                   "$heat_conductivity &".
                   "$thermal_expansion &".
                   "$heat_value_lower &".
                   "$heat_value_upper ".
                   " \\\\ \n";
      $substance_entry="$latex_line_substance".
                       "\\hline ";
      if ($type==100){
           push @metals, $substance_entry;
       } elsif($type==110){
           push @fluids, $substance_entry;
       } elsif($type==115){
           push @gases, $substance_entry;
       }

       push @substance_lines, $latex_line_substance;
   }

   ##################################################
   # building materials
   if ($type>=200 and $type<300){
       $latex_line_build_mat="$name_lang & ".
                   "$density & ".
                   "$heat_capacity_solid & ".
                   "$heat_conductivity &".
                   "$comment_lang".
                   " \\\\ \n";

      $build_mat_entry="$latex_line_build_mat".
                       "\\hline ";

       if ($type==202){
           push @construction, $build_mat_entry;
       } elsif($type==205){
           push @insulation, $build_mat_entry;
       }

       push @build_mat_lines, $latex_line_build_mat;
   }




} # end while loop




############################################################
# sorting cool_good
@meat = sort alphabetical @meat;
@fish = sort alphabetical @fish;
@dairy = sort alphabetical @dairy;
@other = sort alphabetical @other;
@vegetables = sort alphabetical @vegetables;
@fruit = sort alphabetical @fruit;

$meat_header="\\hline \n\\multicolumn{9}{|l|}{\\bfseries ".$header_height.
             &coldformix::translate("Fleisch", $lang, \%trans).
             " } \\\\ \n\\hline \n\\hline \n ";
$fish_header="\\hline \n\\multicolumn{9}{|l|}{\\bfseries ".$header_height.
             &coldformix::translate("Fisch", $lang, \%trans).
             " } \\\\ \n\\hline \n\\hline \n ";
$dairy_header="\\hline \n\\multicolumn{9}{|l|}{\\bfseries ".$header_height.
              &coldformix::translate("Molkereiprodukte", $lang, \%trans).
              " }\\\\ \n\\hline \n\\hline \n ";
$other_header="\\hline \n\\multicolumn{9}{|l|}{\\bfseries ".$header_height.
              &coldformix::translate("Sonstige Khlgter", $lang, \%trans).
              " } \\\\ \n\\hline \n\\hline \n ";
$vegetables_header="\\hline \n \\multicolumn{9}{|l|}{\\bfseries ".$header_height.
                   &coldformix::translate("Gemse", $lang, \%trans).
                   " } \\\\ \n\\hline \n\\hline \n ";
$fruit_header="\\hline \n\\multicolumn{9}{|l|}{\\bfseries ".$header_height.
              &coldformix::translate("Obst", $lang, \%trans).
              " } \\\\ \n\\hline \n\\hline \n ";

@cool_good_lines = ($meat_header,
                    @meat,
                    $fish_header,
                    @fish,
                    $dairy_header,
                    @dairy,
                    $other_header,
                    @other,
                    $vegetables_header,
                    @vegetables,
                    $fruit_header,
                    @fruit);



############################################################
# sorting substances
@metals = sort alphabetical @metals;
@fluids = sort alphabetical @fluids;
@gases = sort alphabetical @gases;

$metals_header="\\hline \n\\multicolumn{10}{|l|}{\\bfseries ".$header_height.
             &coldformix::translate("Metalle", $lang, \%trans)." } ".
             " & \\multicolumn{1}{c|}{\$$symbols{'length_expansion'}{'si_symbol'}\$}".
             " & \\multicolumn{1}{c|}{\$$symbols{'heat_value'}{'si_symbol'}\$}".
             " & \\multicolumn{1}{c|}{\$$symbols{'heat_value_upper'}{'si_symbol'}\$}".

"  \\\\ \n\\hline \n\\hline ";
$fluids_header="\\hline \n\\multicolumn{10}{|l|}{\\bfseries ".$header_height.
              &coldformix::translate("Flssigkeiten", $lang, \%trans)." } ".
              " & \\multicolumn{1}{c|}{\$$symbols{'volume_expansion'}{'si_symbol'}\$}".
              " & \\multicolumn{1}{c|}{\$$symbols{'heat_value'}{'si_symbol'}\$}".
              " & \\multicolumn{1}{c|}{\$$symbols{'heat_value_upper'}{'si_symbol'}\$}".
              " \\\\ \n\\hline \n\\hline ";
$gases_header="\\hline \n\\multicolumn{10}{|l|}{\\bfseries ".$header_height.
              &coldformix::translate("Gase", $lang, \%trans)." } ".
              " & \\multicolumn{1}{c|}{\$$symbols{'volume_expansion'}{'si_symbol'}\$}".
              " & \\multicolumn{1}{c|}{\$$symbols{'heat_value_gas'}{'si_symbol'}\$}".
              " & \\multicolumn{1}{c|}{\$$symbols{'heat_value_gas_upper'}{'si_symbol'}\$}".
              " \\\\ \n\\hline \n\\hline ";

@substance_lines = ($metals_header,
                    @metals,
                    $fluids_header,
                    @fluids,
                    $gases_header,
                    @gases);


############################################################
# sorting building materials
@construction = sort @construction;
@insulation = sort @insulation;

$construction_header="\\hline \n\\multicolumn{5}{|l|}{\\bfseries ".$header_height.
             &coldformix::translate("Baustoffe", $lang, \%trans).
             " } \\\\ \n\\hline \n\\hline ";
$insulation_header="\\hline \n\\multicolumn{5}{|l|}{\\bfseries ".$header_height.
             &coldformix::translate("Dmmstoffe", $lang, \%trans).
             " } \\\\ \n\\hline \n\\hline ";

@build_mat_lines = ($construction_header,
                    @construction,
                    $insulation_header,
                    @insulation,
                   );



############################################################
#  printing the cool goods

open(LATEXOUT,">../coldformix-generated/$lang/latex/cool_good-$lang.tex");
 
print LATEXOUT "\\begin{longtable}{",
                                  "|l",
                                  "|D{.}{,}{3.1}",
                                  "|c",
                                  "|c",
                                  "|D{.}{,}{3.1}",
                                  "|D{.}{,}{2.3}",
                                  "|D{.}{,}{2.3}",
                                  "|D{.}{,}{3.0}",
                                  "|D{.}{,}{3.2}",
                                  "|} \\hline\n";

print LATEXOUT "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{Lager-}  & ",
    "\\multicolumn{1}{|c}{Lager-}  & ",
    "\\multicolumn{1}{|c}{Lager-}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'t_melt'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_liquid'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_solid'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'enthalpy_melt'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c|}{\$$symbols{'spec_heat_breathe'}{'si_symbol'}\$}   ",
    "\\tabularnewline \n";


print LATEXOUT "\\multicolumn{1}{|c}{Khlgut}  & ",
    "\\multicolumn{1}{|c}{temperatur}  & ",
    "\\multicolumn{1}{|c}{-feuchte}  & ",
    "\\multicolumn{1}{|c}{-dauer}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c|}{in}   ",
    "\\tabularnewline \n";

print LATEXOUT "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{\$$units{'temperature'}{'latex_0'}\$}  & ",
    "\\multicolumn{1}{|c}{\\%}  & ",
    "\\multicolumn{1}{|c}{d}  & ",
    "\\multicolumn{1}{|c}{\$$units{'temperature_c'}{'latex_0'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'spec_energy'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c|}{\$$units{'spec_heat_flow'}{'latex_1'}\$}  ",
    "\\tabularnewline[1mm] \\hline \\hline \n";
print LATEXOUT "\\endhead \n\n",
               "      \\hline"," \n";
print LATEXOUT "   \\endfoot ","\n","\\hline \n",
#               "   \\\\ ","\n",
               "   \\endlastfoot ","\n";

foreach my $line (@cool_good_lines){
   print LATEXOUT $line;
}

print LATEXOUT "\\end{longtable} \n";

close LATEXOUT;





############################################################
# printing the substances

# footnotes in multilie Headers of longtables are not working
#my $footnote='$'."$symbols{'length_expansion'}{'si_symbol'}".'$';
#my $solid=&coldformix::translate("bei Festkrpern", $lang, \%trans);
#$footnote=$footnote." $solid".", ";
#
#$footnote=$footnote.'$'."$symbols{'volume_expansion'}{'si_symbol'}".'$';
#my $liquid=&coldformix::translate("bei Flssigkeiten/Gasen", $lang, \%trans);
#$footnote=$footnote." $liquid".".";

open(LATEXOUT2,">../coldformix-generated/$lang/latex/substances-$lang.tex");

print LATEXOUT2 "\\begin{longtable}{",
                                  "|l",
                                  "|D{.}{,}{2.4}",
                                  "|D{.}{,}{4.1}",
                                  "|D{.}{,}{4.1}",
                                  "|D{.}{,}{1.3}",
                                  "|D{.}{,}{1.3}",
                                  "|D{.}{,}{3.2}",
                                  "|D{.}{,}{3.2}",
                                  "|D{.}{,}{5.1}",
                                  "|D{.}{,}{3.4}",
                                  "|D{.}{,}{4.1}",
                                  "|D{.}{,}{5.0}",
                                  "|D{.}{,}{5.0}",
                                  "|} \\hline\n";

print LATEXOUT2 "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'density'}{'si_symbol'}\$}  & ",      
    "\\multicolumn{1}{|c}{\$$symbols{'t_melt'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'t_evap'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_solid'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_liquid'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_gas_p'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'enthalpy_melt'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'enthalpy_evap'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c|}{\$$symbols{'heat_conductivity'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{c|}{\$$symbols{'length_expansion'}{'si_symbol'}"."/".
                            "$symbols{'volume_expansion'}{'si_symbol'}\$} & ",
    "\\multicolumn{1}{c|}{\$$symbols{'heat_value'}{'si_symbol'}"."/"."\\hspace{-0.4ex}".
                            "$symbols{'heat_value_gas'}{'si_symbol'}\$} & ",
    "\\multicolumn{1}{c|}{\$$symbols{'heat_value_upper'}{'si_symbol'}"."/"."\\hspace{-0.4ex}".
                            "$symbols{'heat_value_gas_upper'}{'si_symbol'}\$}",
    "\\tabularnewline \n";

print LATEXOUT2 "\\multicolumn{1}{|c}{Stoff}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c|}{in}  & ",
    "\\multicolumn{1}{c|}{in} ",
    "\\tabularnewline \n";

print LATEXOUT2 "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{\$$units{'density'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'temperature'}{'latex_0'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'temperature'}{'latex_0'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'spec_energy'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'spec_energy'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_conductivity'}{'latex_5'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'thermal_expansion'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c|}{\$$units{'spec_energy'}{'latex_1'}\$} &",
    "\\multicolumn{1}{c|}{\$$units{'spec_energy'}{'latex_1'}\$} ",
    "\\tabularnewline[1mm] \\hline \\hline \n";
print LATEXOUT2 "\\endhead \n\n",
               "      \\hline"," \n";
print LATEXOUT2 "   \\endfoot ","\n","\\hline \n",
#               "    \\multicolumn{11}{|l}{Fortsetung auf der nchsten Seite ...} \\\\","\n",
               "   \\endlastfoot ","\n";


#@substance_lines = sort @substance_lines;

foreach my $substance_line (@substance_lines){
   print LATEXOUT2 "$substance_line \n";
}

print LATEXOUT2 "\\end{longtable} \n";

close LATEXOUT2;






############################################################
# printing the building-materials
open(LATEXOUT3,">../coldformix-generated/$lang/latex/building-materials-$lang.tex");

print LATEXOUT3 "\\begin{longtable}{",
                                  "|l",
                                  "|D{.}{,}{2.4}",
                                  "|D{.}{,}{1.3}",
                                  "|D{.}{,}{1.3}",
                                  "|l",
                                  "|} \\hline\n";

print LATEXOUT3 "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'density'}{'si_symbol'}\$}  & ",      
    "\\multicolumn{1}{|c}{\$$symbols{'heat_capacity_solid'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$symbols{'heat_conductivity'}{'si_symbol'}\$}  & ",
    "\\multicolumn{1}{|c|}{}",
    "\\tabularnewline \n";


print LATEXOUT3 "\\multicolumn{1}{|c}{Stoff}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c}{in}  & ",
    "\\multicolumn{1}{|c|}{Bemerkungen} ",
    "\\tabularnewline \n";

print LATEXOUT3 "\\multicolumn{1}{|c}{}  & ",
    "\\multicolumn{1}{|c}{\$$units{'density'}{'latex_1'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_capacity'}{'latex_0'}\$}  & ",
    "\\multicolumn{1}{|c}{\$$units{'heat_conductivity'}{'latex_0'}\$} &",
    "\\multicolumn{1}{|c|}{} ",
    "\\tabularnewline[1mm] \\hline \\hline \n";

#print LATEXOUT3 "\\endhead \n\n",
#               "      \\hline"," \n";

#print LATEXOUT3 "   \\endfoot ","\n",
#               "     \\\\ \\hline ","\n",
#               "   \\endlastfoot ","\n";


foreach my $build_mat_line (@build_mat_lines){
   print LATEXOUT3 "$build_mat_line \n";
}

print LATEXOUT3 "\\end{longtable} \n";

close LATEXOUT3;


sub alphabetical {
    my $strip_a=$a;
    my $strip_b=$b;
    $strip_a=~s//A/g;
    $strip_b=~s//A/g;
    $strip_a=~s//a/g;
    $strip_b=~s//a/g;
    $strip_a=~s//O/g;
    $strip_b=~s//O/g;
    $strip_a=~s//o/g;
    $strip_b=~s//o/g;
    $strip_a=~s//U/g;
    $strip_b=~s//U/g;
    $strip_a=~s//u/g;
    $strip_b=~s//u/g;
    $strip_a cmp $strip_b;
}



