#!/usr/bin/env python

"""hes does Hesiod lookups for the most common types of Hesiod entries."""

import os
import sys

keys = ["passwd", "filsys", "pobox", "gid", "uid", "grplist", "sloc", "cluster",
"group", "pcap", "service", "lpralias", "printinfo", "palladium", "tloc"]

usage = """
Usage: hes thing_you_want_info_about  [next_thing ...]  [type]

Where 'type' is: %s
""" % ", ".join(keys)

def query(thing, type):
	input, output, err = os.popen3(('hesinfo', thing, type))
	# hesinfo prints errors (like queries not existing) to stderr, so if it's
	# empty, we're probably good
	if err.read() == '':
		return output.read().strip()

def printQuery(thing, type):
	"""printQuery takes the output of a Hesiod query, formats it, and prints it
	out"""
	info = query(thing, type)
	if info != None:
		print "%s: %s" % (type.upper().rjust(10), info)

def main():
	# Print the usage information if there were no arguments
	if len(sys.argv) <= 1:
		print usage
		return
	
	# If a specific type of query was asked for, do that for each thing
	if sys.argv[-1] in keys:
		type = sys.argv[-1]
		for thing in sys.argv[1:-1]:
			printQuery(thing, type)
	# Otherwise LOOKUP EVERYTHING!
	else:
		for thing in sys.argv[1:]:
			for type in keys:
				printQuery(thing, type)
			# ...with a blank line between each thing being looked up
			print ""

if __name__ == "__main__":
	main()
