#!/usr/local/bin/perl
#
# froshlog - create freshman logins
#
# assumes first field is alias
#
# usage: froshlog config ph-output login-output [files]
#	froshlog prod.cnf sf.new frosh.logins sf.prefrosh >frosh.problems
#	         $CONFIG  $PH    $FROSH       $STDIN

if ($#ARGV<3)
  {print STDERR "Usage: froshlog config ph-output login-output [files]\n";exit 1;}
 
#
# find the numbers of the name and alias fields
$anum = $enum = $cnum = -1;
$config=shift;
open (CONFIG,$config) || die "$config: $!\n";
while (<CONFIG>)
{
  if (/:curriculum:/) {($cnum)=(split(':'))[0];}
  elsif (/:alias:/) {($anum)=(split(':'))[0];}
  elsif (/:email:/) {($enum)=(split(':'))[0];}
  elsif (/:CCSO-accounts:/) {($canum)=(split(':'))[0];}
}

$ph = shift;
$frosh = shift;
open(PH,">$ph") || die "$ph: $!\n";
open(FROSH,">$frosh") || die "$frosh: $!\n";

while (<>)
{
  if (/\t$cnum:incoming/o && !/\t$enum:/o && !/\t$canum:[^@]+@uxa/o &&
      !/\t$canum:[^@]+@ux4/o)
  {
    $alias = $_;
    chop($alias);
    $alias =~ s/.*\t$anum://;
    $alias =~ s/$anum://;
    $alias =~ s/\t.*//;
    if (length($alias)>8) {print;}
    else
    {
      $_ =~ s/\n/\t$enum:$alias@ux4.cso.uiuc.edu\t$canum:$alias@ux4\n/;
      print FROSH;
    }
  }
  print PH;
}
