#!/usr/local/bin/perl
# Be sure to set proper path to perl above
# 
# classgen.pl - usage: classgen.pl DIRNAME 
# Version 14Jan97 S.T.Myers (UPenn)
#
# Script to automatically generate an HTML index to a CLASS directory
# containing .gif files.  Works on subdirectory of current working
# directory.  For example, 
#      classgen.pl CLASS1a
# will index subdirectory CLASS1a putting
# in file CLASS1a/CLASS1a.html 
#
# Need library module
use Cwd;
# Get current working directory
$dir = cwd() || die "cannot get cwd\n";
print "Found current directory to be $dir \n";
# These are some directory specific variables from command line
if ( $#ARGV == 0 ) {
  $target = $ARGV[0];
  $topfile = "$ARGV[0].html";
} else {
  die "usage: classgen.pl DIRNAME \n";
}
# Construct target directory name
$classdir = $dir . "/" . $target;
print "Target directory is $classdir\n";
chdir $classdir || die "cannot chdir to $classdir\n";
system("pwd");
# Open target directory and make sorted file list
opendir(DOT,".") || die "cannot opendir . ";
@a = sort(readdir(DOT));
print "Found ","".@a," total files\n";
# Now make stack of gif filenames
foreach $name (@a) {
   if ($name =~ /\.gif$/) {
      push(@b,$name);
   }
}
print "Found ","".@b," gif files\n";
closedir(DOT);
#
# Make the associative array for the sources, splitting the name
# at the . and putting the number of fields in the array
#
foreach $name (@b) {
   @fields = split(/\./,$name);
   $src = $fields[0]; # this is the source name (eg. 16156554)
   $cnt = $fields[1]; # this is the field number
#   print "Processing $src field $cnt\n";
   if ($cnt eq "0") {
      $srclist{$src} = 0; # A blank field
   } else {
      $srclist{$src}++ ; # increment field count
   }
   $counter{$cnt}++; # increment counter array
}
print "Found ","".keys(%srclist)," individual sources\n";

# Sort sourcenames
@sortedkeys = sort keys(%srclist);

#
# Now we want to make the top-level html file
if ( -e $topfile ) {
   die " Error: $topfile already exists ";
} else {
   open(TOPF,">$topfile") || die "Error: cannot create $topfile ";
}
print "Creating HTML file $topfile\n";
# Write HTML header
print TOPF "<HTML>\n";
print TOPF "<HEAD>\n";
print TOPF "<TITLE> $target HTML Database </TITLE>\n";
print TOPF "</HEAD>\n\n";

print TOPF "<BODY bgcolor=FFFFFF text=000000>\n";
print TOPF "<P><center><H1>$target Survey Database</H1></center></P>\n";
print TOPF "<HR>\n\n";
print TOPF "<P>There are ","".@b," images of ","".keys(%srclist)," sources in the database</P>\n";
# Construct table header
print "Creating HTML table\n";
print TOPF "<P><center>\n";
print TOPF "<table>\n";
print TOPF "<TR align=left><TH>Source</TH><TH>Blank Field</TH><TH>Fields</TH></TR>\n";
print TOPF "<TR><TD colspan=3><HR noshade size=1></TD></TR>\n";
# Construct table entries
foreach (@sortedkeys) {
  print TOPF "<TR><TD> $_ </TD>";
  if ( $srclist{$_} == 0 ) { 
    $gif = $_ . ".0.gif" ;
    print TOPF "<TD> <A HREF=$gif TARGET=_blank>$gif</A> </TD><TD> ";
  } else {
    print TOPF "<TD> </TD><TD> ";
    for ($i = 1; $i <= $srclist{$_}; $i++ ) {
      $gif = $_ . "." . $i . ".gif" ;
      print TOPF "<A HREF=$gif TARGET=_blank>$gif</A> ";
    }
  }
  print TOPF "</TD></TR>\n";
}
# Construct table footer
print TOPF "</table></center></P>\n";
# Write descriptive info
print TOPF "<HR>\n";
print TOPF "<P>This file automatically generated on ".`date`;
print TOPF "</P>\n";
# Write HTML footer
print TOPF "</BODY>\n";
print TOPF "</HTML>\n";
# close files - clean up
close(TOPF);
# report
print "Created HTML file $topfile\n";



