#!/usr/bin/perl5
if($#ARGV<0) {
    &usage();
} else {

    $supress_section = 0 ;

    $login = getlogin || (getpwuid($<))[0] || "an unidentified user" ;

    $name =`finger $login | sed -n "s/Login.*:.*: //p"` ;
    if( $name eq "" ) { $name = $login ; }

    $date =  `date +"on %-e-%b-%Y at %H:%M"` ;

    foreach $ag (@ARGV) {

	if( $ag =~ "-s" ) {
	    $supress_section = 1 ;
	}
	else {

	    $agtmp = "/tmp/" . $login . "-tmpl2html" ;

	    open(TMPL,"<$ag.tmpl") or die "Unable to open $ag.tmpl\n" ;
	    open(HTML,">$agtmp") or die "Unable to open $agtmp\n" ;

	    $title=<TMPL> ;

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

	    $process_headings = 1 ;
	    $tmp=<TMPL> ;
	    while( $process_headings ) {
		$process_headings = 0 ;

		if( $tmp =~ /^SECTION&(.*)$/ ) {
		    $sectionhead=$1 ;
		    $process_headings = 1 ;
		}

		if( $tmp =~ /^COMMENTS&(.*)$/ ) {
		    $commentcontact=$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 ) {
		    $tmp=<TMPL> ;
		}
	    }

#### 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 = "" ;
	    }


	    open(TOP,"</anfs/www/html/UoCCL/template/top.def") or die "Unable to open top.def\n" ;
	    while(<TOP>) {
		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 ;
		print HTML $_ ;
	    }
	    close(TOP) ;

	    $_ = $tmp ;
	    while(defined($_)) {
		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 ;
		print HTML $_ ;
		$_ = <TMPL>
		}
	    open(BOTTOM,"</anfs/www/html/UoCCL/template/bottom.def") or die "Unable to open bottom.def\n" ;
	    while(<BOTTOM>) {
		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 ;
		print HTML $_ ;
	    }
	    close(BOTTOM) ;
	    close(HTML);
	    close(TMPL);

	    if( system "mv $agtmp $ag.html" ) {
		die "Unable to rename $agtmp to $ag.html\n" ; ;
	    }
	}
    }
}

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