#!/bin/csh -f
#
# Program to create newton database files.
#
# Tom Coppeto
# 13 February 1994
#

set path = (/usr/ucb /bin /usr/bin /bin/athena /usr/athena/bin)

set out   = "/tmp/nets.out"
set tmp   = "/tmp/nets.tmp"
set hosts = "/afs/net/admin/hosts/hstath.txt"
set nets  = "/afs/net/admin/hosts/networks.txt"
set drops = "/afs/net/admin/assignments/ethernet-drops"
echo "building header..."

cp /dev/null $out
echo ".title MIT Network"                  >> $out
echo ".author Tom Coppeto"                 >> $out
echo ".isbn 0-1234-5678-9"                 >> $out
echo ".date 3/14/94"                       >> $out
echo ".key MIT Network"                    >> $out
echo ".copyright (c) 1994 Massachusetts Institute of Technology" >> $out
echo ""                                    >> $out
echo ".subject 1 Centered"                 >> $out
echo "The MIT Network"                     >> $out
echo ""                                    >> $out
echo "The entries in this here network database are indexed by network and object. Use the index frob below to navigate through the pages.">> $out
echo ""                                    >> $out
echo "The data for this table was last updated March 14, 1994 by tom@mit.edu.">> $out
echo ""                                    >> $out
echo "Have a nice day and remember, don't turn on arp debugging\!" >> $out
echo ""                                    >> $out
echo ".subject 2 name=networks"            >> $out
echo "Network Table"                       >> $out

echo "building network table..."

fgrep "Network table generated by" $nets   >> $out
echo ""                                    >> $out
fgrep NETWORK $nets | awk '{printf("%s %s (%s)\n%s %s %s %s %s %s\n\n", $1, $3, $5, $7, $8, $9, $10, $11, $12)}'               >> $out 

echo "building host table..."
echo ".subject 2 name=hosts"               >> $out
echo "Host Table"                          >> $out
fgrep "Hosts table generated by" $hosts    >> $out

rm -f $tmp
foreach net (`fgrep NETWORK $nets | awk '{print $5}' | awk '{FS="."; print $1"."$2}' | grep 18. | sort -u`)
	echo ".subject 3 name=hst-$net.0.0 StartsPage"   >> $out
	echo "hosts on $net.0.0"                         >> $out
	set print = "yes"
	set count = "0"
	foreach sub (`fgrep $net. $hosts | awk '{print $3}' | awk '{FS="."; print $3}' | sort -n | uniq`)
		echo ".subject 4 name=hst-$sub StartsPage" >> $out
		echo "hosts from $net.$sub.0"              >> $out
		fgrep $net.$sub. $hosts                     > $tmp
		foreach i (`awk '{print $3}' $tmp | awk '{FS="."; print $4}' | sort -n | uniq`)
			if($print == "yes") then
				echo ".subject 5 name=hst-$net.$sub.$i StartsPage" >> $out
				echo "hosts from $net.$sub.$i" >> $out
				echo ""                        >> $out
				set print = "no"
			endif
	       		fgrep "$net.$sub.$i " $tmp | awk '{printf("%s %s\n%s\n%s  %s\n\n", $1, $5, $3, $7, $9)}' >> $out
			set count = `echo $count 1 + p | dc`
			if($count == "25") then
				set print = "yes"
			endif
		end
	end
end


echo "building repeater table..."
echo ".subject 2 name=rptr"                >> $out
echo "Repeater Table"                      >> $out

foreach net (`fgrep NETWORK $nets | awk '{print $5}' |  awk '{FS="."; print $1"."$2}'`)
	rm -f $tmp
	echo ".subject 3 name=rptr-$net.0.0 StartsPage"   >> $out
	echo "repeaters on $net.0.0"                      >> $out
	fgrep "$net " $drops 	                           > $tmp
	mv $tmp $tmp.2
	sort -n +2 +3 $tmp.2                               > $tmp
	foreach rep (`fgrep "$net " $tmp | awk '{print $2}' | sort | uniq`)
		echo ".subject 4 name=$rep StartsPage"    >> $out
		set name = `hostinfo -h rep-$rep`
		set addr = `hostinfo -a rep-$rep`
		echo "repeater $name ($addr $rep)" >> $out
	        fgrep "$rep " $tmp | awk '{printf("drop %s %s\nport %s %s/%s\ncloset %s (%s)\n%s %s %s %s %s %s %s %s %s\n\n", $6, $7, $2, $3, $4, $5, $1, $8, $9, $10, $11, $12, $13, $14, $15, $16)}' >> $out
#		fgrep "$rep " $tmp | awk '{printf("     port %-4s %s/%-2s....drop %s %s\n", $2, $3, $4, $6, $7)}' >> $out
	echo "" >> $out
	end
end

echo done.
