#! /usr/bin/perl -w
#
#   Build an html table describing all monitor trends.
#
#   Monitors that are to be included in the table and the necessary 
#   data files for each monitor are in directories $docpath/<monitor>/
#   The html file produced by this script should be placed in $docpath/ 
#   also.
#
#---------------------------------------  Initialize
chomp($cwd=`pwd`);

#---------------------------------------  Parse arguments
if (grep(/--help/, @ARGV) > 0) {
    print "This script generates an html file listing all known monitor\n";
    print "trends and the process and monitor that produce them\n\n";
    print "The following arguments control the script:\n";
    print "--docpath:<doc-path>   Path containing the monitor document directories\n";
    print "                       [\$GDSAPACHE/dmt/Monitors]\n\n";
    print "--help                 Print this message.\n\n";
    print "--procdir:<trend-path> Path containing procmgt process list\n";
    print "                       [\$HOME]\n\n";
    print "--trendir:<trend-path> Path containing DMT trend directories\n";
    print "                       [/export/DMTtrends]\n\n";
    exit;
}

$docpath = (grep(/--docpath:/, @ARGV))[0];
if (defined($docpath)) {
    $docpath =~ s/--docpath://;
} elsif (defined($ENV{GDSAPACHE})) {
    $docpath = "$ENV{GDSAPACHE}/dmt/Monitors";
} else {
    $docpath="../doc/Monitors";
    $docpath="$cwd/$docpath" if (substr($docpath,0,1) =~ "\.");
}

$procdir = (grep(/--procdir:/, @ARGV))[0];
if (defined($procdir)) {
    $procdir =~ s/--procdir://;
} else {
    $procdir="$ENV{HOME}";
}

$trendir = (grep(/--trendir:/, @ARGV))[0];
if (defined($trendir)) {
    $trendir =~ s/--trendir://;
} else {
    $trendir="/export/DMTtrends";
}

#---------------------------------------  Print a header
my $credate = `date`;
chomp $credate;
print "<html>\n";
print "<head>\n";
print "<title>Monitor Trend Channels</title>\n";
print "The following table lists all DMT trends available to the DataViewer\n";
print "as of the time this page was created ($credate). This page may be\n";
print "regenerated using the MonTrend perl script in the current DMT bin\n";
print "directory. The three columns contain the name of the process and \n";
print "monitor that created the trend and the trend channel name.\n";
print "<center>\n";
print "<table border width=\"90%\">\n";
print "<tr>\n";
print "<td><font size=+2>Process</font></td>\n";
print "<td><font size=+2>Monitor</font></td>\n";
print "<td><font size=+2>Channel</font></td>\n";
print "</tr>\n";

opendir(DIRHANDLE, "$trendir");
while ( defined($proc = readdir(DIRHANDLE)) ) {
    next if (! -d "$trendir/$proc" );

    my $fname="$trendir/$proc/channel.cfg";
    next if (! -f "$fname" );
    print "<tr><td width=\"15%\"><font size=+2><a href=../../monitor_reports/$proc>$proc</a></font></td>\n";

    #-----------------------------------  Get the monitor name
    my $mon;
    my $procl = `ls -d $procdir/Process_List_*/$proc`;
    chomp $procl;
    if (defined($procl) && $procl ne "") {
	$mon = readlink("$procl/exe");
	$mon =~ s|.*/||;
    } elsif (-d "$docpath/$proc" ) {
	$mon = $proc;
    } else {
	$mon = "Unknown";
    }

    #-----------------------------------  Link monitor name to document
    if (! -d "$docpath/$mon" ) {
	print "<td width=\"15%\"><font size=+2>$mon</font></td>\n";
    } elsif ( -e "$docpath/$mon/index.html" ) {
	print "<td width=\"15%\"><font size=+2><a href=$mon/index.html>$mon</a></font></td>\n";
    } elsif ( -e "$docpath/$mon/$mon.html" ) {
	print "<td width=\"15%\"><font size=+2><a href=$mon/$mon.html>$mon</a></font></td>\n";
    } elsif ( -e "$docpath/$mon/$mon.pdf" ) {
	print "<td width=\"15%\"><font size=+2><a href=$mon/$mon.pdf>$mon</a></font></td>\n";
    } else {
	print "<td width=\"15%\"><font size=+2><a href=$mon/index.html></font></td>\n";
    }

    #-----------------------------------  Skip past the 
    open(CFGHANDLE, "<$fname");
    while (defined($chan=<CFGHANDLE>)) {
	chomp $chan;
	last if ($chan =~ "\\[signals\\]");
    }

    my $first = 1;
    while (defined($chan=<CFGHANDLE>)) {
	print (" <tr><td> </td><td> </td>") if (! $first);
	$first = 0;
	my @list = split(' ', $chan);
	print "<td width=\"60%\">$list[0]</td></tr>\n";
    } 
    close(CFGHANDLE);
    print "</tr>\n" if ($first);
}
print "</table>\n";
print "</center>\n";
print "</head>\n";
print "</html>\n";

