#!/usr/athena/bin/perl -w
# $Id: mapstuff.pl,v 1.10 2009/01/23 22:24:24 jdreed Exp $
# USAGE mapstuff.pl <cluster-name>*
# if you give it cluster names on the command line it will
# htmlize those ones otherwise it will use the default list

# w20-575 was left out of the above list, it must be edited by hand
# because of test-sgi
# (no longer true as of 2005)


chdir("/afs/sipb/admin/text/cluster-maps");

@files_to_do = (@ARGV) ? @ARGV :
    ("barker-5", "barker-6", "e51-075", 
     "m2-225", "m37-312", "m38-370", "w20-575",
     "m37-332", "m4-167", "rotch",
     "hayden", "m66-080", "m56-129", "m12-182", "m2-032");

grep( &do_file($_, "www/$_.html"), @files_to_do);

sub do_file {
    local($infile, $outfile) = @_;

    if (-e $infile) {
	system "echo Htmlizing $infile";
	
	# read file into array
	$updated = localtime((stat($infile))[9]);
	open (INFH, $infile);
	@stuff = <INFH>;
	# ignore the first line if it's an RCS identification line
	if ( $stuff[0] =~ m/^\$.*\$$/) {
	    shift @stuff
	    }
	close(INFH);
	
	# Escape special HTML stuff
	grep( s/&/&amp;/g, @stuff);
	grep( s/</&lt;/g, @stuff);
	grep( s/>/&gt;/g, @stuff);
	
	# Make the links. Magic.
	# I picked "$" because it's convenient.
	grep (s/ / \$ /g, @stuff);
	grep( s%([\|\+\b\s])(\d{1,3})(\*?[\|\+\b\s])%$1<a href="http://www.mit.edu:8001/machine?$infile-$2">$2</a>$3%g, @stuff);
	grep( s%([\|\+\b\s])Q(((\d{1,3})-){0,1}(\d{1,2}))([\|\+\b\s])%$1<a href="http://www.mit.edu:8001/machine?quickstation-$2">Q$2</a>$6%g, @stuff);
#	grep (s/(machine?)Q(\d+\"\>)/$1quickstation-$2/, @stuff);
	grep (s/ \$ / /g, @stuff);
	
	open(OUTFH, ">$outfile");
	print OUTFH <<EOF;
<head><title>Map of $infile cluster</title>
<link rev=made href=mailto:cluster-maps\@mit.edu>
</head><body>
<H1>Map of $infile cluster</H1>
<PRE>
 @stuff
</PRE>
Can be found at http://web.mit.edu/cluster-maps/www
<h5>Maintained by <a href=mailto:cluster-maps\@mit.edu> 
cluster-maps\@mit.edu</a></h5>
Last updated $updated
</body>
EOF
        close(OUTFH);
    }
    else {
	system "echo $infile does not exist"
	}
}
