#! /usr/bin/perl -w
use diagnostics;
use strict;

my $arg = shift;

print "Querying hermes for $arg ... ";

my $pid = open FINGER, "-|";
if (!defined $pid)
{
	print "can't fork: $!\n";
	exit 2;
}
elsif (!$pid)
{
	if (not exec 'finger', "$arg\@hermes")
	{
		print "can't exec finger: $!\n";
		exit 2;
	}
}
else
{
	<FINGER>;
	<FINGER>;
	$/ = undef;
	my $finger = <FINGER>;
	close FINGER;

	unless (defined $finger)
	{
		print "no matches\n";
		exit 1;
	}

	my @people = split /\n/, $finger;
	foreach my $person (@people)
	{
		$person =~ s/^([^ ]*) *(.{1,20}[^ ]).*$/$1\t$2/ or $person = '';
	}
	@people = grep /./, @people;

	if    (@people == 0) { print "no matches\n"; exit 1; }
	elsif (@people == 1) { print "1 match\n"; }
	else                 { print scalar(@people), " matches\n"; }

	print "$_\n" foreach @people;
}

