#!/bin/sh

# $Id: mancheck.sh 23 2011-08-20 20:29:26Z gjb $

THISDIR=`dirname $0`
CONFFILE="${THISDIR}/mancheck.conf"

updateonemanlist() {
	cd ${TMPDIR}
	# look for manual page updates - specifically '[A-Za-z.*].[1-9]$' -
	# and write them to a separate file
	egrep ".*\.[1-9]$" ${TREE}.update.log | awk '{print $2}' \
		> ${TMPDIR}/${TREE}.update.manlist.txt
}

updateallsrc() {
	for TREE in ${SRCDIRS}; do
		echo "Updating ${TREE}." >> ${LOGFILE}
		updateonesrc
	done
}

updateonesrc() {
	cd ${TLD}/${TREE}
	orev=`svn info | grep "^Revision: " | awk '{print $2}'`
	echo ${orev} > ${TMPDIR}/${TREE}.old.revision
	if [ ! ${DEBUG} ]; then
		svn update > ${TMPDIR}/${TREE}.update.log
	fi
	nrev=`svn info | grep "^Revision: " | awk '{print $2}'`
	if [ ${DEBUG} ]; then
		echo "U   share/man/man4/axe.4" > ${TMPDIR}/${TREE}.update.log
		# ugly hack so we don't accidentally skip the next real update
		nrev=`expr ${nrev} - 9999`
	fi
	echo ${nrev} > ${TMPDIR}/${TREE}.new.revision
	diff -q ${TMPDIR}/${TREE}.old.revision ${TMPDIR}/${TREE}.new.revision >/dev/null
	if [ $? -ne 0 ]; then
		echo -n "Revisions have changed for ${TREE}: " >> ${LOGFILE}
		echo "${orev} -> ${nrev}" >> ${LOGFILE}
		echo "Checking if manlint needs to run." >> ${LOGFILE}
		checkneedmanlint
		if [ $? -eq 1 ]; then
			echo "No manual page updates for ${TREE}." >> ${LOGFILE}
		else
			echo "Manual page changes found." >> ${LOGFILE}
			updateonemanlist
			checkmanlint
		fi
		checkcodespell
		sendemail
	else
		if [ ${WANTALLMAIL} ]; then
			echo "Revisions have not changed for ${TREE}." >> ${LOGFILE}
			echo "No need for manlint to run.  Exiting." >> ${LOGFILE}
			sendemail
		fi
	fi
}

checkneedmanlint() {
	# look for manual page updates - specifically '[A-Za-z.*].[1-9]$'
	egrep -q ".*\.[1-9]$" ${TMPDIR}/${TREE}.update.log
	return $?
}

checkmanlint() {
	echo "Running manlint on ${TREE}." >> ${LOGFILE}
	cd ${TLD}/${TREE}
	if [ -e ${TMPDIR}/${TREE}.manlint.log ]; then
		rm -f ${TMPDIR}/${TREE}.manlint.log
	fi
	for MAN in `cat ${TMPDIR}/${TREE}.update.manlist.txt`; do
		echo "=== ${MAN}" >> ${TMPDIR}/${TREE}.manlint.log
		groff -Tascii -mtty-char -man -t -ww -z ./${MAN} \
			>> ${TMPDIR}/${TREE}.manlint.log 2>&1
	done
}

checkcodespell() {
	echo "Running codespell on ${TREE}." >> ${LOGFILE}
	cd ${TLD}/${TREE}
	if [ -e ${TMPDIR}/${TREE}.codespell.log ]; then
		rm -f ${TMPDIR}/${TREE}.codespell.log
	fi
	for FILE in `cat ${TMPDIR}/${TREE}.update.log | awk '{print $2}'`; do
		echo "=== ${FILE}" >> ${TMPDIR}/${TREE}.codespell.log
		if [ -f ${FILE} ]; then
			$CSPELLCMD ./${FILE} >> \
				${TMPDIR}/${TREE}.codespell.log 2>&1
		fi
	done
}

sendemail() {
	SUBJECT="${TREE} manlint run"
	cat ${LOGFILE} > ${MAILLOG}
	echo "" >> ${MAILLOG}
	if [ -e ${TMPDIR}/${TREE}.manlint.log ]; then
		echo "# manlint results:" >> ${MAILLOG}
		count=`grep -v "^=== " ${TMPDIR}/${TREE}.manlint.log | wc -l | awk '{print $1}'`
		echo "mdoc(7) errors found in ${TREE}: ${count}" >> ${MAILLOG}
		egrep -B1 -v "(No such file or directory|${EXCDIRS})" \
			${TMPDIR}/${TREE}.manlint.log \
			>> ${MAILLOG}
	fi
	echo "" >> ${MAILLOG}
	if [ -e ${TMPDIR}/${TREE}.codespell.log ]; then
		echo "# codespell results:" >> ${MAILLOG}
		count=`grep -v "^=== " ${TMPDIR}/${TREE}.codespell.log | egrep -v "^(WARNING: |--)" | wc -l | awk '{print $1}'`
		echo "Spelling errors found in ${TREE}: ${count}" >> ${MAILLOG}
		egrep -B1 -v "(^WARNING: |${EXCDIRS}|^--|^=== revision)" \
			${TMPDIR}/${TREE}.codespell.log \
			>> ${MAILLOG}
	fi
	cat ${MAILLOG} | mail -s "${SUBJECT}" "${ADMIN}"
	# cleanup
	#rm -f ${MAILLOG} ${LOGFILE}
	#rm -f ${TMPDIR}/${TREE}.manlint.log
	#rm -f ${TMPDIR}/${TREE}.codespell.log
}

missingconffile() {
	echo "Error: ${CONFFILE} does not exist."
	exit 1
}

# START

# Pull in the configuration
if [ -f ${CONFFILE} ]; then
	. ${CONFFILE}
else
	missingconffile
fi

# remove stale files before running
if [ -e ${LOGFILE} ]; then
	rm -f ${LOGFILE}
fi
if [ -e ${MAILLOG} ]; then
	rm -f ${MAILLOG}
fi

if [ ${TERM} ]; then
	# if run interactively, allow passing which tree to check
	if [ "$#" -lt 1 ]; then
		# if no arguments, check HEAD only
		TREE=head && updateonesrc
	else
		# if there are arguments, check the specified tree
		TREE=$1 && updateonesrc
	fi
else
	# if no arguments are passed, check HEAD; otherwise, check
	# each tree in the argument list.
	if [ "$#" -lt 1 ]; then
		TREE=head && updateonesrc
	else
		for tree in $*; do
			TREE=${tree} && updateonesrc
		done
	fi
fi

exit 0

