#!/afs/athena/contrib/perl/p

$rcs_binary_dir = "/afs/athena.mit.edu/contrib/watchmaker/bin";
$ln_bin         = '/bin/ln';
$ls_bin         = '/bin/ls';
$mv_bin         = '/bin/mv';

# if (@ARGV) { &explain_usage(); }

($filename, $url, $username, $name, $temp_file, $symlink_old, $symlink_new) = @ARGV;

# Take this line out if you switch back to rshing for server-add-home!!!
# 2/26/95 -- nocturne
$name =~ s/^\'(.*)\'$/$1/;

if (($symlink_old ne "") && ($symlink_new ne "")) {
    unlink($symlink_new);
    symlink($symlink_old, $symlink_new);

    print "WWW :  Symlink to $symlink_old made.\n";
}

&add_to_homepage_file($filename, $url, $username, $name, $temp_file);

print "WWW :  Generating alphabetical homepage list\n";
system "/afs/sipb.mit.edu/project/www/bin/home-alpha.pl";

sub add_to_homepage_file {
    local($filename, $url, $username, $name, $temp_file) = @_;

    &rcs_check_out_file($filename);

    &modify_homepage_list($filename, $url, $username, $name);

    system $mv_bin, $temp_file, $filename;

# Uncomment this if this script is to run on an NFS filesystem!
#    chown ($>, (getgrnam("www"))[2], $filename);
    chmod (0555, $filename);
    
    &rcs_check_in_file($filename,
		       "added homepage for $username w/ magic");
}

sub rcs_check_out_file {
    local($filename) = @_;

    $rlog_output = `$rcs_binary_dir/rlog -h $filename`;

    if ($rlog_output !~ /locks: \S*\s*$ENV{'USER'}/) {
	`$rcs_binary_dir/co -l -q $filename`;
	print "WWW :  Checking out $filename via RCS....\n";
    }
}

sub rcs_check_in_file {
    local($filename, $reason) = @_;

    open(CI, "|$rcs_binary_dir/ci -q -u $filename");
    print CI ($reason . "\n.\n");
    close CI;

    print "WWW :  Checking in $filename via RCS....\n";
}


sub modify_homepage_list {
    local($filename, $url, $username, $name) = @_;
    
    local($head, $foot, @body) = &suck_in_homepage_file($filename);

    $index = 0;
    
    for ($index=0; $index < $#body; $index++) {

	# Pick the username out of the "name" field from the homepage entry
	$body[$index] =~ /name=\"([^\"]*)/;

	# Compare the username with the username we're adding.
	if (! ($username gt $1)) { last ;}
    }

    open(OFILE, ">" . $temp_file);

    print OFILE ($head, @body[0..($index - 1)]);
    
    print OFILE "<a name=\"$username\" href=\"$url\">$username</a>, $name\n\n<li>";
    
    if ($body[$index] =~ /name=\"([^\"]*)/) {
	if ($1 eq $username) {
	    # Increment the index, "forgetting" the old version of this
	    # person's homepage entry.
	    $index++;
	}
    }
	
    print OFILE (@body[$index..$#body], $foot);

    close OFILE
}

sub suck_in_homepage_file {
    local($filename) = @_;
    if (open(HFILE, $filename)) {
	
	# Find the start of the list. If you change the list-type, or
	# add a list before the list of homepages, you need to modify this
	# line/block of code to find the first thing before the list of
	# homepages.
	until (($bar = <HFILE>) =~ /<ol>/) {$header .= $bar; }

	$header .= "<ol>\n";

	# set the line separator to <li>. It's easiest this way. :-)
	$/="<li>";
	
	# suck the list elements into an array.
	@body=<HFILE>;
	close(HFILE);
	
	# The first two lines of code here *only* modify the last element
	# of the array.
	# They trim off the trailing HTML code from the list o' homepages.
	$body[$#body] =~ /<\/ol>/;
	$body[$#body] = $` ;
	$footer = "</ol>" . $' ;
	
	# Nuke the <li>'s from the list elements.
	# grep( s/<li>//, @body);
	
	return($header, $footer, @body);
    }
    else {
	print "WWW :  Couldn't open $filename.\n";
	print "WWW :  No changes made. Giving up.\n";
	exit;
    }
}

sub explain_usage {
    print STDERR <<EOU;
WWW :  Usage: $0
WWW :  
WWW :  This script should only be run remotely, with the *right* arguments.
WWW :  You loser. Don't run this incorrectly.
WWW :
EOU
    exit;
}
