#! /bin/sh
#
#	$NetBSD: cknames,v 1.2 1998/01/09 08:04:57 perry Exp $
#
set x $*
. ./.param

trap "rm -f $TMPDIR/*$$; exit;" 2

for sec in $DIRLST
do
	/bin/ls $MANDIR/man$sec > $TMPDIR/man${sec}$$
done

for sec in $DIRLST
do
	cd $MANDIR/man$sec
	dt=`date`
	echo "$dt\n" > $TMPDIR/not.one.line$sec
	echo "$dt\n" > $TMPDIR/name.order$sec
	echo "$dt\n" > $TMPDIR/file.match$sec
	echo "$dt\n" > $TMPDIR/no.TH$sec
	echo "$dt\n" > $TMPDIR/no.NAME$sec
	while read file
	do
		so=`line < $file | sed "s/ .*$//"`
		if test "$so" = ".so"
		then
			continue
		fi
		grep "^.SH NAME" $file > /dev/null
		if test $? -eq 1
		then
			echo $file >> $TMPDIR/no.NAME$sec
			continue
		fi
		grep "^.SH SYNOPSIS" $file > /dev/null
		if test $? -eq 0
		then
			lines=`cat $file | sed -n "/\.SH NAME/,/\.SH SYNOP/p" | wc -l | sed "s/ //g"`
		else
			lines=`cat $file | sed -n "/\.SH NAME/,/\.SH DESCR/p" | wc -l | sed "s/ //g"`
		fi
		if test $lines -gt 3
		then
			echo $file >> $TMPDIR/not.one.line$sec
			continue
		fi
		th=`grep "^.TH" $file | line`
		if test -z "$th"
		then
			echo $file >> $TMPDIR/no.TH$sec
			continue
		fi
		set $th
		name=`echo $2 | tr [A-Z] [a-z]`
		suf=`echo $3 | tr [A-Z] [a-z]`
		fname="${name}.$suf"
		if test $fname != $file
		then
			echo $file >> $TMPDIR/file.match$sec
		fi
		ed - $file > $TMPDIR/tmp.$$ <<!
			/^.SH NAME/+p
			q
!
		nname=`cat $TMPDIR/tmp.$$ | sed -e "s/ .*//" -e "s/,.*//"`
		rm -f $TMPDIR/tmp.$$
		if test $name != $nname
		then
			echo $file >> $TMPDIR/name.order$sec
		fi
	done < $TMPDIR/man${sec}$$
done

rm -f $TMPDIR/*$$
