#!/usr/bin/perl

use strict;

umask 0002;

#-----------------------------------------------------------------------------
my $update_always = 0; #update files even if content unmodified
my $update_scriptnew = 1; # update if the generate script changed

#-----------------------------------------------------------------------------
#my $homedir = `pwd`;
#chomp($homedir);
my $homedir = "/usr/web/robots";
my $webroot = "/usr/web";

my $thisscript = $0;

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

#-----------------------------------------------------------------------------
sub compare_dates {
    my ($file1, $file2) = @_;

    my ($dev1,$ino1,$mode1,$nlink1,$uid1,$gid1,$rdev1,$size1,
        $atime1,$mtime1,$ctime1,$blksize1,$blocks1) = stat($file1);
    
    my ($dev2,$ino2,$mode2,$nlink2,$uid2,$gid2,$rdev2,$size2,
        $atime2,$mtime2,$ctime2,$blksize2,$blocks2) = stat($file2);

    if ($mtime1 > $mtime2)
    {
        return 1;
    }  
    elsif ($mtime1 == $mtime2)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

#-----------------------------------------------------------------------------
sub generate_menu 
{
    my ($currentdir,$menu_aref,$contents_href) = @_;
    my $menucode;
    
    $menucode .= qq {
           <ul id="menu" name="menu">
    };

    for my $key ( @$menu_aref ) {
        
        if( $currentdir eq $$contents_href{$key}[0] ) {
            
            $menucode .= qq {
                <li class="menuitem" id="menuactive" name="menuactive"> &nbsp;$key</li>
            };
            
        } else {
            my $parenturl = "";
            
            if ($$contents_href{$key}[0] =~ /^($webroot(.*))$/)
            {
                $parenturl = $2;
            }
            	
            $menucode .= qq {
                <li class="menuitem">&nbsp;<a href="$parenturl/index.html">$key</a></li>
            };
        }
    }
    $menucode .= qq {
        </ul>
    };

    return $menucode;
}

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

    "Home" => ARRAYREF( "$homedir", "index.html" ),
    "Awards" => ARRAYREF( "$homedir/awards", "index.html" ),
    "Courses" => ARRAYREF( "$homedir/courses", "index.html" ),
    "Facilities" => ARRAYREF( "$homedir/facilities", "index.html" ),
    "Outreach" => ARRAYREF( "$homedir/outreach", "index.html" ),
    "People" => ARRAYREF( "$homedir/people", "index.html" ),
    "Reading-Group" => ARRAYREF( "$homedir/reading-group", "index.html" ),
    "Robots" => ARRAYREF( "$homedir/robots", "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" ),
    "91540" => ARRAYREF( "$homedir/courses/91.549", "index.html" ),
    "ReadingGroupOldAnnounce" => ARRAYREF( "$homedir/reading-group", "old_announce.html" ),
    "labcam" => ARRAYREF( "$homedir/labcam", "index.html" ),
    "motioncam" => ARRAYREF( "$homedir/motioncam", "index.html" ),
    "atrv-jr" => ARRAYREF( "$homedir/atrv-jr", "index.html" ),

    "Research" => ARRAYREF( "$homedir/research", "index.html" ),
    "Gallery" => ARRAYREF( "$homedir/gallery", "index.html" ),
    "RoboTank" => ARRAYREF( "$homedir/research/robot-hardware/robotank", "index.html" ),
    "USARInterface" => ARRAYREF( "$homedir/research/hri/interface", "index.html" ),
    "Wheelesley" => ARRAYREF( "$homedir/wheelesley", "wheelesley.html" )
    

);

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

    "Home",
    "Research",
    "People",
    "Courses",
    "Facilities",
    "Gallery",
    "Reading-Group",
    "Awards",
    "Outreach"

);

#-----------------------------------------------------------------------------
print "\n";
print "dir: $homedir\n\n";
print "running $thisscript ...\n\n";

#-----------------------------------------------------------------------------
for my $contentkey ( sort keys %contentitems ) {

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

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

    $contentinfile =~ s/^$homedir/\./;
    $contentoutfile =~ s/^$homedir/\./;

    if ($contentoutfile =~ /content/)
    {
        print "** skipping ** possible program error : $contentoutfile\n";
    	next;
    }

    # check to make sure the file exists
    if( ! -e "$contentinfile" ) {
        print "** skipping ** does not exist : $contentinfile\n";
    	next;
    }

    # check to see if the script has changed since the output 
    # file has been modified. Update the file if it has been 
    # updated.
    if ((compare_dates($thisscript,$contentoutfile) > 0) &&
        ($update_scriptnew == 1))
    {
    }
    # check to see if the content file has been updated 
    # since last creating the output file. update the output file
    # if the content file has been changed
    elsif ((compare_dates($contentinfile,$contentoutfile) <= 0) &&
        ($update_always == 0))
    {
        print "** skipping ** file unmodified : $contentinfile\n";
        next;
    }

        
    print "generating   $contentinfile\t-->$contentoutfile\n";
	
    open( IN, "$contentinfile" ) or die "$! $contentinfile"; 
    my @stuff = <IN>;
    close IN;

    my $stuff = join( "", @stuff );
    
    #  get the title of the web page
    my $title = "LAIR";
    if ($stuff =~ /<title>(.*?)<\/title>/is)
    {
        $title = $1;
    }
    
    my $metatags = "";
    while ($stuff =~ /<meta(.*?)>/isg)
    {
        my $tmp = $1;
        if ($tmp =~ /(gentag(.*?)=(.*?)["']?keep["']?)/is)
        {
            my $tmp2 = $1;
            $tmp =~ s/$tmp2//;
            $metatags .= "<meta$tmp>\n";
        }
    }

    #  get the body (content) of the web page
    my $body = "<b>No Content</b>\n";
    if ($stuff =~ /<body.*?>(.*?)<\/body>/is)
    {
        $body = $1;
    }
         
#    print "\t\t$contentoutfile\t";

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


if( $contentkey eq "labcam1" ) {

    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="Tue, 01 Jan 1980 1:00:00 GMT">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="refresh" CONTENT="20">
    <title>
    UMass Lowell Robotics Lab - $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">
        UMass Lowell Robotics Lab
        </h1>
        
   };
   

} else {


    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" />
    $metatags
    <title>
    UMass Lowell Robotics Lab                               - $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">
        UMass Lowell Robotics Lab
        </h1>
        
   };


}

    print OUT &generate_menu($contentparentdir,\@menuitems,\%contentitems);

    print OUT qq {

    </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 "\n\nend of script  $0\n\n";

