#!/usr/athena/bin/perl

unshift (@INC, '/afs/sipb.mit.edu/project/www/src/db');
require 'dbparse.pl';
require '/afs/sipb.mit.edu/project/www/bin/homelib.pl';

$web_root_dir = "/afs/sipb.mit.edu/project/www/root";

## Never use add-home or edithome on a Sparc5 because of AFS cache corruption:
#if (`machtype -c` =~/SPARC\/5/)
#{
#    print "DO NOT USE THIS PROGRAM ON A SPARC5!\n";
#    print "(AFS Cache Corruption) Thank you :-)\n";
#    exit 0;
#}


###############################################################################

#$rcs_binary_dir = "/mit/gnu/gbin";

#if (! ( -d '/mit/gnu' )) {
#    print "GNU locker was not attached. Attaching it.\n";
#    system('/bin/athena/attach -h -n -q gnu');
#}

#############################################################################

$master_db_file = $DB'default_home;

if (@ARGV) {
    for (@ARGV) {
	&edit_home($_);
    }
} else {
    $input = "";
    while($input eq "") {
        print "What username/mail-address would you like to edit?\n>>";
	$input = (<STDIN>);
	chop($input);
    }
    &edit_home($input);
}

sub edit_home {
    local($username) = @_;
    local($url, $name, $sort_index, $date, $done, $input, $data, $oldname,
	  $symlink);

    $oldname = $username;
    
    &rcs_check_out_file($master_db_file);

    if (&DB'ParseHome($master_db_file) == 0) {
        print "Cannot open $master_db_file. Aborting.\n";
        exit(0);
    }

    ($url, $name, $sort_index, $symlink, $date) = &DB'FetchUser($username); #'
    $oldurl = $url;
    $oldlink = $symlink;
    # We're going to throw away the date. :-)

    if ($url eq "0") {
	&rcs_check_in_file($master_db_file, "no change");
	# this almost certainly means we gave them a nonexistent username
	print "There is no entry in the database for \"$username\".\n";
	print "Would you like to add this user to the database?\n>>";
	if (&accept()) {
	    &DB'ClearHome();
	    &input_home($username);
	}
        return;
    }

    $done = 0;
    EDITLOOP: while(! $done) {

	print "----------------------------------------------\n";
	print "(1)   Username : $username\n";
	print "(2)        URL : $url\n";
	print "(3)       Name : $name\n";
	print "(4) Sort Index : $sort_index\n";
        print "(5)    Symlink : $symlink\n\n";
	print "(y) Save these changes to the database file. I'm done.\n";
	print "(d) Delete this user from the database entirely.\n";
	print "(q) ABORT. Nevermind. Check the database file back in.\n\n";

	print "Which element would you like to change?\n>>";
	$input = "";
	$input = (<STDIN>);
	chop($input);
	if (($input > 0) && ($input < 6)) {
	   print "What would you like to change it to?\n";
           print "(Enter a single \".\" if you want to empty the field)\n>>";
	   $data = "";
	   $data = (<STDIN>);
	   chop($data);
        } elsif ($input eq "y") {
	   $done = 1;
        } elsif ($input eq "d") {
           print "Are you sure you want to delete this user?\n>>";
	   if (&accept()) {
	       $username = "";
               $url = "";
	       $done = 1;
	   }
        } elsif ($input eq "q") {
           &rcs_check_in_file($master_db_file,
		              "no change");
           return;
        }
	if ($data ne "") {
           if ($data eq ".") {
               $data = "";
           }
	   if ($input == 1) {
	       $username = $data;
	   } elsif ($input == 2) {
	       $url = $data;
	   } elsif ($input == 3) {
	       $name = $data;
	   } elsif ($input == 4) {
	       $sort_index = $data;
           } elsif ($input == 5) {
               $symlink = $data;
	   }
	}

        if(substr($oldurl, 0, 8) eq "/people/") {
            ($osfile) = $oldurl =~ m|^/people/([^/]+)|;
        } else {
            $osfile = "";
        }

        if(substr($url, 0, 8) eq "/people/") {
            ($nsfile) = $url =~ m|^/people/([^/]+)|;
        } else {
            $nsfile = "";
        }

        if($url ne $oldurl) {
            if($osfile && !$nsfile) {
	        next EDITLOOP unless nuke_link($osfile, 1);
	    } elsif(!$osfile && $nsfile) {
		next EDITLOOP unless create_link($nsfile, $symlink, 1);
	    } elsif($osfile && $nsfile && $osfile ne $nsfile) {
		next EDITLOOP unless nuke_link($osfile, 1) && create_link($nsfile, $symlink, 1);
	    }
	    $oldurl = $url;
            $oldsymlink = $symlink;
	} elsif($symlink ne $oldsymlink && $nsfile) {
            $link = $nsfile;
	    if(-l $web_root_dir."/people/".$link) {
                print "tweak $link so it points at $symlink?\n>>";
                if(&accept()) {
   		    if(!unlink($web_root_dir."/people/".$link)) {
                        warn "unlink ${web_root_dir}/people/${link} failed: $!\n";
                        next EDITLOOP;
                    }
		    if(!symlink($symlink, $web_root_dir."/people/".$link)) {
                        warn "symlink ${web_root_dir}/people/${link} failed: $!\n";
                        next EDITLOOP;
                    }
                }
	    } elsif(! -e $web_root_dir."/people/".$link) {
                next EDITLOOP unless create_link($link, $symlink);
            } else {
		warn "${web_root_dir}/people/${link} is not a symlink, i'm not touching it.\n";
		next EDITLOOP;
	    }
            $oldsymlink = $symlink;
	}
    }

    if ($oldname ne "") {
	&DB'DelUser($oldname);
    }
    if ($username ne "") {
	&DB'AddUser($username, $url, $name, $sort_index, $symlink,
                    &cooldate());
    }

    if (&DB'SpewHome($master_db_file) == 0) {
        print "Cannot write $master_db_file. Aborting.\n";
        print "The database file may be in an inconsistent state. Beware.\n";
        exit(0); 
    }

    &rcs_check_in_file($master_db_file, "changed homepage for $username via script");


}

sub input_home {
    local($username) = @_;
    local($name, $url, $index, $file, $new, $old);

    print "What is this person's real name?\n>>";
    $name = &getinput();
    
    $index = &pick_an_index($name);

    print "Is their homepage on our web server?\n>>";
    if (&accept()) {
	print "What is the path of their homepage?\n>>";
	$file = &getinput();

	$file =~ s/\/afs\/athena\//\/afs\/athena.mit.edu\//;
	$file =~ s/\/afs\/sipb\//\/afs\/sipb.mit.edu\//;

	($url, $old, $new) = &pick_symlink_for_file($file);

	$new = ( $root_people_dir . "/" . $new);

	&add_to_database($url, $username, $name, $index, $old, $new);
    } else {
	print "What is this person's URL?\n>>";
	$url = &getinput();
	&add_to_database($url, $username, $name, $index,
			 "", "");
    }
}

sub getinput {
    local($temp);
    $temp = (<STDIN>);
    chop $temp;
    return $temp;
}
