#!/bin/sh
#
#	Domcheck - Check UK-Sendmail domain data files for obvious blunders
#
#	Copyright (c) 1988 Jem Taylor
#

if [ $# -lt 1 ]
then	echo	usage: $0 files...
	exit
fi

for i in $@
do
	sed -e 's/#.*//' -e '/^$/d' $i | awk '
BEGIN	{ bad=0; ok=1; format=ok }
/%s[ 	.]/||/%s$/ { printf "%s: line %d - %%s has no meaning in DOMAIN data\n", file, NR 
	  format=bad
	}
NF>2	{ printf "%s: line %d - %d columns of data\n", file, NR, NF
	  format=bad
	}
/[][<>|\\*(){}~`";:,?\/]/ {
	  printf "%s: line %d - bogus character\n", file, NR
	  format=bad
	}
	{ for ( i=1 ; i <=2 ; i++ )
	  {	if (substr($i,1,1)==".")
	  	{	printf "%s: line %d - leading dot column %d\n",\
					file, NR, i
			format=bad
		}
		if ( substr($i,length($i),1) == "." )
		{	printf "%s: line %d - trailing dot column %d\n",\
					file, NR, i
			format=bad
		}
	  }
	  if ( format == bad )
	  {	print "	" $0
	  	format=ok
	  	errval++
	  }
	}
END	{ if ( errval > 0 )
		printf "%d errors detected\n", errval
	  exit errval
	} ' file=$i -
done

# delete everything from first whitespace or first dot in domain declarations
duplicates=`sed -e '/^#/d' -e '/^$/d' $@ \
| awk '{ if (index($1,".")==0 && $1==substr($2,1,index($2,".")-1)) print $2 }'\
| sed -e 's/[. 	].*//' | sort | uniq -d`

if [ -n "$duplicates" ]
then
	echo '
================ Domain entries duplicated or conflicting ================'
	for i in $duplicates
	do
		echo "
--------------		$i		--------------"
		grep "^$i[. 	]" $@ | sed 's/:/:	/'
	done
	exit 99
fi
