#!/usr/local/bin/perl

umask 0002;

$homedir = "/usr/web/robots";

sub ARRAYREF {
    my @array = @_;
    return \@array;
}

#  need to do a hash so we know which directory we're in
#  for the menu creation stuff
%contentitems = (

    "Home" => ARRAYREF( "$homedir", "index.html" ),
    "Awards" => ARRAYREF( "$homedir/awards", "index.html" ),
    "Courses" => ARRAYREF( "$homedir/courses", "index.html" ),
    "Facilities" => ARRAYREF( "$homedir/facilities", "index.html" ),
    "Movies" => ARRAYREF( "$homedir/movies", "index.html" ),
    "Outreach" => ARRAYREF( "$homedir/outreach", "index.html" ),
    "People" => ARRAYREF( "$homedir/people", "index.html" ),
    "Projects" => ARRAYREF( "$homedir/projects", "index.html" ),
    "Reading Group" => ARRAYREF( "$homedir/reading-group", "index.html" ),
    "Trinity2002" => ARRAYREF( "$homedir/firefight", "index.html" ),
    "91450" => ARRAYREF( "$homedir/courses/91.450", "index.html" ),
    "91450Fall2001Botball" => ARRAYREF( "$homedir/courses/91.450/Fall2001", "Botball.html" ),
    "91450Fall2001Finals" => ARRAYREF( "$homedir/courses/91.450/Fall2001", "Finals.html" ),
    "91450Fall2001Labs" => ARRAYREF( "$homedir/courses/91.450/Fall2001", "Labs.html" ),
    "91450Fall2002Botball" => ARRAYREF( "$homedir/courses/91.450/Fall2002", "Botball.html" ),
    "91450Fall2002Finals" => ARRAYREF( "$homedir/courses/91.450/Fall2002", "Finals.html" ),
    "91450Fall2002Labs" => ARRAYREF( "$homedir/courses/91.450/Fall2002", "Labs.html" ),
    "91451" => ARRAYREF( "$homedir/courses/91.451", "index.html" ),
    "91451Spring2003Finals" => ARRAYREF( "$homedir/courses/91.451/Spring2003", "Finals.html" ),
    "91451Spring2003Labs" => ARRAYREF( "$homedir/courses/91.451/Spring2003", "Labs.html" )

);

#  need this to enforce the order of the menu buttons
@menuitems = (

    "Home",
    "Awards",
    "Courses",
    "Facilities",
    "Movies",
    "Outreach",
    "People",
    "Projects",
    "Reading Group"

);

print "\n\nrunning $0 ...\n\n";

for $key ( sort keys %contentitems ) {

    #if( $key ne "Reading Group" ) { next; }

    $contentparentdir = $contentitems{$key}[0];
    $contentdir = "$contentitems{$key}[0]/content";
    $contentinfile = "$contentdir/$contentitems{$key}[1]"; 
    $contentoutfile = "$contentparentdir/$contentitems{$key}[1]"; 

    if( ! -e "$contentinfile" ) {
        print "$contentinfile does not exist -- skipping ...\n\n";
	next;
    }

    print "reading $contentinfile ...\n\n";
	
    open( IN, "$contentinfile" ) or die "$! $contentinfile"; 
    @stuff = <IN>;
    close IN;

    $stuff = join( "", @stuff );
    
    #  get the title of the web page
    $stuff =~ /<title>(.*?)<\/title>/is;
    $title = $1;
    
    #  get the body (content) of the web page
    $stuff =~ /<body.*?>(.*?)<\/body>/is;
    $body = $1;
         
    print "creating $contentoutfile ... ";

    open( OUT, ">$contentoutfile" ) or die $!;


    print OUT qq {

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Expires" content="0" />
    <title>
    UML Laboratory for Artificial Intelligence and Robotics - $title
    </title>  
    <link rel="stylesheet" type="text/css" href="/robots/css/stylesblue.css" />
  </head>
  <body bgcolor="#ffffff" link="#0000ff" vlink="#0000ff" alink="#0000ff">

    <div id="header" name="header">
        <h1 id="pagetitle" name="pagetitle">
        UML Laboratory for Artificial Intelligence and Robotics
        </h1>
        
        <ul id="menu" name="menu">

   };
   
    for $key ( @menuitems ) {
        
        if( $contentparentdir eq $contentitems{$key}[0] ) {
            
            print OUT qq {
            <li class="menuitem" id="menuactive" name="menuactive"> &nbsp;$key</li>
            };
            
        } else {
            
            $contentitems{$key}[0] =~ /^\/usr\/web(.*)$/;
            $parenturl = $1;
		
            print OUT qq {
            <li class="menuitem">&nbsp;<a href="$parenturl/index.html">$key</a></li>
            };
        }
        
    }

    print OUT qq {
        
        </ul>
    </div>
    
    <div id="content" name="content">
        $body
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <script language = "Javascript">
        <!--
            document.write( "Last updated&nbsp;&nbsp;" + document.lastModified );
        //-->
        </script>
        <div class="footer">
            <div class="sig" id="sig" name="sig">
                Copyright &copy; 2003 UMass Lowell - Email <a href="mailto:holly\@cs.uml.edu">Holly Yanco</a>
            </div>
        </div>
    </div>
    <br />
    <br />
  </body>
</html>


    };

    close OUT;

    print "done\n\n";

}

print "\n\nend of script  $0\n\n";

