#!/bin/bash

BUILD=${1:-_build}

set -eo pipefail

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

ninja -C "${BUILD}" phosh-doc |& tee | grep warning: > "${WARNINGS}"

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

# We ignore errors from generated DBus bindings, contrib code and wayland protocols
if ! grep -v -E '(^html/api-index-full.html)|(^./phosh-decl.txt)'\
'|(^\./phosh-sections.txt:[0-9]+: warning: Double)'\
'|Parameter description for shell_network_agent_search_vpn_plugin_finish'\
'|client-protocol.h'\
'|(no link for: "gdbus)|(/src/.*dbus.c)'\
'|(descriptions for (struct )?zwlr_)|(descriptions for (struct )?z?xdg_)'\
'|gamma_control_error|gamma_control_listener|org_kde_kwin_idle_timeout_listener'\
'|phosh_private_error|phosh_private_keyboard_event_error'\
'|phosh_private_keyboard_event_listener|phosh_private_xdg_switcher_error'\
'|phosh_private_startup_'\
'|phosh_private_shell_state'\
'|phosh_private_xdg_switcher_listener'\
'|zwp_virtual_keyboard_manager_v1_error|zwp_virtual_keyboard_v1_error'\
'|zwp_input_method_v2_listener|zwp_input_popup_surface_v2_listener|zwp_input_method_keyboard_grab_v2_listener'\
'|no link for: "NM(Connection|Client)"'\
 "${WARNINGS}" > "${ERRORS}"; then
   echo "No critical errors found."
else
   echo "Documentation errors:"
   cat "${ERRORS}"
   exit 1
fi

exit 0
