#!/bin/sh
#	program:	netname and gw (same code for both tools)
#	release:	1.5.0
#	input:		network address in normal format ("134.114.64"),
#			optional nameserver to direct queries to.
#	output:		network-name domain record,
#			or the word "ERROR" (distinguishable from a real
#			record because it has no period on the end).
#	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.
#

if test $# -lt 1 -o $# -gt 2; then
	echo "usage: $0 [@nameserver] net-IP-addr" >&2
	echo 'ERROR'
	exit 1
fi

ip=
nameservers=
atnameserver=
while test $# -gt 0; do
        case "$1" in
                @*)  atnameserver="$1" nameservers=`echo $1 | sed -e 's/@//'` ;;
                *)  ip="$1" ;;
        esac
        shift
done
if test x"$ip" = x""; then
        echo "$0: No network IP address specified; aborting." >&2
        echo "usage: $0 [@nameserver] net-IP-addr" >&2
        echo 'ERROR'
        exit 1
fi

# Force network number into host-zero format
addr=`/usr/local/domtools/bin/netwithzeros $ip`
if test x"$addr" = x"ERROR"; then
        echo "$0: netwithzeros failed to work on $ip IP address." >&2
        echo 'ERROR'
        exit 1
fi

# Convert network number into in-addr format
net=`/usr/local/domtools/bin/f2iaddr $addr`
if test x"$net" = x"ERROR"; then
        echo "$0: f2iaddr failed to work on $addr IP address." >&2
        echo 'ERROR'
        exit 1
fi

# Perform PTR lookup on that domain name, write it to STDOUT
/usr/local/domtools/bin/ptr $atnameserver $net
status=$?
if test $status -ne 0; then
        echo "$0: ptr failed on $net" >&2
#	echo 'ERROR'		already generated by ptr tool
	exit 1
fi

exit $status
