#!/bin/sh
#	program:	type
#	release:	1.5.0
#	input:		a network address or domain name of any kind
#	output:		a single word describing the type of element it was:
#				"forward" = forward address ("1.2.3")
#				"inverse" = inverse domain address, must
#					    have an ending period
#					    ("3.2.1.in-addr.Arpa.")
#				"domain"  = normal domain name, must have an
#					    ending period ("nau.edu.")
#			or the word "ERROR".
#			If error occurs, stderr describes it.
#	exit value:	0 if succeeds, 1 if fails (and "ERROR" output).
#
# Copyright (C) 1993-2000 Paul A. Balyoz <pab@domtools.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

AWK=/usr/local/domtools/lib/type.awk

if test $# -ne 1; then
	echo "usage: $0 networkthing" >&2
	echo 'ERROR'
	exit 1
fi

thing="$1"
if test x"$thing" = x""; then
	echo "$0: empty argument passed to me; aborting." >&2
	echo 'ERROR'
	exit 1
fi

allnumeric=false
temp="`echo $thing | sed -e 's/[0-9.]//g'`"
if test x"$temp" = x""; then
	allnumeric=true
fi

answer="`echo $thing | awk -f $AWK`"
status=$?
if test $status -ne 0; then
	echo "$0: $thing is neither an IP address nor a FQDN." >&2
	echo "$0: (remember ending periods on fully qualified domain names!)" >&2
	echo 'ERROR'
	exit 1
fi

if test x"$answer" = x"forward" -a $allnumeric = false; then
	echo "$0: $thing is neither an IP address nor a FQDN." >&2
	echo "$0: (remember ending periods on fully qualified domain names!)" >&2
	echo 'ERROR'
	exit 1
fi

echo "$answer"
exit $status
