#!/bin/sh

# Copyright © 2015 Jakub Wilk <jwilk@jwilk.net>
#
# This file is part of pdf2djvu.
#
# pdf2djvu is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# pdf2djvu 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.

color()
{
    sed -e 's/^/  /' \
    | grep --color=auto '.*'
}

color_msgid()
{
    sed -n -e '/^msgid/ { s/^/  /; p }' \
    | grep --color=auto ' "[^"].*'
}

set -e -u
rc=0
for po in "$@"
do
    ok=yes
    m=$(msgfmt --check -o /dev/null "$po" 2>&1 || true)
    if [ -n "$m" ]
    then
        [ -z "$ok" ] || printf '%s:\n' "$po"
        printf '%s:\n' '- errors from msgfmt --check'
        printf '%s' "$m" | color
        ok=
    fi
    if command -v i18nspector > /dev/null
    then
        m=$(i18nspector "$po")
        if [ -n "$m" ]
        then
            [ -z "$ok" ] || printf '%s:\n' "$po"
            printf '%s:\n' '- warnings from i18nspector'
            printf '%s' "$m" | color
            ok=
        fi
    fi
    m=$(msgattrib --no-wrap --only-fuzzy "$po")
    if [ -n "$m" ]
    then
        [ -z "$ok" ] || printf '%s:\n' "$po"
        printf '%s:\n' '- fuzzy messages'
        printf '%s' "$m" | color_msgid
        ok=
    fi
    m=$(msgattrib --no-wrap --untranslated "$po")
    if [ -n "$m" ]
    then
        [ -z "$ok" ] || printf '%s:\n' "$po"
        printf '%s:\n' '- untranslated messages'
        printf '%s' "$m" | color_msgid
        ok=
    fi
    if [ -z "$ok" ]
    then
        printf '\n'
        rc=1
    fi
done
exit $rc

# vim:ts=4 sts=4 sw=4 et
