#!/bin/bash

# Midnight Commander - check the documentation for compatibility with groff and nroff.
#
# Copyright (C) 2002, 2003, 2011, 2013
# The Free Software Foundation, Inc.
#
# Written by:
#  Pavel Roskin <proski@gnu.org> 2002, 2003
#  Ilia Maslakov <il.smind@gmail.com>, 2011
#  Slava Zanko <slavazanko@gmail.com>, 2013
#
# This file is part of the Midnight Commander.
#
# The Midnight Commander is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# The Midnight Commander is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

set +e

MC_SOURCE_ROOT_DIR=${MC_SOURCE_ROOT_DIR:-$(dirname "$(pwd)")}

#*** main code *********************************************************

[ -r "${MC_SOURCE_ROOT_DIR}/doc/man/mc.1.in" ] || {
    echo "ERROR: cannot read doc/man/mc.1.in" 2>&1
    exit 1
}

for cmd in groff nroff ; do
    IFS='' ROFF_WARNINGS="$ROFF_WARNINGS
$(
    find "${MC_SOURCE_ROOT_DIR}/doc" -name '*.[1-9].in' \
        -exec sh -c "$cmd -K UTF-8 -Tutf8 -mandoc -ww '{}' | grep 'warning:'" \; 2>&1
)"
done

# Check that English manuals are in ASCII
ASCII_WARNINGS=$(find "${MC_SOURCE_ROOT_DIR}/doc" -maxdepth 1 -name '*.[1-9].in' -exec groff -Tascii -ww {} \; 2>&1)

if [ "x`printf '%s' "${ROFF_WARNINGS}${ASCII_WARNINGS}" | tr -d "$IFS"`" != x ] ; then
    echo "$ROFF_WARNINGS" | sort -u | grep .
    echo "$ASCII_WARNINGS" | sort -u | grep .
    exit 1
fi
