#!BOURNESHELL
#	program:	addr2mask
#	input:		only one network address on the command line,
#			consisting of dot-separated octets or words.
#	output:		the default netmask for the network, according to
#			it's classification (class A, B, C).  No "D" or beyond.
#			No RFC1101 resource-records taken into consideration.
#			Usage printed only if called with wrong number of args.
#	example:	"134.114.64.24" -> "255.255.0.0"
#	exit value:	0 if nothing goes wrong, 1 if something did.

if test $# -ne 1; then
	echo "usage: $0 ipaddress" >&2
	echo '       where address must be in class A, B, or C.' >&2
	echo 'ERROR'
	exit 1
fi

if test "`DOMBIN/type $1`" != "forward"; then
        echo "$0: argument $1 is not an IP address." >&2
        echo 'ERROR'
        exit 1
fi

firstoct=`echo $1 | awk -F. '{print $1}'`
status=$?

if test $status -ne 0; then
	status=1
else
	if test $firstoct -eq 0; then
		status=1
	else

		if test $firstoct -lt 128; then
			echo "255.0.0.0"
		else
			if test $firstoct -lt 192; then
				echo "255.255.0.0"
			else
				if test $firstoct -lt 224; then
					echo "255.255.255.0"
				else
					status=1
				fi
			fi
		fi
	fi
fi

if test $status -ne 0; then
	echo 'ERROR'
fi

exit $status
