#!/usr/athena/bin/perl

require 'sys/socket.ph';

$pad_number = 19;

# A list of the hosts in the sipb office.
@hosts = ("portnoy", "savage", "btc",              # sparc/solaris
	  "opus", "steve",                         # indy
	  "cutter", "granola", "zorp",             # fast i386
          "oliver", "pickled",                     # RS/6000
	  "yaz",     "hodge",                      # dec 5000 and 3100
	  "lime",                                  # slow i386
	  "zsr",                                   # unreliable i386
#	  "milq",                                  # vaxen
#	  "speaker", "slarty",                     # rts
          "snork", "milo"                          # not-always-working
#	  , "inf", "shim-sham", "hal", "binkley",
#         "ds9", "weyr", "maze", "tori", "glacier",#other machines I like
#	  "packet-drop", "medic", "akom", "so", "rsa-129"
	  );

@pika_hosts = (
	       "pestilence", "plague",
	       "death", "famine", "war"
	       #, "sanj-o-matic", "anarch", "scrubbing-bubble"
		);

@consult_hosts =("atc", "sonny", "navigator", "momoney", "tailgunner",
                 "copilot", "bombardier", "dale");

@esg_hosts = ("lock", "torque", "hammock", "pick-gun", "bander", "l-slide");

@fen_hosts = ("jhereg", "loth", "i-win");
# bunch of lame-os must have left for the summer
# or something. None of their hosts are up now. :-)

# These are magic numbers. But they're not *these* numbers, under Solaris.
# $AF_INET = 2;
# $SOCK_STREAM = 1;
$sockaddr = 'S n a4 x8';

# The port number for finger is 79
$portnum = 79;

($name, $aliases, $proto) = getprotobyname("tcp");

foo:
# Command line args:
#  -Z  Send output personal via zephyr
#  -P  Pika cluster
#  -C  Consult cluster
#  -E  ESG cluster
#  -F  Fenway
while ($#ARGV >= $[) {
    $_ = shift ARGV;

    /^-Z$/ && do {$zephyr = 1; next foo;};
    /^-P$/ && do {@hosts = @pika_hosts; next foo;};
    /^-C$/ && do {@hosts = @consult_hosts; next foo;};
    /^-E$/ && do {@hosts = @esg_hosts; next foo;};
    /^-F$/ && do {@hosts = @fen_hosts; next foo;};
}

# -Z flag handled here
open(STDOUT, "| /usr/athena/bin/zwrite -n $ENV{USER} > /dev/null") if $zephyr;

foreach (@hosts) {
    print if  $^W; 
    ($name, $aliases,$hostaddr) = (gethostbyname($_))[0,1,4];
# [2,3] = (type,len)

    $name =~ tr/A-Z/a-z/;
    $name = $1 if ($name =~ /(.+)\.mit\.edu/);

    $where = pack($sockaddr, &AF_INET, $portnum, $hostaddr);

    socket(FSOCK, &AF_INET, &SOCK_STREAM, $proto) || die "socket: $!\n";

    connect(FSOCK, $where) || next; #die "Couldn't connect to port: $!\n";
    select(FSOCK); $| = 1; select(STDOUT); $| = 1;

    print FSOCK "\n";

    &munge_finger_output(FSOCK, $name);

    close(FSOCK);
}

sub munge_finger_output {
    local($fhandle, $hostname) = @_;

    until ((($b = <$fhandle>) =~ /^Login.*/) || (! $b)) {}

    if ($b =~ /^Login.*/) {
	print STDOUT &pad("[$hostname]: ");
	while (<$fhandle>) { 
	    chop;
	    split(/ /);
	    $names{$_[0]}++;
	}
	    
	foreach (keys %names) {
	    print STDOUT "$_ ";
	}
	undef(%names);
	print STDOUT "\n";
    }
}

sub pad {
    local ($str) = @_;
    $p = ' 'x($pad_number - length($str)) . $str;
    $p
}
