#!/usr/bin/perl

my $host = "/usr/bin/host";

sub hesResolve {
	my ($name, $type) = @_;
	my @rval = ();
	my $rec = "$name.$type.ns.athena.mit.edu";
	my $q = `$host -t txt $rec`;
	my @qs = split(/\n/, $q);
	foreach (@qs) {
	    # This regexp parses the host command's output, so it can
	    # break if the host command is modified
	    if ($_ =~ /^$rec (?:.* )?text \"(.*)\"$/) {
		@rval = (@rval,$1);
	    }
	}
	
	return @rval;
}

my @types = ("passwd", "filsys", "pobox", "gid", "uid", "grplist", "sloc", 
	  "cluster", "group", "pcap", "service", "lpralias", "printinfo", 
	  "palladium", "tloc");

if (@ARGV == 0) {
    print "\nUsage: hes thing_you_want_info_about [next_thing ...] [type]\n\n";
    print "Where 'type' is: ";
    print "$types[0]";
    foreach my $t(@types[1..$#types]) {
	print ", $t";
    }
    print "\n";
}


my @modes = @types;
my @things = @ARGV;
foreach my $t(@types) {

    if ($t eq lc($ARGV[$#ARGV])) {

	@things = @ARGV[0..$#ARGV-1];
	@modes = ($ARGV[$#ARGV]);
	break;
    }
}

foreach my $mode(@modes) {

    foreach my $thng(@things) {
	if ($thng =~ /^(\w{1}[\w\-\.]*)$/) {
	    $targ = $1;
	} else {
	    print STDERR "Invalid characters in '$thng'.\n";
	    exit 1;
	}
	
	my @rets = hesResolve($targ, $mode);
	if (@rets > 0) {
	    foreach my $ret(@rets) {
		if (@modes > 1) {
		    printf "%10s: ", uc($mode);
		}
		print "$ret\n";
	    }
	}
    }
}

exit 0;
