#!/bin/csh -f
#
# This calls dumphost and searches the addhost database, returning the
# hostnames of all the hosts matching the criteria given in the form
# "addhostl [[keyword expression] ...]  If addhostl is called in the
# form "addhostl -k", the list of keywords for addhost is returned.  
# 1 is returned if an error occurred, 0 otherwise.
#
# examples:
#	addhostl hostname refuge
#		this returns the hostname refuge
#	addhostl hostname r\* adminaffil unixops
#		this returns all hostnames beginning with r with the
#		adminaffil unixops
#

set path = (/bin /usr/ucb /usr/local/etc)
set dhconf = /tmp/dh.config.$$
set STATUS = 0

if ($#argv == 1) then
	if ($argv[1] == "-k") then
		cat /dev/null	>  $dhconf
		echo 'list'		>> $dhconf
		echo 'quit'		>> $dhconf
		dumphost $dhconf | fmt
		goto END
	endif
	set STATUS = 1
	goto USAGE
endif

if ($#argv == 0) goto USAGE
if (`expr $#argv % 2` != 0) goto USAGE

cat /dev/null > $dhconf
@ n = 1
while ($n < $#argv)
	@ n2 = `expr $n + 1`
	echo "set $argv[$n] $argv[$n2]"	>> $dhconf
	@ n++
	@ n++
end
echo "show hostname"			>> $dhconf
echo "match"					>> $dhconf
echo "quit"					>> $dhconf

dumphost $dhconf

set STATUS = $status
if ($STATUS != 0) then
	echo "ERROR\!"
endif

rm $dhconf

goto END

USAGE:
echo "usage:	addhostl [[keyword expression] ...]"
echo "	addhostl -k"

END:
exit($STATUS)
