#!/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 <http://www.gnu.org/licenses/>.

#set -e

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

#*** include section (source functions, for example) *******************

#*** file scope functions **********************************************

one_test() {
    "$@" >/dev/null 2>doctest.err
    if test -s doctest.err; then
        echo "ERROR messages follow:" 2>&1
        cat doctest.err 2>&1
        echo "ERROR while running following command:" 2>&1
        echo "$@" 2>&1
        echo "ERROR messages are preserved in doctest.err"
        exit 1
    fi
}

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

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

# Test the documentation for possible errors.
for i in $(find "${MC_SOURCE_ROOT_DIR}/doc" -name '*.[1-9].in'); do
    echo "test (groff): $i"

    preconv -e UTF8 "${i}" | \
        groff -wall -mandoc -Tutf8 | \
        grep "warning:"
done

for i in $(find "${MC_SOURCE_ROOT_DIR}/doc" -name '*.[1-9].in'); do
    echo "test (nroff): $i"

    preconv -e UTF8 "${i}" | \
        nroff -Tutf8 -mandoc | \
        grep "warning:"
done

# Check the English manuals to be in ASCII.
one_test find "${MC_SOURCE_ROOT_DIR}/doc" -maxdepth 1 -name '*.[1-9].in' -exec groff -wall -Tascii {} \;

rm -rf doctest.err
exit 0
