#!/bin/sh
#
# man page lint checking, mainly for use in PCP CI builds.
# Usage: scripts/man-lint <file>...
#
# Note: silently exits if 'mandoc' utility is unavailable.
# Will also (optionally) use 'lexgrog' if it is installed,
# as a secondary static check.
#

which mandoc >/dev/null 2>&1
[ $? -eq 0 ] || exit 0	# not installed

sts=1
tmp=`mktemp -d /tmp/pcp.XXXXXXXXX` || exit 1
trap "rm -rf $tmp; exit \$sts" 0 1 2 3 15

mandoc -T lint "$@" \
    | grep -v ' UNSUPP: ' \
    | grep -v ' WARNING: missing date' \
    | grep -v ' WARNING: cannot parse date' \
> $tmp/mandoc 2>&1

if test -s $tmp/mandoc
then
    cat $tmp/mandoc	# failure, report issues
else
    which lexgrog >/dev/null 2>&1
    if test $? -eq 0
    then
	lexgrog "$@" > $tmp/lexgrog
	sts=$?
	[ $sts -ne 0 ] && cat $tmp/lexgrog	# failure, report issues
    else
        sts=0
    fi
fi

exit $sts
