#! /usr/local/bin/perl -s
#
# Usage: xl2ddb  -p=plotlist_file -d=datalist_file -r=references_file
#
# Use this program to turn DOS / OS/2 Text files from Excel into
# some formatted hyperlatex code.
#
# If there are no command-line switches, xl2ddb looks for the files
# "plotlist", "datalist", and "references".  If any of the switches
# -P, -d, or -r are used, it will use the file names specified with them.
#
# Documentation needed:
#  1. What files it needs, and formatting rules
#     (plots.tex, references, various data_*.tex)
#  2. What files it creates
#

# The Beginning

$datadir="/home/vipera/detn_db/data/texdata/";

print "Opening files for reading and writing.\n";
# Open files for reading
if ($p)
   { open(PLOT_LIST, $p) || die "Couldn't open plotlist file!\n"; }
else
  { open(PLOT_LIST, "plotlist") || die "Couldn't open plotlist file!\n"; }

if ($d)
   { open(DATA_LIST, $d) || die "Couldn't open data list file!\n"; }
else
   { open(DATA_LIST, "datalist") || die "Couldn't open data list file!\n"; }

if ($r)
   { open(REFERENCE, $r) || die "Couldn't open references file!\n"; }
else
   { open(REFERENCE, "references") || die "Couldn't open references file!\n"; }


# Open files for writing
&open_data();
&open_plot();
open(REFBIB, "> references.bib") || die "Couldn't open references.bib file!\n";

# Doing the references thing
print "Translating references file -> references.bib.\n";
# Read references file
$ref=<REFERENCE>;
while($ref=<REFERENCE>) {
   chop($ref);
   &decode_ref($ref,*Shortref,*Shrefid);
}

# Write data tex files
$ref=<DATA_LIST>;
$ref=<DATA_LIST>;
print "Creating data LaTeX files.";
while($ref=<DATA_LIST>) {
   print ".";
   chop($ref);
   &decode_data($ref,$datadir,*Shortref,*Shrefid,*Dfile,*Digit,*Accur,
 *Refid,*Temper,*Pressur,*Equivrat,*Perdil,*Subcat,*Fuel,*Oxidiz,
 *Diluent);
}
print "\n";


# Write plot tex files
$ref=<PLOT_LIST>;
print "Creating plot LaTeX files.\n";
while($ref=<PLOT_LIST>) {
   chop($ref);
   &decode_plot($ref,*Shortref,*Shrefid,*Dfile,*Refid,*Temper,*Pressur, 
 *Equivrat,*Perdil,*Subcat,*Fuel,*Oxidiz,*Diluent);
}

print "Finished!\n";
# The End


# Open data tex files for writing
sub open_data {

#open(CELL_WIDTH, "> data_cell_width.tex") || die \
#                 "Couldn't open data_cell_width.tex file!\n";
open(CELL_WIDTH_H2, "> data_cell_width_H2.tex") || die
                 "Couldn't open data_cell_width_H2.tex file!\n";
open(CELL_WIDTH_CH4, "> data_cell_width_CH4.tex") || die
                 "Couldn't open data_cell_width_CH4.tex file!\n";
open(CELL_WIDTH_C2H2, "> data_cell_width_C2H2.tex") || die
                 "Couldn't open data_cell_width_C2H2.tex file!\n";
open(CELL_WIDTH_C2H4, "> data_cell_width_C2H4.tex") || die
                 "Couldn't open data_cell_width_C2H4.tex file!\n";
open(CELL_WIDTH_MISC, "> data_cell_width_misc.tex") || die
                 "Couldn't open data_cell_width_misc.tex file!\n";
open(CELL_LENGTH_H2, "> data_cell_length_H2.tex") || die
                 "Couldn't open data_cell_length_H2.tex file!\n";
open(CELL_LENGTH_MISC, "> data_cell_length_misc.tex") || die
                 "Couldn't open data_cell_length_misc.tex file!\n";
open(CELL_UNSORT, "> data_cell_unsort.tex") || die
                 "Couldn't open data_cell_unsort.tex file!\n";
open(CRIT_TUBE_H2, "> data_crit_tube_H2.tex") || die
                "Couldn't open data_crit_tube_H2.tex file!\n";
open(CRIT_TUBE_CH4, "> data_crit_tube_CH4.tex") || die
                "Couldn't open data_crit_tube_CH4.tex file!\n";
open(CRIT_TUBE_C2H2, "> data_crit_tube_C2H2.tex") || die
                "Couldn't open data_crit_tube_C2H2.tex file!\n";
open(CRIT_TUBE_C2H4, "> data_crit_tube_C2H4.tex") || die
                "Couldn't open data_crit_tube_C2H4.tex file!\n";
open(CRIT_TUBE_MISC, "> data_crit_tube_misc.tex") || die
                "Couldn't open data_crit_tube_misc.tex file!\n";
open(CRIT_ENERGY_H2, "> data_crit_energy_H2.tex") || die
                  "Couldn't open data_crit_energy_H2.tex file!\n";
open(CRIT_ENERGY_CH4, "> data_crit_energy_CH4.tex") || die
                  "Couldn't open data_crit_energy_CH4.tex file!\n";
open(CRIT_ENERGY_C2H2, "> data_crit_energy_C2H2.tex") || die
                  "Couldn't open data_crit_energy_C2H2.tex file!\n";
open(CRIT_ENERGY_C2H4, "> data_crit_energy_C2H4.tex") || die
                  "Couldn't open data_crit_energy_C2H4.tex file!\n";
open(CRIT_ENERGY_MISC, "> data_crit_energy_misc.tex") || die
                  "Couldn't open data_crit_energy_misc.tex file!\n";
open(MIN_TUBE, "> data_min_tube.tex") || die
                  "Couldn't open data_min_tube.tex file!\n";
open(MISC_DAT, "> data_misc.tex") || die "Couldn't open data_misc.tex file!\n";

}


# Open plot tex files for writing
sub open_plot {

open(PLOT_CELL_SIZE, "> plot_cell_size.tex") || die
                      "Couldn't open plot_cell_size.tex file!\n";
open(PLOT_CRIT_TUBE, "> plot_crit_tube.tex") || die
                      "Couldn't open plot_crit_tube.tex file!\n";
open(PLOT_CRIT_ENERGY, "> plot_crit_energy.tex") || die
                      "Couldn't open plot_crit_energy.tex file!\n";
open(PLOT_MIN_TUBE, "> plot_min_tube.tex") || die
                      "Couldn't open plot_min_tube.tex file!\n";
open(PLOT_MISC, "> plot_misc.tex") || die
                      "Couldn't open plot_misc.tex file!\n";

}


# Decode references input and write Bibtex file
sub decode_ref {
   local($lref) = $_[0];
   local(*Shortref) = $_[1];
   local(*Shrefid) = $_[2];
   $lref =~ s/"//g;
   @REF     = split(/\t/,$lref);
   $ID      = $REF[0];
   $year    = $REF[1];
   $authors = $REF[2];
   $title   = $REF[3];
   $pub     = $REF[4];
   $volume  = $REF[5];
   $number  = $REF[6];
   $pp      = $REF[7];
   $type    = $REF[8];

   $pp =~ s/-/--/;    # change - to -- in page ranges
   $authors = &arrange_auth($authors,*Shortref,*year);  # rearrange authors
   push(@Shrefid,$ID);

   print REFBIB "\@\U$type\E\{$ID,\n";
   print REFBIB "  author = \"$authors\",\n";
   print REFBIB "  title = \"$title\",\n";
   if ("\U$type" =~ /ARTICLE/)
     { print REFBIB "  journal = \"$pub\",\n"; }
   elsif ("\U$type" =~ /INPROCEEDINGS/)
     { print REFBIB "  booktitle = \"$pub\",\n"; }
   elsif ("\U$type" =~ /INCOLLECTION/)
     { print REFBIB "  booktitle = \"$pub\",\n";
       print REFBIB "  publisher = \"$number\",\n"; }
   elsif ("\U$type" =~ /PHDTHESIS/)
     { print REFBIB "  school = \"$pub\",\n"; }
   elsif ("\U$type" =~ /TECHREPORT/)
     { print REFBIB "  institution = \"$pub\",\n"; }
   else
     { print REFBIB "  journal = \"$pub\",\n"; }
   print REFBIB "  year = \"$year\",\n";
   print REFBIB "  volume = \"$volume\",\n";
   if ("\U$type" !~ /INCOLLECTION/)
     { print REFBIB "  number = \"$number\",\n"; }
   print REFBIB "  pages = \"$pp\"\}\n";
   print REFBIB "\n";
}


# Rearrange the authors' names
sub arrange_auth {
   local($authorlist) = $_[0];
   local(*Shortref) = $_[1];
   local(*year) = $_[2];

   @broken = split(/\,\s/,$authorlist);
   @main = split(/\s/,@broken[0]);
   foreach $name (0 .. $#broken) {
     @person = split(/\s/,$broken[$name]);
     $broken[$name] = $person[1].' '.$person[0];
   }
   push(@Shortref, $main[0]." ($year)");
   $authorlist = join(' and ',@broken);
}


# Rearrange and print data tex files
sub decode_data {
   local($entry) =    $_[0];
   $datadir =         $_[1];
   local(*Shortref) = $_[2];
   local(*Shrefid) =  $_[3];
   local(*Dfile) =    $_[4];
   local(*Digit) =    $_[5];
   local(*Accur) =    $_[6];
   local(*Refid) =    $_[7];
   local(*Temper) =   $_[8];
   local(*Pressur) =  $_[9];
   local(*Equivrat) = $_[10];
   local(*Perdil) =   $_[11];
   local(*Subcat) =   $_[12];
   local(*Fuel) =     $_[13];
   local(*Oxidiz) =   $_[14];
   local(*Diluent) =  $_[15];

   $entry =~ s/"//g;
   @line = split(/\t/,$entry);
   $datafile = $line[0];
   $digitize = $line[1];
   $accurate = $line[2];
   $referenc = $line[3];
   $autho  = $line[4];
   $date   = $line[5];
   $figur  = $line[6];
   $categ  = $line[7];
   $subcat = $line[8];
   $fuel   = $line[9];
   $oxid   = $line[10];
   $diluen = $line[11];
   $press0 = $line[12];
   $temp0  = $line[13];
   $equivr = $line[14];
   $dilper = $line[15];
   $commen = $line[16];

   push(@Dfile,$datafile);
   push(@Digit,$digitize);
   push(@Accur,$accurate);
   push(@Refid,$referenc);
   push(@Temper,$temp0);
   push(@Pressur,$press0);
   push(@Equivrat,$equivr);
   push(@Perdil,$dilper);
   push(@Subcat,$subcat);
   push(@Fuel,$fuel);
   push(@Oxidiz,$oxid);
   push(@Diluent,$diluen);

   foreach $id (0 .. $#Shrefid) {
     if ($Shrefid[$id] eq $referenc) {
       $lshortref = $Shortref[$id];
     }
   }


   if ($categ =~ /cell size/) {
     if ($subcat =~ /width/) {
       if ($fuel eq "H2")
         { open(FILE_HAND, ">&CELL_WIDTH_H2");  }
       elsif ($fuel eq "CH4")
         { open(FILE_HAND, ">&CELL_WIDTH_CH4");  }
       elsif ($fuel eq "C2H2")
         { open(FILE_HAND, ">&CELL_WIDTH_C2H2");  }
       elsif ($fuel eq "C2H4")
         { open(FILE_HAND, ">&CELL_WIDTH_C2H4");  }
       else
         { open(FILE_HAND, ">&CELL_WIDTH_MISC");  }
       }
       elsif ($subcat =~ /length/) {
         if ($fuel eq "H2")
           { open(FILE_HAND, ">&CELL_LENGTH_H2");  }
         else
           { open(FILE_HAND, ">&CELL_LENGTH_MISC");  }
       }
       else 
         { open(FILE_HAND, ">&CELL_UNSORT");  }
     }
   elsif ($categ =~ /critical tube/) {
     if ($fuel eq "H2")
       { open(FILE_HAND, ">&CRIT_TUBE_H2"); }
     elsif ($fuel eq "CH4")
       { open(FILE_HAND, ">&CRIT_TUBE_CH4"); }
     elsif ($fuel eq "C2H2")
       { open(FILE_HAND, ">&CRIT_TUBE_C2H2"); }
     elsif ($fuel eq "C2H4")
       { open(FILE_HAND, ">&CRIT_TUBE_C2H4"); }
     else
       { open(FILE_HAND, ">&CRIT_TUBE_MISC"); }
   }
   elsif ($categ =~ /critical energy/) {
     if ($fuel eq "H2")
       { open(FILE_HAND, ">&CRIT_ENERGY_H2"); }
     elsif ($fuel eq "CH4")
       { open(FILE_HAND, ">&CRIT_ENERGY_CH4"); }
     elsif ($fuel eq "C2H2")
       { open(FILE_HAND, ">&CRIT_ENERGY_C2H2"); }
     elsif ($fuel eq "C2H4")
       { open(FILE_HAND, ">&CRIT_ENERGY_C2H4"); }
     else
       { open(FILE_HAND, ">&CRIT_ENERGY_MISC"); }
   }
   elsif ($categ =~ /minimum tube/)
     { open(FILE_HAND, ">&MIN_TUBE"); }
   else
     { open(FILE_HAND, ">&MISC_DAT"); }

   if (($digitize eq "Y") && ($accurate eq "Y")) {

     $count = 0;
     open(datafile, $datadir.$datafile.".txt");
     $firstline = <datafile>;
     while ($firstline =~ s/\&//){
       $count++;
     }
     print FILE_HAND "\\W\\begin\{center\}\n";
     print FILE_HAND "\\W\\begin\{table\}[!ht]\n";
     print FILE_HAND "\\W\\xlink\{$datafile\.txt\}\{\\rawdatadir";
     print FILE_HAND "/$datafile\.txt\}~$lshortref~\\cite\{$referenc\}\n";
     print FILE_HAND "\\W\\label\{$datafile\}\n";
     print FILE_HAND "\\W\\end\{table\}\n";
     print FILE_HAND "\\W\\end\{center\}\n";

     $texgreeting = "\\T\\begin\{longtable\}\{";
     for (0..$count) {$texgreeting .= "c"; }
     $texgreeting .= "\}\n";
     print FILE_HAND $texgreeting;
     print FILE_HAND "\\T\\caption\{$datafile~\\protect";
     print FILE_HAND "\\cite[$lshortref]\{$referenc\}";
     print FILE_HAND "\\label\{$datafile\}\}\\\\\n";
     print FILE_HAND "\\T\\input\{\\datadir/$datafile\.txt\}\n";
     print FILE_HAND "\\T\\end\{longtable\}\n";
     print FILE_HAND "\n";

     print FILE_HAND "\\begin\{center\}\n";
     print FILE_HAND "\\begin\{tabular\}\{rlrl\}\n";
     print FILE_HAND "Category: \& $categ \& Fuel: \& $fuel \\\\\n";
     print FILE_HAND "Sub-Category: \& $subcat \& Oxidizer: \& $oxid \\\\\n";
     print FILE_HAND "Initial Pressure: \& $press0 kPa ";
     print FILE_HAND "\& Diluent: \& $diluen \\\\\n";
#     if ($dilper)
#        { print FILE_HAND " $dilper\\\% \\\\\n";}
#     else
#        { print FILE_HAND "\\\\\n";}
     print FILE_HAND "Initial Temperature: \& $temp0 K ";
     print FILE_HAND "\& Equivalence Ratio: \& $equivr \\\\\n";
     print FILE_HAND "\\end\{tabular\}\n";
     print FILE_HAND "\\end\{center\}\n";
     print FILE_HAND "\n";
   }

}

sub decode_plot {
#
# File Name	Title	Mixture	F1	F2	F3	F4	F5	F6
# F7	F8	F9	F10	F11	F12	F13	F14	F15
#
   local($lref) =     $_[0];
   local(*Shortref) = $_[1];
   local(*Shrefid) =  $_[2];
   local(*Dfile) =    $_[3];
   local(*Refid) =    $_[4];
   local(*Temper) =   $_[5];
   local(*Pressur) =  $_[6];
   local(*Equivrat) = $_[7];
   local(*Perdil) =   $_[8];
   local(*Subcat) =   $_[9];
   local(*Fuel) =     $_[10];
   local(*Oxidiz) =   $_[11];
   local(*Diluent) =  $_[12];
   $lref =~ s/"//g;
   @REF      = split(/\t/,$lref);
   $file     = $REF[0];
   $title    = $REF[1];
   $category = $REF[2];
   $mixture  = $REF[3];
   $xlabel   = $REF[4];
   $ylabel   = $REF[5];
   $logaxes  = $REF[6];
   $xrange   = $REF[7];
   $yrange   = $REF[8];
   $details  = $REF[9];
   @data     = @REF[10 .. 24];

   if ($category =~ /cell size/)
      { open(FILE_HAND, ">&PLOT_CELL_SIZE"); }
   elsif ($category =~ /critical tube/)
      { open(FILE_HAND, ">&PLOT_CRIT_TUBE"); }
   elsif ($category =~ /critical energy/)
      { open(FILE_HAND, ">&PLOT_CRIT_ENERGY"); }
   elsif ($category =~ /minimum tube/)
      { open(FILE_HAND, ">&PLOT_MIN_TUBE"); }
   else
      { open(FILE_HAND, ">&PLOT_MISC"); }
 
   print FILE_HAND "\\xname\{$file\}\n";
   print FILE_HAND "\\W\\subsubsection\{$title; $mixture\}\n";
   print FILE_HAND "\\begin\{figure\}[!ht]\n";
   print FILE_HAND "\\texorhtml\{\\epsfig\{file=\\plotdir/$file\.ps,";
   print FILE_HAND "width=6in\}\}\n";
# Removed trans part of gif names.
   print FILE_HAND "\{\\htmlimage\{\\plotdir/$file", "\.gif\}\}\n";
   print FILE_HAND "\\T\\caption\{$title; $mixture\}\n";
   print FILE_HAND "\\end\{figure\}\n";
   print FILE_HAND "\n";
   print FILE_HAND "\\W\\xlink\{Postscript Version\}\{\\plotdir/$file\.ps\}\n";
   print FILE_HAND "\n";
   foreach $datfile (0 .. $#data) {  
     if ($data[$datfile]) {
       if ($data[$datfile] =~ /\,/)
          { ($data[$datfile],$col1,$col2) = split(/\,/,$data[$datfile]); }
       $reference = "";
       foreach $datline (0 .. $#Dfile) {
         if ($Dfile[$datline] eq $data[$datfile]) {
           $reference = $Refid[$datline]; 
           $ltemp =     $Temper[$datline];
           $lpress =    $Pressur[$datline];
           $lequivr =   $Equivrat[$datline];
           $ldilper =   $Perdil[$datline];
           $lsubcat =   $Subcat[$datline];
           $lfuel =     $Fuel[$datline];
           $loxid =     $Oxidiz[$datline];
           $ldiluent =  $Diluent[$datline]; }
       }
       foreach $id (0 .. $#Shrefid) {
         if ($Shrefid[$id] eq $reference) {
           $lshortref = $Shortref[$id];
         }
       }
       print FILE_HAND "\\T $data[$datfile] -";
       print FILE_HAND " Table~\\ref\{$data[$datfile]\}";
       print FILE_HAND "~\\cite[$lshortref]\{$reference\}\n";
       print FILE_HAND "\\W\\xlink\{$data[$datfile]\.txt\}";
       print FILE_HAND "\{\\rawdatadir/$data[$datfile]\.txt\}\n";
       print FILE_HAND "\\W $lshortref \\cite\{$reference\}\n";
       @detail_list = "";
       if ($details =~ /T/)
          { push(@detail_list, "T=$ltemp K");  }
       if ($details =~ /P/)
          { push(@detail_list, "P=$lpress kPa"); }
       if ($details =~ /E/)
          { push(@detail_list, "ER=$lequivr"); }
       if ($details =~ /D/ && $ldiluent)
          { push(@detail_list, "$ldilper\\% $ldiluent"); }
       if ($details =~ /S/)
          { push(@detail_list, "Subcategory=$lsubcat"); }
       if ($details =~ /F/)
          { push(@detail_list, "Fuel=$lfuel"); }
       if ($details =~ /O/)
          { push(@detail_list, "Oxidizer=$loxid"); }
       shift(@detail_list);
       print FILE_HAND join(', ',@detail_list), "\n";
       print FILE_HAND "\n";
     }
   }
   print FILE_HAND "\\T\\clearpage\n";
   print FILE_HAND "\n";
}
