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

die "Usage: $0 dumpfile\n" unless ($dumpfile = $ARGV[0]);

open (DUMP, $dumpfile) || die "Error opening $dumpfile: $!\n";

while (1)			# Server status section.
{				# No bogons here.  I hope.
    last unless ($dline = <DUMP>);
    chop $dline;
    last unless ($dline =~ /^(\d+)\:(\d+\.\d+\.\d+.\d+)\/(SERV\_\w+)$/);
    $server_ip[$1]=$2;
    $server_state[$1]=$3;
}

DBLOOP while (1)		# Location DB section
{				# Turns out slashes in the display
				# aren't escaped.  Joy.
    last DBLOOP if ($dline =~ /^\d+\.\d+\.\d+\.\d+\/\d+\:$/); 
    $fields = split (/\//, $dline);
    if ($fields == 7)		# Normalcy.  I'm not doing more sanity
    {				# checking here because if someone has
	($user, $realm) = split (/\@/, $_[0]); # gone this far to fool
	push(@loc_user, $user);	# this script, there's not a lot we can
	push(@loc_realm, $realm); # do about it.
	push(@loc_mach, $_[1]);
	push(@loc_date, $_[2]);
	push(@loc_disp, $_[3]);
	push(@loc_exp, $_[4]);
	push(@loc_ip, $_[5]);
	push(@loc_port, $_[6]);
    }
    else			# Something's bogus.
    {				# Insert cleverness here.
	$loc_bogon++;
	if ($dline =~
	    /^([\w\.\-]+)\@([\w\.\-]+)\/([\w\.\-]+)\/([\w\ \.\-:]+)\/(.*)$/)
	{			# Try to pull the initial
	    $user = $1;		# fields out.  If we can't, eit.
	    $realm = $2;
	    $machine = $3;
	    $date = $4;
	    $rest = $5;
	    if ($rest =~ /^(.*)\/(OPSTAFF|RLM_VIS|RLM_ANN|NET_VIS|NET_ANN)\/(\d+\.\d+\.\d+\.\d+)\/(\d+)$/)
	    {			# If we've got the last fields
		push (@loc_user, $user); # then we're home free.
		push (@loc_realm, $realm);
		push (@loc_mach, $machine);
		push (@loc_date, $date);
		push (@loc_disp, $1);
		push (@loc_exp, $2);
		push (@loc_ip, $3);
		push (@loc_port, $4);
	    }
	    else		# We've got the first few fields, but
	    {			# not the end.
		while (1)
		{
		    last DBLOOP unless ($dline = <DUMP>);
		    chop $dline;
		    last DBLOOP if ($dline =~ /^\d+\.\d+\.\d+\.\d+\/\d+\:$/); 
		    $fields = split (/\//, $dline);
		    next DBLOOP if ($fields == 7); # I give up; you win.
		    if ($dline =~ /^(.*)\/(OPSTAFF|RLM_VIS|RLM_ANN|NET_VIS|NET_ANN)\/(\d+\.\d+\.\d+\.\d+)\/(\d+)$/)
		    {
			push (@loc_user, $user);
			push (@loc_realm, $realm);
			push (@loc_mach, $machine);
			push (@loc_date, $date);
			push (@loc_disp, $rest . "\n" . $1);
			push (@loc_exp, $2);
			push (@loc_ip, $3);
			push (@loc_port, $4);
			last DBLOOP unless ($dline = <DUMP>);
			chop $dline;
			last DBLOOP if 
			    ($dline =~ /^\d+\.\d+\.\d+\.\d+\/\d+\:$/);
			next DBLOOP;
		    }
		    else
		    {
			$rest .= ("\n" . $dline);
		    }
		}
	    }
	}
	else			# If we can't be clever, dump
	{			# this one and go on.
	    $loc_tossed++;
	}
    }
    last DBLOOP unless ($dline = <DUMP>);
    chop $dline;
}

CLIENTLOOP while (1)			# Client subs section
{
    local ($curclient);
    last if /^Triplet/;
    if /^(\d+\.\d+\.\d+\.\d+)\/(\d+)\:$/
    {
	push(@clients, $1);
	$subservers{$1}=$2;
	$curclient=$1;
	PORTLOOP while (1)
	{
	    last CLIENTLOOP unless <DUMP>;
	    chop;

	}
	
    }
    last CLIENTLOOP unless <DUMP>;
    chop;
}

REVLOOP while (1)		# Client subs section (reverse index)
{

}
