#!/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) ;
    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") ;

    foreach $ag (@ARGV) {
	open(TMPL,"<$ag.tmpl") or die "Unable to open $ag.tmpl\n" ;
	open(HTML,">$ag.html") or die "Unable to open $ag.html\n" ;
	$title=<TMPL> ;
	open(TOP,"</anfs/www/html/UoCCL/template/top.def") or die "Unable to open top.def\n" ;
	while(<TOP>) {
	    s/##TITLE##/$title/g ;
	    print HTML $_ ;
	}
	close(TOP) ;
	while(<TMPL>) {
	    s/##LISTSTART##/<UL>/g ;
	    s/##ITEMHEAD##/<LI>/g ;
	    s/##ITEMBODY##/<BR>/g ;
	    s|##LISTEND##|</UL>|g ;
	    print HTML $_ ;
	}
	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 ;
	    print HTML $_ ;
	}
	close(BOTTOM) ;
	close(HTML);
	close(TMPL);
    }
}

sub usage
{
    print "Usage: install.pl <name>\n";
    print "       Installs web page <name>.html based on <name>.tmpl\n";
    exit;
}
