#!/bin/sh
#	program:	issubdom
#	release:	1.5.0
#	input:		a possible subdomain name and a domain name.
#	output:		"true" if first arg is subdomain of second,
#			"false" if not.
#			If the two are identical, output is "false".
#			"ERROR" is output if an error occurred.
#	exit value:	0 if "true", 1 if "false", 2 if error occurred.
#
# 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 $# -ne 2; then
	echo "usage: $0 sub.dom.ain. dom.ain." >&2
	echo 'ERROR'
	exit 2
fi

subdom=$1
dom=$2

# If subdom has dom on it's right side and subdom is not exactly equal to dom,
# then return "true" else "false".
# This is a case-less compare (ignores upper/lower case differences).

echo $subdom | grep -i $dom\$ > /dev/null
if test $? -eq 0; then
	echo $subdom | grep -i \^$dom\$ > /dev/null
	if test $? -ne 0; then
		echo "true"
		exit 0
	else
		echo "false"
		exit 1
	fi
else
	echo "false"
	exit 1
fi

echo 'ERROR'
exit 2
