#!/usr/bin/perl

# Generates the TOF calibration main web page, index.html,
# and the individual store web pages. All files are assumed
# to be in the current directory

# Handle for index.html file
open(INDEX, ">index.html");

printf(INDEX "<html><head>\n");
printf(INDEX "<title> TOF Calibration results </title>");
my $datestring=`date`;
chop($datestring);
printf(INDEX "</head><body><h1>TOF Calibration</h1><br>Generated: $datestring<br>\n");
my $runs=`cat runs.list`;
printf(INDEX "<h2>Runs: [<font color=blue size=2>$runs</font>]\n</h2><br><hr>\n");

foreach my $logf (`ls *.log`)
{
  chop($logf);
  printf(INDEX "Logfiles: <a href=\"$logf\">$logf</a><br>\n");
}

print INDEX "<br><br><h2>Store-by-store results</h2><br><br>\n";

foreach my $fstores (`ls files_*.list`)
{
  chop($fstores);
  my ($store)=( $fstores =~ /_(\d+)./ );
  printf(INDEX "Store: <a href=\"$store.html\">$store</a><br>\n");

  # Handle for individual store html file
  open(ASTORE, ">$store.html");
  print ASTORE "<html><head><title>TOF: store $store </title>\n";
  print ASTORE "</head><h3>Store $store</h3><br><br>\n<body>";
  my $f=$fstores;
  printf(ASTORE "<a href=\"$f\_qmip.eps\"><img src=\"$f\_qmip.gif\"></a><br>\n");
  printf(ASTORE "<a href=\"$f\_speed.eps\"><img src=\"$f\_speed.gif\"></a><br>\n");
  printf(ASTORE "<a href=\"$f\_walk.eps\"><img src=\"$f\_walk.gif\"></a><br>\n");
  printf(ASTORE "<a href=\"$f\_slope.eps\"><img src=\"$f\_slope.gif\"></a><br>\n");
  printf(ASTORE "<a href=\"$f\_res.eps\"><img src=\"$f\_res.gif\"></a><br>\n");
  printf(ASTORE "<a href=\"$f\_sigma.eps\"><img src=\"$f\_sigma.gif\"></a><br>\n");
  print ASTORE "<br><br>\n";
  my $logfile=$f . ".log";
  print ASTORE "Log file: <a href=\"files/$logfile\">$logfile</a><br><br>\n";
  my $reslist=$f . "_*.txt";
  foreach my $sfile (`cd files; ls $reslist`)
  {
    chop($sfile);
    print ASTORE "Calibration file: <a href=\"files/$sfile\">$sfile</a><br>\n";
  }
  my $flist=`cat $fstores`;
  print ASTORE "<hr><br>List of files used:\n<pre>$flist</pre>\n";
  print ASTORE "</body></html>";
  close ASTORE;
}

print INDEX "</body></html>";
close INDEX;

exit;
