#!/usr/athena/bin/perl

# Given list of students, make directories and set ACLs appropriately


while (<>) {
    chop;
    $student = $_;

    # get name via hesiod info, name is fifth field
    $password = (split(':',`hesinfo $_ passwd`))[4];
    if ($password !~ /,/) {
	die "Error finding name for $_\n";
    }
    @uname = split(',',$password);
    $uname = $uname[0];
    #get first name
    @fname = split(/[ ,]/,$password);
    $fname = lc $fname[0];
    $fname =~ s/[^a-z-]//g;
#    print "$fname\n";
    if ($fname =~ /^-/) {
	print STDERR "Warning: $_ name '$fname' begins with -, changed to 'dash$fname'\n";
	$fname = "dash$fname";
    }
     $fname{$_} = $fname;
    if ($fname{$_} eq "") {
	die "Error finding first name for $_\n";
    }
}

foreach $student (sort keys %fname) {
    # create first-name directory unless it already exists
    print STDERR "$student $fname{$student}\n";
    unless (-d "$fname{$student}") {
	unless (mkdir("$fname{$student}", 0755)) {
	    die "Couldn't make directory for $student: $!";
	}
    }
    $fs = `fs sa $fname{$student} $student l system:anyuser none system:authuser none`;
    if (mkdir("$fname{$student}/$student", 0755)) {
	$fs = `fs sa $fname{$student}/$student $student read system:expunge ld system:acs rlidwka system:anyuser none system:authuser none -clear`;
    } else {
	die "Couldn't make directory for $student: $!";
    }
}
