#!/usr/bin/perl
# defaults
my $HOME = "/home/seds";
my $SKEL = "$HOME/skel";
my $SHELL = "/opt/local/bin/tcsh";
my $DIR = '/servers/majordomo/lists';
# Get info
my $NAME = &name;
my $USER = &user;
my $PHONE = &phone;
my $GROUP = &group;
my $UID = &uid($GROUP);
my @GROUPS = &groups;
my $DATE = &expires;
# TO DO:
# virtual base function, etc.
# use libnet to write PH entry

sub name {
print "Name (First MI. Last): ";
chop (my $NAME = <STDIN>);
unless(&okay("Full Name is $NAME?")) { &name; }
return $NAME;
}

sub user {
print "Username (max 8 chars): ";
chop (my $USER = <STDIN>);
unless(&okay("Username is $USER?")) { &user; }
return $USER;
}

sub phone {
print "Local Phone #: ";
chop (my $PHONE = <STDIN>);
unless(&okay("Phone # is $PHONE?")) { &phone; }
return $PHONE;
}

sub group {
print "Primary Group (e.g. 7337 for SEDS):\n";
chop (my $GROUP = <STDIN>);
unless(&okay("Primary group is $GROUP?")) { &group; }
return $GROUP;
}

sub groups {
### ****Get supplementary groups ****
# in the future, pull list from /etc/group
my @GROUPS =
('sysadmin','node','seds-www','seds-ftp','galaxy','hackers',
'sedsat','flandrau');
my @MYGROUPS = ();
foreach (@GROUPS) {
   if(&okay("Append user to $_?")) {
       push(@MYGROUPS,$_);
   }
}
return @MYGROUPS;
}

sub uid {
my $GROUP = shift;
my @UIDS = ();

open(IN, "/etc/passwd") || die "Could not read /etc/passwd\n";
while(<IN>) {
    @row = split(/\:/);
    if($row[3] == $GROUP) { push(@UIDS, $row[2]); }
}
return ($UIDS[$#UIDS])+1; # sort and return max @UIDS
}

sub expires {
# Get Time
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
# Account expires in date + 1 year + 1 month
$mon +=2; 
++$year;
my $DATE =  "$mon/01/$year";
print "Account expires on [$DATE]: ";
chop (my $DATEIN = <STDIN>);
if($DATEIN eq '') { $DATEIN = $DATE; }
unless(&okay("Account expires on $DATEIN")) { &expires; }
return $DATE;
}

# Add user with useradd command
my $cmd = "useradd -c \"$NAME\" -d $HOME/$USER -e $DATE -g $GROUP ";
if($#GROUPS>0) { 
    $cmd .= "-G ";
    foreach $i (0 .. $#GROUPS) { 
	$cmd .= "$GROUPS[$i]"; 
	unless($i ==  $#GROUPS) { $cmd .= ","; }
	}
}
$cmd .= " -m -k $SKEL -u $UID -s $SHELL $USER";
# Detect if user is root
my $loser = getlogin;
if($loser eq 'root') { if(&okay("Run useradd command")) { `$cmd`; } }
else { print "\n$cmd\n\n"; }

#### *** Append name to lists ***
my @LIST =
('uaseds','hackers','anode','rockets','sedsat','telescope','wa7iyg');

foreach (@LIST) {
if(&okay("Append $USER to $_?")) {
   open(OUT, ">> $DIR/$_") || print "Could not open $LIST Skipping\n";
   if($loser eq 'root') { print OUT "$USER\n"; } else { print "$USER\n"; }
   close OUT;
   }
else { next; }
}

# Add entry to UASEDS Phone Book
print<<END;
Entry for the Phone Book HTML:

<TR><TD>$NAME</TD>
<!-- $DATE --><TH>$PHONE</TH><TD>$USER\@seds.org</TD></TR>

Now run edquota $USER
END
# Add PH entry

# Mail message to user

sub okay {
        local($message) = @_;
        print "\n\n$message (y/n)? ";
        $input = join('', getc(STDIN), getc(STDIN));
        if($input =~ /y/i) { return(1); }
        elsif($input =~ /n/i) { return(0); }
        else { &okay; }
        }
                       
