#!/bin/csh
#
#	$Header: /usr/local/src/named/RCS/create_inaddr,v 1.2 91/02/21 17:44:40 aggarwal Exp $
#
# $Log:	create_inaddr,v $
#Revision 1.2  91/02/21  17:44:40  aggarwal
#	o stripped trailing period automatically
#	o can now handle fully qualified names.
#
#
########################################################################
# Converts addresses into in-addr format suitable to be read in directly
# into a nameserver file (but with little or no comments)
#
# Usage:
#	$0 <domain name> [ network number ...]
#
# Just give it the domain name and it does the rest. However, you might
# want to make sure that a "ls domain" works in yur nslookup. The end of
# the files have a "Eof" so indicate that the file was created ok. Also,
# the files created are <net number>.inaddr - previously existing files
# are overwritten.
#
# Given only the domain name, it creates inverse files for all addresses
# Given domain name and networek numbers, it creates the inverse files
# for only those network numbers in the given domain.
#
#	vikas@jvnc.net			Nov 1990
# Method:
#	echo argument into stdin of awk
#	use awk to separate fields and provide input to dc
#	use dc to calculate the IP address value and print it out in hex
#	use awk to pad to 8 bytes, if necessary
#
#
# set to name of the nameserver
if (! $?Server ) set Server = localhost	
set TMP1 = /var/tmp/create_inaddr.tmp1
set TMP2 = /var/tmp/create_inaddr.tmp2
set Status = 1
#
set path = (. /bin /usr/bin /usr/ucb /etc /usr/etc /usr/bin/X11 /usr/local/bin)
set Date = `date`

onintr cleanup

if ($#argv == 0) then
	echo "USAGE: $0 <domain> <net-number net-number...>"
	echo -n "Enter domain name: "
	set Domain = $<
else
	set Domain = $1
	shift
endif

set Domain = `echo $Domain | sed 's/\.$//'`	# strip trailing period
rm -f $TMP1 $TMP2
nslookup <<Eoo >/dev/null
lserver $Server
ls ${Domain}. > $TMP1
Eoo

if ( -z $TMP1 ) then
	echo "Error in getting nameserver records"
	goto cleanup
endif

# Now sort all the records and concatenate the name to the address
# in the form:		18.5.4.3.nisc
#
cat $TMP1 | egrep -v 'server =' | awk ' /^ / { print $2 "." $1}' | sort | uniq> $TMP2
rm -f $TMP1

# Now extract all the networks from the listed hosts (get class A-D)
# if no nets supplied on the command line.
#
if ( $#argv == 0 ) then
  cat $TMP2 |\
    awk ' BEGIN { FS="." ; prevdom = 0 } \
	{ \
	  if ($1 < 128)  dom = $1 \
	  else { \
		if ($1 < 192) dom = $1 "." $2 \
		else { \
	 		if ($1 < 224) dom = $1 "." $2 "." $3 \
			else dom = $1 "." $2 "." $3 "." $4 \
		} \
	  } \
	} \
	{ if ( dom != prevdom ) \
		printf ("%s ", dom) ; \
	} \
	{ prevdom = dom } \
  ' > $TMP1 			# end of awk program (couldn't directly do set)

  set dom = `cat $TMP1`		# shell wouldn't do a set directly
  rm -f $TMP1
else
  set dom = "$*"				# use commnad line args
endif

foreach i ( $dom )
  rm -f ${i}.inaddr
  if ( -e ${i}.inaddr ) then
	echo "Error in deleting file ${i}.inaddr, skipping..."
	continue
  endif

  set j = `echo $i | awk -F. ' { for (x=NF; x > 0; --x) printf ("%s.", $x) }'`
  echo "; Inverse domain mapping for $i" > ${i}.inaddr
  echo "; Created on $Date by $user using $0" >> ${i}.inaddr
  echo ";" >>${i}.inaddr
  echo -n '$ORIGIN ' >> ${i}.inaddr
  echo "in-addr.arpa." >> ${i}.inaddr

  grep "^$i" $TMP2 | sed 's/\.$/.QualiFied/'  |\
    awk ' BEGIN { FS="." } \
	{ dom="" ; qual=0 } \
	{ for (x = 4 ; x > 1 ; --x) printf ("%s.", $x) } \
	{ for (x = 5 ; x < NF ;++x) dom = sprintf("%s%s.", dom, $x) } \
	{ if ( $NF != "QualiFied" ) dom = sprintf("%s%s.DoMain.", dom,$NF) } \
	{ printf ("%s\t\tIN\tPTR\t%s\n", $1, dom) } \
	END { printf ";\n; Eof" } '  |\
	sed "s/DoMain/$Domain/"  >> ${i}.inaddr
#
  echo -n "."
end

set Status = 0
cleanup:
	echo ""
	rm -f $TMP1 $TMP2
	exit $Status
