#!/usr/bin/perl
# Computer Laboratory web house-style tool
# Original version by Neil Dodgson, current maintainer: Markus Kuhn
# Please send any comments or suggestions concerning this tool
# to pagemaster@cl.cam.ac.uk

use POSIX qw(strftime);

&usage() unless (@ARGV);

my $headerfile = "/anfs/www/html/UoCCL/template/head.def";
my $footerfile = "/anfs/www/html/UoCCL/template/foot.def";

my $supress_section = 0;
my $bare_html = 0;

# read current time and real uid as fall-back metadata
my @passwd = getpwuid($<);
my $current_login = @passwd[0] || "an unidentified user" ;
my $current_name  = @passwd[6] || $current_login ;
my $current_date  = strftime('on %-e-%b-%Y at %H:%M', localtime(time)) ;
my $current_year  = strftime('%Y', localtime(time)) ;

foreach $ag (@ARGV) {

    if( $ag =~ "^-s" ) {
	$supress_section = 1 ;
	next;
    } elsif( $ag =~ "^-b" ) {
	$bare_html = 1 ;
	next;
    }

    my $date  = $current_date;
    my $year  = $current_year;
    my $login = $current_login;
    my $name  = $current_name;

    my $agtmp    = $ag . ".html~$$";
    my $bare_fn  = $ag . "-b.html";

    my $allincluded = &doIncludes("$ag.tmpl");

    # Read owner uid and lastmod time of (top-level) template file
    my @inode = stat("$ag.tmpl");
    if (@inode) {
	my $uid   = $inode[4];
	my $mtime = $inode[9];
	my @pwd = getpwuid($uid);
	if (@pwd) {
	    # If we know the name of the file owner, use inode data
	    # instead of caller data to fill in name and date
	    $login = $pwd[0];
	    $name  = $pwd[6];
	    $date  = strftime("on %-e-%b-%Y at %H:%M",
			      localtime($mtime));
	    $year  = strftime("%Y", localtime($mtime));
	}
    }

    $allincluded =~ s/^(.*?\n)//;
    $title=$1 ;

    $sectionhead    = "Computer Laboratory" ;
    $copyright      = "University of Cambridge Computer Laboratory" ;
    $breadcrumbline = "" ;
    $commentcontact = "<a href=\"mailto:pagemaster\@cl.cam.ac.uk\">pagemaster\@cl.cam.ac.uk</a>" ;

    $process_headings = 1 ;
    $allincluded =~ s/^(.*?\n)//;
    $tmp=$1 ;
    while( $process_headings ) {
	$process_headings = 0 ;

	if( $tmp =~ /^SECTION&(.*)$/ ) {
	    $sectionhead=$1 ;
	    $process_headings = 1 ;
	}
	
	if( $tmp =~ /^COMMENTS&(.*)$/ ) {
	    $commentcontact=$1 ;
	    $process_headings = 1 ;
	}
	
	if( $tmp =~ /^COPYRIGHT&(.*)$/ ) {
	    $copyright=$1 ;
	    $process_headings = 1 ;
	}
	
	if( $tmp =~ /^HEADERFILE&(.*)$/ ) {
	    $headerfile=$1 ;
	    $process_headings = 1 ;
	}
	
	if( $tmp =~ /^FOOTERFILE&(.*)$/ ) {
	    $footerfile=$1 ;
	    $process_headings = 1 ;
	}
	
	if( $tmp =~ /BREADCRUMB&.*/ ) {
	    @bread=split(/&/,$tmp) ;
	    @bwords=split(/ /, @bread[1]) ;
	    $bname=@bwords[0] ;
	    for( $i=1 ; $i <= $#bwords ; $i++ ){
		$bname = $bname . "&nbsp;" . @bwords[$i] ;
	    }
	    $breadcrumbline = $breadcrumbline . "&nbsp;&gt;&nbsp;<a href=\"" . @bread[2] . "\"class=\"bread\">" . $bname . "</a>" ; 
	    $process_headings = 1 ;
	}
	
	if( $process_headings ) {
	    $allincluded =~ s/^(.*?\n)//;
	    $tmp=$1 ;
	}
    }
    
#### generate the final breadcrumb which is the current file itself
    @path=split(/\//, $ag) ;
    $localfilename=@path[$#path] . ".html" ;
    @bwords=split(/ /, $title) ;
    $bname=@bwords[0] ;
    for( $i=1 ; $i <= $#bwords ; $i++ ){
	$bname = $bname . "&nbsp;" . @bwords[$i] ;
    }
    $breadcrumbline = $breadcrumbline . "&nbsp;&gt;&nbsp;<a href=\"" . $localfilename . "\" class=\"bread\">" . $bname . "</a>" ; 
    
    
#### if suppressing the section header then do so now
    if( $supress_section ) {
	$sectionhead = "" ;
    }
    
    my $html_header;
    my $html_body;
    my $html_footer;

    open(HEADER,"<$headerfile") or die "Unable to open '$headerfile': $!\n" ;
    while(<HEADER>) {
	s/##TITLE##/$title/g ;
	s/##SECTION##/$sectionhead/g ;
	s/##BREADCRUMBS##/$breadcrumbline/g ;
	s/##FILENAME##/$ag.html/g ;
	s/##DATE##/$date/g ;
	s/##OWNERNAME##/$name/g ;
	s/##OWNERUSERID##/$login/g ;
	s/##OWNEREMAIL##/$login\@cl.cam.ac.uk/g ;
	s/##COMMENTCONTACT##/$commentcontact/g ;
	s/##COPYRIGHT##/$copyright/g ;
	s/##YEAR##/$year/g ;
	$html_header .= $_ ;
    }
    close(HEADER) ;
    
    $_ = $tmp . $allincluded ;
    s/##LISTSTART##/<ul>/g ;
    s/##ITEMHEAD##/<li>/g ;
    s/##ITEMBODY##/<br \/>/g ;
    s|##LISTEND##|</ul>|g ;
    s/##TITLE##/$title/g ;
    s/##SECTION##/$sectionhead/g ;
    s/##BREADCRUMBS##/$breadcrumbline/g ;
    s/##FILENAME##/$ag.html/g ;
    s/##DATE##/$date/g ;
    s/##OWNERNAME##/$name/g ;
    s/##OWNERUSERID##/$login/g ;
    s/##OWNEREMAIL##/$login\@cl.cam.ac.uk/g ;
    s/##COMMENTCONTACT##/$commentcontact/g ;
    s/##COPYRIGHT##/$copyright/g ;
    s/##YEAR##/$year/g ;
    $html_body .= $_ ;

    open(FOOTER,"<$footerfile") or die "Unable to open '$footerfile': $!\n" ;
    while(<FOOTER>) {
	s/##TITLE##/$title/g ;
	s/##SECTION##/$sectionhead/g ;
	s/##BREADCRUMBS##/$breadcrumbline/g ;
	s/##FILENAME##/$ag.html/g ;
	s/##DATE##/$date/g ;
	s/##OWNERNAME##/$name/g ;
	s/##OWNERUSERID##/$login/g ;
	s/##OWNEREMAIL##/$login\@cl.cam.ac.uk/g ;
	s/##COMMENTCONTACT##/$commentcontact/g ;
	s/##COPYRIGHT##/$copyright/g ;
	s/##YEAR##/$year/g ;
	$html_footer .= $_ ;
    }
    close(FOOTER);

    if ($bare_html) {
	chomp $title;
	open(HTML, '>', $bare_fn) or die "Unable to open '$bare_fn': $!\n" ;
	print HTML "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	print HTML "<title>$title</title>\n";
	print HTML "<body>\n";
	unless ($html_body =~ /^\s*<h1>/i) {
	    # Repeat TITLE as H1 because the old house-style implemented
	    # by this script automatically places the title near the
	    # top-right corner of the page (below the organization name),
	    # but newer ones don't.
	    print HTML "<h1>$title</h1>\n";
	}
	print HTML $html_body;
	print HTML "</body>\n";
	close(HTML);
    } else {
	open(HTML,">$agtmp") or die "Unable to open '$agtmp': $!\n" ;
	print HTML $html_header, $html_body, $html_footer;
	close(HTML);
	rename $agtmp, "$ag.html"
	    or die "Unable to rename '$agtmp' to '$ag.html': $!\n";
    }
}

sub deprecated
{
    print "WARNING: This script is deprecated and ucampas should be used instead.\n\n";
    print "How to migrate old *.tmpl files:\n\n";
    print "Instead of calling\n\n";
    print "   /anfs/www/html/UoCCL/template/install.pl filename\n\n";
    print "as you did so far, just call once\n\n";
    print "   /anfs/www/tools/bin/ucampas -b filename\n\n";
    print "and you can then\n\n";
    print "   - discard the old filename.tmpl file\n";
    print "   - edit filename-b.html instead\n";
    print "   - call /anfs/www/tools/bin/ucampas filename\n";
    print "     to (re)format the page and generate filename.html\n\n";
    print "For more information:\n\n";
    print "  http://www.cl.cam.ac.uk/local/web/ucampas/\n\n";
}

sub usage
{
    print "Usage: install.pl [-s|-b] <name>\n";
    print "       Installs web page <name>.html based on <name>.tmpl\n";
    print "       -s supresses the generation of any section title\n";
    print "          which says 'Computer Laboratory' in the default\n";
    print "          case or is specified by SECTION& in the .tmpl file\n";
    print "       -b generates <name>-b.html input file for ucampas instead\n";
    print "\n\n";
    deprecated;
    exit 1;
}

sub doIncludes
{
    my $infile = $_[0];
    my $out;

    open(FIN, "<$infile")
	or die "doIncludes unable to read from '$infile': $!\n";
    my @lines=<FIN>;
    close(FIN);
    foreach my $line (@lines) {
	if( $line =~ /^INCLUDE&(.*)$/ ) {
	    $out .= &doIncludes($1);
	} else {
	    $out .= $line;
	}
    }
    return $out;
}
