#
# MakeModulePage.pl
#
# Syntax: perl MakeModulePage.pl <DUTSerialNumber> 
#
# DUTSerialNumber is as you specified in sctdaq config
#
# If installing this script afresh on a new system, you also need the perl
# libraries SctDAQWebLib.pl and LocalVariables.pl; You should (at least) look
# at LocalVariables.pl to check it is OK for your local system
#
# This script should run happily on Windows and Linux
#
# Send feedback to Dave.Charlton@cern.ch
#
# Version 0.2 DGC  Write separate hybrid and module index pages
# Version 0.1 DGC  First version

require "SctDAQWebLib.pl" || die "Cannot load SctDAQWebLib.pl" ;
&InitialiseVariables ;

if ( $#ARGV!=0 ) {
    print "syntax: MakeModulePage.pl <s/n>\n" ;
    die "bad arguments" ;
}
$DUT=$ARGV[0] ;

$Directory="$archive/$DUT" ;
@IndexPage=("$Directory/index.html","$Directory/module.html") ;
@DUTTypeName=("Hybrid","Module") ;

$NumSequences=0 ;
foreach $s (@KnownSequences) {
    $Tests{$s}="" ;
    $NumSequences++ ;
}
#print "$NumSequences\n" ;

opendir(DUTDIR,$Directory) ;
@files=grep{!/^\./} readdir(DUTDIR) ;
@anytest=(0,0) ;
foreach $f (@files) {
    next if !-d "$Directory/$f" ;
    next if !-f "$Directory/$f/index.html" ;
    foreach $s (@KnownSequences) {
	if ($f=~/^$s/) {
#	    print "Found $f as a $s\n" ;
	    $Tests{$s}.=" $f" ;
	    for ($i=0;$i<2;$i++) {
		$duttype=$DUTTypeName[$i] ;
		next if !($s=~/^$duttype/) ;
		$anytest[$i]++ ;
	    }
	}
    }
}
closedir(DUTDIR) ;

for ($i=0;$i<2;$i++) {

    next if !$anytest[$i] ;

    open(HTML,">$IndexPage[$i]") ;

    print HTML "<html>\n<head>\n" ;
    print HTML "<title>Electrical Tests of $DUTTypeName[$i] $DUT</title>\n" ;
    print HTML "</head>\n<body bgcolor=white>\n" ;
    print HTML "<h1>Electrical Tests of $DUTTypeName[$i] $DUT</h1>\n" ;

    foreach $s (@KnownSequences) {
	next if $Tests{$s} eq "" ;
#	print $Tests{$s} , $DUTTypeName[$i] ;
	$duttype=$DUTTypeName[$i] ;
	next if !($s=~/^$duttype/) ;
	@Subtests=split(' ',$Tests{$s}) ;
	print HTML "<ul>\n" ;
	foreach $t (@Subtests) {
	    print HTML "<li><a href=$t/index.html>$t</a><br>\n" ;
	}
	print HTML "</ul>\n" ;
    }

    print HTML "</body>\n</html>\n" ;

    close(HTML) ;
}

# print "\n$0 execution complete\n" ;
exit 0 ;

