#!/bin/sh
#
#	Chncheck - syntax check UK-Sendmail channel tables for obvious errors.
#	Returns the number of errors found.
#
#	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 }
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

#janet chan tends to blow this up because top domain is at front - adjust
#the 'ignore' list to suit your janet.chn; if you have no janet chan then
#delete the ignore list altogether. If you prefer, just exit here:
#exit

# delete everything after the first whitespace
duplicates=`sed -e '/^#/d' -e '/^$/d' -e 's/[ 	].*//' $@ | sort | uniq -d`

exitcode=0
if [ -n "$duplicates" ]
then	for i in $duplicates
	do	case $i in
		uk|com|edu|us|ie|irl|nz|au|gov|uucp|arpa|dk|se|no|earn)
			;;
		*)	echo "
---------	$i : duplicate or conflict	---------"
			egrep "^$i\$|^$i[ 	]" $@ | sed -e 's/:/:	/'
			echo
			exitcode=99
			;;
		esac
	done
fi
exit $exitcode
