#!/usr/bin/perl5
if($#ARGV<0) {
    &usage();
} else {
#    $tmpfile = "/tmp/install.pl.tmp" ;

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

#    system("/anfs/www/html/UoCCL/template/getrealname $login > $tmpfile") ;
#    open(TMP,"<$tmpfile") or die "Unable to open $tmpfile\n" ;
#    $name = <TMP> ;
#    close(TMP) ;

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

#    system("date > $tmpfile") ;
#    open(TMP,"<$tmpfile") or die "Unable to open $tmpfile\n" ;
#    $date = <TMP> ;
#    close(TMP) ;
#    system("rm $tmpfile") ;

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

    foreach $ag (@ARGV) {
#	@breadcrumbs=split(/\//,$ag);
#       $href="http://www.cl.cam.ac.uk/~nad/teststyle/UoCCL" ;
#       $breadcrumbline="" ;
#       for( $i=0 ; $i <= $#breadcrumbs ; $i++ ){
#         $href = $href . "/" . @breadcrumbs[$i] ;
#         if( $i == $#breadcrumbs ) { $href = $href . ".html" ; }
#           $breadcrumbline = $breadcrumbline . "&nbsp;&gt;&nbsp;<a href=\"" . $href . "\"class=\"bread\">" . @breadcrumbs[$i] . "</a>" ; 
#       } 

	$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> ;
	$breadcrumbline = "" ;
        $commentcontact="<a href=\"mailto:pagemaster\@cl.cam.ac.uk\">pagemaster\@cl.cam.ac.uk</a>" ;
        $tmp=<TMPL> ;
        if( $tmp =~ /^COMMENTS&(.*)$/ ) {
            $commentcontact=$1 ;
            $tmp=<TMPL> ;
        }

	while( $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>" ; 
	    
	    $tmp=<TMPL> ;
	}

#######################################################
#### this is new stuff to generate the final breadcrumb
#### which is the current file itself
#### NAD - 29/8/02
	@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>" ; 
#######################################################


	open(TOP,"</anfs/www/html/UoCCL/template/top.def") or die "Unable to open top.def\n" ;
	while(<TOP>) {
	    s/##TITLE##/$title/g ;
	    s/##BREADCRUMBS##/$breadcrumbline/g ;
	    print HTML $_ ;
	}
	close(TOP) ;

	$_ = $tmp ;
	while(defined($_)) {
	    s/##LISTSTART##/<ul>/g ;
	    s/##ITEMHEAD##/<li>/g ;
	    s/##ITEMBODY##/<br \/>/g ;
	    s|##LISTEND##|</ul>|g ;
	    print HTML $_ ;
	    $_ = <TMPL>
	}
	open(BOTTOM,"</anfs/www/html/UoCCL/template/bottom.def") or die "Unable to open bottom.def\n" ;
	while(<BOTTOM>) {
	    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 <name>\n";
    print "       Installs web page <name>.html based on <name>.tmpl\n";
    exit;
}
