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


sub mm {
    while(TRUE){
	system("clear");
	print("Main Menu:  FrontEnd fs\n");
	print("-" x (19+length($dir)));
	print("\n");
	print("Current direcory:  $dir\n");
	print("-" x (19+length($dir)));
	print("\n");
	print("1)  List permissions\n");
	print("2)  Change directory\n");
	print("3)  List directory contents\n");
	print("4)  Change permissions\n");
	print("x)  Exit fefs\n\n");
	print("fefs> ");
	$input = (<STDIN>);
	chop($input);
	if($input eq "1")
	{
	    do lsperm();
	    do anykey();
	}
	if($input eq "2")
	{
	    do cd();
	}
	if($input eq "3")
	{
	    system("clear");
	    print("Contents of directory $dir\n");
	    print `/bin/ls -C`;
	    do anykey();
	}
	if($input eq "4")
	{
	    do setperm();
	}
	if($input eq "x"){
	    exit(0);}

    }
}

sub setperm {
    while($input ne "m"){
    system("clear");
    print("Change permissions for directory $dir\n\n");
    print("1)  Change permissions on whole directory\n");
    print("2)  Change permissions on one file (symlink)\n");
    print("m)  Back to Main Menu\n\n");

    print("fefs> ");
    $input =(<STDIN>);
    chop($input);
    if($input eq "1")
    {
	$inputb = "foo";
	while($inputb ne "m") {
	    do lsperm();
	    print("Modify...\n");
	    print("1)  Normal permissions\n");
	    print("2)  Negative permissions\n");
	    print("m)  Back to Change permissions menu\n\n");

	    print("fefs> ");
	    $inputb = (<STDIN>);
	    chop($inputb);
	    if($inputb eq "1")
	    {
		$neg = "";
		do chpperm();
	    }
	    if($inputb eq "2")
	    {
		$neg = "-negative";
		do chpperm();
	    }
	    
	}
    }
    if($input eq "2")
    {
	do makelink();
#	print("Not yet implemented.\n");
	do anykey();
    }
}
}
sub chpperm {
    $inputc = "foo";
    while($inputc ne "m") {
	system("clear");
	do lsperm();
	if($neg){print("NOTE:  Changing NEGTIVE permissions.\n");}
	print("\n1)  Change a user's permissions\n");
	print("2)  Change a group's permissions\n");
	print("3)  Change permissions for all users\n");
	print("m)  Back to Modify menu\n\n");
	
	print("fefs> ");
	$inputc = (<STDIN>);
	chop($inputc);
	
	if($inputc eq "1"){
	    print("\n\nUsername:  ");
	    $name = (<STDIN>);
	    chop($name);
	}
	if($inputc eq "2"){
	    print("\n\nGroup:  ");
	    $group = (<STDIN>);
	    chop($group);
	    $name = "system:".$group;
	}
	if($inputc eq "3"){
	    $name="system:anyuser";
	}
	if(($inputc eq "2") || ($inputc eq "1") ||($inputc eq "3")) {
	    print("\nPlease type all the letters that correspond\n");
	    print("to the $neg access you want $name to have to\n");
	    print("directory $dir\n");
	    print("\nr)  read\nl)  list\ni)  insert\nd)  delete\nw)  write\n");
	    print("k)  lock\na)  administer\nnone) None of these\nall) All of these\n");
	    print("\nfefs> ");
	    $permsi = (<STDIN>);
	    chop($permsi);
	    $error = `fs sa $neg $dir $name $permsi`;
	    if($error){
		print $error;
	    }
	    else {print("Permissions changed.");}
	    do anykey();
	}
    }
}

sub cd {
    system("clear");
    print("(Type ~ to go to your home directory)\n");
    print("Subdirectories of $dir\n");
    print "-" x 70;
    print("\n");
    $sdirs = `/bin/ls -FC`;
    $sdirs =~ s/[\w\d\-\.\*\@~]+[^\/][ \t\n]//g;
    $sdirs =~ s/\/[\t\n ]+/ /g;
    split(/ /,$sdirs);
    $llen = 0;
    foreach $d (@_)
    {
	$pad = (" "x(20-length($d)));
	$llen += length($pad.$d);
	if($llen>70)
	{
	    print("\n");
	    $llen=length($pad.$d);
	}
	print($d.$pad);
    }
    print("\n\nChange to: ");
    $whereto = (<STDIN>);
    chop($whereto);
    if($whereto eq "~"){
	print("Homeward bound...\n");
	$whereto = $ENV{'home'};
    }
    else{
    if($whereto eq ""){
	$whereto = ".";
    }}
    if(!chdir($whereto)){
	print("Sorry, that directory is fascist or doesn't exist...\n");
	do anykey();
    }
    $dir = `pwd`;
    chop($dir);
}
sub lsperm {
    system("clear");
    %PPERMS=%NPERMS=('');
    print("Directory:  $dir can be\n");
    $perms = `fs la $dir`;
    if($perms=~/nvalid/)
    {
	print("Current Directory invalid.\n");
	print("Possibly it is not in AFS.\n");
	do anykey();
    }
    else
    {
	@plin = split(/\n/, $perms);
	$normrights =0;
	$negrights = 0;
	foreach $line (@plin)
	{
	    if($line=~/Negative rights:/)
	    {
		$negrights =1;
		$normrights =0;
	    }
	    if($normrights)
	    {
		@p = split(/ +/, $line);
		$PPERMS{$p[1]} = $p[2];
	    }
	    if($negrights)
	    {
		@p = split(/ +/, $line);
		if($negrights>1){$NPERMS{$p[1]} = $p[2];}
		$negrights=2;
	    }
	    if($line=~/Normal rights:/)
	    {
		$normrights = 1;
	    }
	}
	do sortperms();
    }
}

sub sortperms {
    @read = @list = @insert = @delete = @write= @lock=@administer ="";
    @nread=@nlist=@ninsert=@ndelete=@nwrite=@nlock=@nadminister="";
    while(($group,$ps)=each %PPERMS) {
	if($ps=~/r/){
	    push(@read,$group);}
	if($ps=~/l/){
	    push(@list,$group);}
	if($ps=~/i/){
	    push(@insert,$group);}
	if($ps=~/d/){
	    push(@delete,$group);}
	if($ps=~/w/){
	    push(@write,$group);}
	if($ps=~/k/){
	    push(@lock,$group);}
	if($ps=~/a/){
	    push(@administer,$group);}
    }
    while(($group,$ps)=each %NPERMS) {
	if($ps=~/r/){
	    push(@nread,$group);}
	if($ps=~/l/){
	    push(@nlist,$group);} # 
	if($ps=~/i/){		# 
	    push(@ninsert,$group);}
	if($ps=~/d/){      
	    push(@ndelete,$group);}
	if($ps=~/w/){
	    push(@nwrite,$group);}
	if($ps=~/k/){
	    push(@nlock,$group);}
	if($ps=~/a/){
	    push(@nadminister,$group);}
    }
    print("\nRead by:  ");
    foreach $g (@read) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    if($#nread) {print("\nExcept: ");}
    foreach $g (@nread)
    {
	if($g ne ""){
	    print("$g ");}
    }

    print("\nListed by:  ");
    foreach $g (@list) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    if($#nlist) {print("\nExcept: ");}
    foreach $g (@nlist)
    {
	if($g ne ""){
	    print("$g ");}
    }
    

    print("\nInserted into by:  ");
    foreach $g (@insert) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    
    if($#ninsert) {print("\nExcept: ");}
    foreach $g (@ninsert)
    {
	if($g ne ""){
	    print("$g ");}
    }
    

    print("\nDeleted from by:  ");
    foreach $g (@delete) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    
    if($#ndelete) {print("\nExcept: ");}
    foreach $g (@ndelete)
    {
	if($g ne ""){
	    print("$g ");}
    }
    

    print("\nWritten into by:  ");
    foreach $g (@write) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    
    if($#nwrite) {print("\nExcept: ");}
    foreach $g (@nwrite)
    {
	if($g ne ""){
	    print("$g ");}
    }
    

    print("\nLocked by:  ");
    foreach $g (@lock) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    
    if($#nlock) {print("\nExcept: ");}
    foreach $g (@nlock)
    {
	if($g ne ""){
	    print("$g ");}
    }
    

    print("\nAdministrated by:  ");
    foreach $g (@administer) 
    {
	if($g ne ""){
	    print("$g  ");}
    }
    
    if($#nadminister) {print("\nExcept: ");}
    foreach $g (@nadminister)
    {
	if($g ne ""){
	    print("$g ");}
    }
    
    print("\n\n");

}
sub makelink {
	print("\n\nWarning, this feature is not menu driven.  Would you\n");
	print("like to go back to the previous menu? (y/n)  ");
	$ans = (<STDIN>);
	chop($ans);
#print("--$ans--\n");
if($ans ne "y") {
	print("\n\nContents of directory $dir\n");
	print `/bin/ls -C`;
    print("What file would you like to change permissions on?: ");
    $filnm = (<STDIN>);
    chop($filnm);
    print("What user or group would you like to change permissions for?: ");
    $whsym = (<STDIN>);
    chop($whsym);
    print("Permissions you would like to grant?: ");
    $symperms = (<STDIN>);
    chop($symperms);
    if (!(-d $dir."/.$filnm.fefs"))
    {
	mkdir(".$filnm.fefs","755");
	$bar = `fs sa .$filnm.fefs $whsym $symperms`;
	print("fs sa: $bar\n") if $bar;
    }
	else
	{
	$bar = `fs sa .$filnm.fefs $whsym $symperms`;
	print("$bar") if $bar;
	}
    if((!(-d $filnm)) && (!(-l $filnm))) {
    $foo = `mv $filnm .$filnm.fefs`;
    print("mv: $foo\n") if $foo;
    $baz = `ln -s .$filnm.fefs/$filnm`;
    print("ln: $baz\n");

}
}
}
sub anykey {
    print("\nPress Return To Continue...");
    $garbage=(<STDIN>);
}


# ----Main----
$dir = `pwd`;
chop($dir);
do mm();
