#!/usr/athena/bin/perl

&set_defaults;
&process_args;
&create_new_list;

# ----------------------------------------------------------------------
# set_defaults - Set default values for contact information files
#
# Global variables set
#	old_contact_file	File containing machine & contact information
#				from the last release
#	new_machine_list	File containing a list of machines 
#				needing contact information.
# ----------------------------------------------------------------------
sub set_defaults
{
$old_contact_file = 
    "/mit/release/R8.0/testers/early/sgi.contacts";

$new_machine_list =
    "/mit/release/R8.1/early/testers/sgi.list";

}

# ----------------------------------------------------------------------
# process_args - Process command line arguments
#
# Global variables set
#	old_contact_file	File containing machine & contact information
#				from the last release
#	new_contact_file	File to create with new contact information
#	new_machine_list	File containing a list of machines 
#				needing contact information.
# ----------------------------------------------------------------------
sub process_args

{
    while (@ARGV) {
      ARGS: {
	  $_ = shift (@ARGV);
	  if ( /-old/ ) {
	      $old_contact_file = shift (@ARGV);
	      last ARGS;
	  }
	  if ( /-mach/ ) {
	      $new_machine_list = shift (@ARGV);
	      last ARGS;
	  }
	  print "Unknown option $_\n";
	  print "Usage is: $0 [-old <contact info>] ";
	  print "[-machines <file with machine names>]\n";
	  exit 0;
      }
    }
    
    $new_contact_file = $new_machine_list . ".contacts";
}

# ----------------------------------------------------------------------
# Create_new_list - Create a new contact information list based on 
#		    old data.
#
# Global variables referenced
#	old_contact_file	File containing machine & contact information
#				from the last release
#	new_contact_file	File to create with new contact information
#	new_machine_list	File containing a list of machines 
#				needing contact information.
# ----------------------------------------------------------------------
sub create_new_list
{
    local ($machine);

    open (MACHLIST, "$new_machine_list") ||
	die "Could not read list of workstations.\n";

    open (NEW_CONTACTS, ">$new_contact_file") ||
	die "Could not open new contact information file for writing.\n";

    open (OLD_CONTACTS, "$old_contact_file") ||
	die "Could not open old contact information file.\n";

    while ($_ = <OLD_CONTACTS>) {
	($machine, $user) = split (' ', $_, 2);
	chop ($user);
	$contact{$machine} = $user;
    }

    close OLD_CONTACTS;

    while ($machine = <MACHLIST>) {
	chop ($machine);
	if (defined ($contact{$machine})) {
	    printf (NEW_CONTACTS "%-25s\t%s\n", $machine, 
		    $contact{$machine});
	}
	else {
	    printf (NEW_CONTACTS "%-25s\t\n", $machine);
	}
    }
    close NEW_CONTACTS;
    close MACHLIST;
}
