#!/bin/bash

BUILD=${1:-_build}

set -eo pipefail

WARNINGS="${BUILD}/gtk-doc-warnings.log"
ERRORS="${BUILD}/doc-errors.log"

rm -f "${BUILD}/src/Phosh-0.gir"
meson compile -C "${BUILD}" |& grep -i -E 'warning|error': > "${WARNINGS}"

if [ ! -s "${WARNINGS}" ]; then
    exit 0
fi

# We ignore errors from generated DBus bindings, contrib code and wayland protocols
if ! grep -v -E '(^protocol/)'\
'|(.*/src/gtk-list-models/.*Warning:)'\
'|(.*/src/contrib/.*Warning: Phosh: .*Unknown namespace for)'\
'|(<unknown>:: Warning: Phosh: Unknown namespace for identifier )'\
 "${WARNINGS}" > "${ERRORS}"; then
   echo "No critical errors found."
else
   echo "Documentation errors:"
   cat "${ERRORS}"
   exit 1
fi

exit 0
