#!/usr/bin/perl -w
#-------------------------------------------------------------------------------
#Purpose     : generate information files in html format
#Author      : Robert Greimel (rgreimel@mail.com)
#Last Change : 20-Feb-2000, 13-may-2005 (yg)
#-------------------------------------------------------------------------------
use Scripts;

##
# make sure directory ends with a "/"
##
$dir = $ARGV[0];
if ($dir !~ m|/$|) {$dir = $dir."/"};

##########
#read generation options
##########
%filenames=();
print "$0: reading filename configuration ... ";
&Scripts::read_filenames(\%filenames,"config.names");
print "done\n";

%options=();
print "$0: reading default options ... ";
( &Scripts::read_options(\%options,$filenames{"default_options"}) ) ? print "successful\n" : print "failed\n" ;

print "$0: reading directory specific options ... ";
( &Scripts::read_options(\%options,$dir.$filenames{"custom_options"}) ) ? print "successful\n" : print "failed\n" ;

if ($options{"directory"} eq "yes")
  {
  print "directory option is on\n";
  $dir_inout=$filenames{"dir_inout"}."/";
  }
else
  {
  print "directory option is off\n";
  $dir_inout="";
  }

# setup variables
@files = ();
%ut = ();
%object = ();
%type = ();
%starname = ();
%in = ();
%rms = ();
%saturate = ();
%color = ("arc" => "#ff00ff",
	  "bias" => "#00ff00",
	  "flat" => "#00ffff",
	  "object" => "#ffff00",
	  "unknown" => "#ff0000");
%color2= ("arc" => "#ffa0ff",
	  "bias" => "#00d000",
	  "flat" => "#00a0ff",
	  "object" => "#ffffa0",
	  "unknown" => "#ff0000");

#####
# read collected header information
#####
if ( -e $dir.$filenames{"header_info"} )
  {
  open HEADER,$dir.$filenames{"header_info"};
  while (<HEADER>)
    {
    if ( /^END$/ )
      {
      push @files,$file;
      $ut{$file}=$ut;
      $object{$file}=$object;
      $type{$file}=$type;
      next;
      }
    if ( ! /^.*=/ )
      {
      $file=(split m"/",$_)[-1];
      chomp $file;
      next;
      }
    if ( /^DATE-OBS/ ) {$ut = (split "=",$_)[1];
                        $ut = (split "/",$ut)[0];
                        $ut = (split "T",$ut)[1];
                        $ut = "'" . $ut; }
#                        $ut=(split)[2];}
    if ( /^OBJECT  / ) {$object=(split /'/)[1];}
#'
    if ( /^OBSTYPE / ) {$type=(split /'/)[1];
                        $type=(split " ",$type)[-1];}
#'
    }
  close HEADER;
  }

#####
# read type file
#####
if ( -e $dir.$filenames{"type_file"} )
  {
  open TYPE,$dir.$filenames{"type_file"};
  while (<TYPE>)
    {
    @frameinfo=split ' ';
    $file=(split m"/",$frameinfo[0])[-1];
    $type{$file}=$frameinfo[2];
    $starname{$file}=$frameinfo[-1];
    }
  close TYPE;
  }

#####
# read statistics file
#####
if ( -e $dir.$filenames{"stat_file"} )
  {
  open STAT,$dir.$filenames{"stat_file"};
  while (<STAT>)
    {
    if ( m/^#/ ) {next;}
    @frameinfo=split ' ';
    $file=(split m"/",$frameinfo[0])[-1];
    $rms{$file}=$frameinfo[2];
    if ( $#frameinfo == 8 ) {$saturate{$file}=$frameinfo[8];}
    else                    {$saturate{$file}="";}
    }
  close STAT;
  }

#####
# read .in files
#####
opendir DIR,$dir.$dir_inout;
foreach $f ( grep /\.in/, readdir DIR )
  {
  open IN,$dir.$dir_inout.$f;
  while (<IN>)
    {
    chomp;
    $file=(split m"/")[-1];
    $f =~ s/\.in//;
    $in{$file} = $f;
    $in{$file} = $f;
    }
  close IN;
  }
closedir DIR;

# write HTML header
open HTML,">".$dir."info.html";
print HTML "<html>\n";
print HTML "<head>\n";
print HTML "<title>Directory Information</title>\n";
print HTML "</head>\n";
print HTML "<body>\n";

# write HTML table
print HTML "<table cellspacing=0 border=0>\n";
print HTML "<tr><th>File</th><th>OBJECT</th><th>Type</th><th>UT</th>\n";
print HTML "    <th>.in</th><th>RMS</th><th>Saturated</th></tr>\n";
for ($i=0; $i<=$#files; $i++)
  {
  $file = $files[$i];
  if ( exists $in{$files[$i-1]} && exists $in{$file} )
    {
    if ( $i && $type{$files[$i-1]} eq $type{$file} && 
               $in{$files[$i-1]} ne $in{$file} )
      { $c = 1-$c }
    }
  if ( !$i || $type{$files[$i-1]} ne $type{$file} )
    { $c = 0 }

  if ( exists $color{$type{$file}} )
    {
    $bgc = "bgcolor=$color{$type{$file}}";
    if ( $c ) { $bgc2 = "bgcolor=$color2{$type{$file}}"; }
    else      { $bgc2 = "bgcolor=$color{$type{$file}}"; }
    }
  else
    {
    $bgc = "";
    $bgc2 = "";
    }
  print HTML "<tr><td $bgc>&nbsp; $file</td>";
  print HTML "<td $bgc>&nbsp; $object{$file} ($starname{$file})</td>";
  print HTML "<td $bgc>&nbsp; $type{$file}</td>";
  print HTML "<td $bgc>&nbsp; $ut{$file}</td>\n";

  if ( exists $in{$file} )
    {print HTML "<td $bgc2>&nbsp; $in{$file}</td>";}
  else
    {print HTML "<td>&nbsp;</td>";}

  if ( exists $type{$file} && exists $rms{$file} )
    {
    $bgce = $bgc2;
    if ( $type{$file} eq "bias" && $rms{$file} > 10.)
      {$bgce = "bgcolor=#ff0000";}
    if ( $type{$file} ne "bias" && $rms{$file} < 10.)
      {$bgce = "bgcolor=#ff0000";}
    print HTML "<td $bgce align=right>&nbsp; $rms{$file}</td>";
    }
  else
    {print HTML "<td>&nbsp;</td>";}

  if ( exists $saturate{$file} )
    {
    $bgce = "";
    if ( $saturate{$file} ne "") {$bgce = "bgcolor=#ff0000";}
    print HTML "<td $bgce>&nbsp; $saturate{$file}</td>";
    }
  else
    {print HTML "<td>&nbsp;</td>";}

  print HTML "</tr>\n";
  }
print HTML "</table>\n";

#close HTML output
print HTML "</body>\n";
print HTML "</html>\n";
close HTML;
