#
set noglob
#####Message Texts######
set mesg1 = "Enter new host entries for /etc/hosts" 
set mesg1a = "in the form: Internet address<TAB>hostname<SP>aliases"
set mesg1b = "To end program, enter blank line at prompt."
set mesg2 = "Address "
set mesg2b = " has already been used, "
set mesg2c = "please reenter that host:"
set mesg3 = "Address must be in the form [1-255].[0-255].[0-255].[1-255]"
########################

cp /dev/null /tmp/nodes
				# break off addresses for comparison
awk '{print $1}' /etc/hosts > /tmp/addresses.$$
				# initial prompt for input
echo $mesg1
echo $mesg1a

get_node:
				# signal that we're ready for more	
echo  -n "ready>"
set NODE = ($<)
				# exit on blank input
if ( $#NODE == 0) then
	goto add_host
endif
				# break address off input for uniqueness check
set ADRS = `echo $NODE | awk '{print $1}'` 
		  		# check uniqueness 
fgrep -s $ADRS /tmp/addresses.$$
				# if its been used already ask to reenter
if ( $status != 1 ) then 
	echo $mesg2$ADRS$mesg2b$mesg2c
	goto get_node
endif
				# check that address is in range
echo $ADRS | grep -s '^[0-9]' 
if ( $status == 1 ) then
	goto bad_in
endif
echo $ADRS | awk '{FS="."} {print $1}' > /tmp/test
if ( `cat /tmp/test` < 1 | `cat /tmp/test` > 255) then
	goto bad_in
endif
echo $ADRS | awk '{FS="."} {print $2}' > /tmp/test
if ( `cat /tmp/test` < 0 | `cat /tmp/test` > 255) then
	goto bad_in
endif
echo $ADRS | awk '{FS="."} {print $3}' > /tmp/test
if ( `cat /tmp/test` < 0 | `cat /tmp/test` > 255) then
	goto bad_in
endif
echo $ADRS | awk '{FS="."} {print $4}' > /tmp/test
if ( `cat /tmp/test` < 1 | `cat /tmp/test` > 255) then
	goto bad_in
endif
				# otherwise add it to used list
echo $ADRS >> /tmp/addresses.$$
echo -n $ADRS >> /tmp/nodes
echo -n '	' >> /tmp/nodes
echo $NODE | sed "/$ADRS /s///" >> /tmp/nodes
goto get_node
				# add new entries to /etc/hosts
add_host:
ex /etc/hosts << EOF > /dev/null
/^.*\..*\..*\..*/
i
`cat /tmp/nodes`
.
w
q
EOF
chmod 644 /etc/hosts

echo "The following entries have been added to /etc/hosts:"
sed -e 's/^/	/' < /tmp/nodes
echo -n "Type RETURN..."
set dummy = ($<)

				# remove scratch files
/bin/rm -f /tmp/addresses.$$
/bin/rm -f /tmp/test
/bin/rm -f /tmp/nodes

exit(0)

bad_in:
echo $mesg3
echo $mesg2c
goto get_node
