#!/bin/bash
#
#	Script to process a "gateways" file into the form needed by
#	the Westerhof "ipip" daemon.
#
#	gateway file on stdin, ipip format route file on stdout

echo "#\n# ipip route table built by $LOGNAME on `date`\n#\n# local routes"
echo "route 44.32.0.0 0xffff0000 open 192.133.104.8"
echo "route 44.20.0.0 0xffff0000 open 192.195.73.3"
echo "#\n# quasi-local routes - we're the open subnet relay"
echo "route 44.130.48.0 0xffffff00 open 15.15.80.34"
echo "#\n# remote routes"

fgrep encap | grep "^route" | grep -v "15.255.240" | \
awk '{
	split($3, s, "/")
	split(s[1], n,".")
	if	(n[1] == "")	n[1]="0"
	if	(n[2] == "")	n[2]="0"
	if	(n[3] == "")	n[3]="0"
	if	(n[4] == "")	n[4]="0"
	if	(s[2] == "1")	mask="0x80000000"
	else if	(s[2] == "2")	mask="0xc0000000"
	else if	(s[2] == "3")	mask="0xe0000000"
	else if	(s[2] == "4")	mask="0xf0000000"
	else if	(s[2] == "5")	mask="0xf8000000"
	else if	(s[2] == "6")	mask="0xfc000000"
	else if	(s[2] == "7")	mask="0xfe000000"
	else if	(s[2] == "8")	mask="0xff000000"
	else if	(s[2] == "9")	mask="0xff800000"
	else if	(s[2] == "10")	mask="0xffc00000"
	else if	(s[2] == "11")	mask="0xffe00000"
	else if	(s[2] == "12")	mask="0xfff00000"
	else if	(s[2] == "13")	mask="0xfff80000"
	else if	(s[2] == "14")	mask="0xfffc0000"
	else if	(s[2] == "15")	mask="0xfffe0000"
	else if	(s[2] == "16")	mask="0xffff0000"
	else if	(s[2] == "17")	mask="0xffff8000"
	else if	(s[2] == "18")	mask="0xffffc000"
	else if	(s[2] == "19")	mask="0xffffe000"
	else if	(s[2] == "20")	mask="0xfffff000"
	else if	(s[2] == "21")	mask="0xfffff800"
	else if	(s[2] == "22")	mask="0xfffffc00"
	else if	(s[2] == "23")	mask="0xfffffe00"
	else if	(s[2] == "24")	mask="0xffffff00"
	else if	(s[2] == "25")	mask="0xffffff80"
	else if	(s[2] == "26")	mask="0xffffffc0"
	else if	(s[2] == "27")	mask="0xffffffe0"
	else if	(s[2] == "28")	mask="0xfffffff0"
	else if	(s[2] == "29")	mask="0xfffffff8"
	else if	(s[2] == "30")	mask="0xfffffffc"
	else if	(s[2] == "31")	mask="0xfffffffe"
	else if	(s[2] == "32")	mask="0xffffffff"
	printf "route %s.%s.%s.%s %s open %s\n",n[1],n[2],n[3],n[4],mask,$5 }'
#	printf "route %s %s open %s\n", subnet, mask, $5 }'

echo "#\n# the world via Brian's machine at UCSD - mirrorshades.ucsd.edu"
echo "route 0.0.0.0 0x00000000 open 128.54.16.18"
echo "#\n# the end"
