#!/bin/sh
# vim: sw=4 ts=4 et
#---------------------------------------------------------------------------#
# Copyright (C) 1998-1999,2002, 2004-2006 The University of Melbourne.
# Copyright (C) 2014, 2020, 2023, 2025 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#

# Some versions of the info tool will emit single quotes as directional
# quotation marks (i.e. U+2018 and U+2019). This causes problems because awk
# on MSYS* / MinGW* / Cygwin* systems does not handle UTF-8 encoded input
# properly. (Apparently, it's some weird Windows code page thing.)
# Using the C locale makes info emit the apostrophe character for single quotes.
# Since that is ASCII, affected versions of awk do not have a problem with it.
# None of this is an issue on non-Windows systems since their versions of
# awk are not affected.
#
# However, see the next comment ...
#
# platform=$(uname -a)
# case "$platform" in
#     MINGW*|MSYS*|CYGWIN*) export LC_ALL=C ;;
# esac

# Regardless of which kinds of quotes awk can or cannot handle,
# our own util/info_to_mdb.c program understands *only* ASCII single quotes.
# The info program can now generate non-ASCII quotes in non-C locales
# even on Linux, so use the C locale on *all* systems.
export LC_ALL=C

# The info menu items that get us to the chapter on debugger commands.
cat mdb_categories > mdb_doc

tmp="mdb_doc_tmp.$$"
trap 'rm -f ${tmp}' 0 1 2 3 15

info -f ./mercury_user_guide.info -o ${tmp} \
    -n "Mercury debugger concepts"
../util/info_to_mdb concepts ${tmp} >> mdb_doc

# Document the declarative debugger.
info -f ./mercury_user_guide.info -o ${tmp} \
    -n "Declarative debugging overview"
echo "document concepts 10 decl_debug"                           >> mdb_doc
sed 's/^   //' < ${tmp} | sed -n '6,1000s/^/     /p'             >> mdb_doc 
echo                                                             >> mdb_doc
info -f ./mercury_user_guide.info -o ${tmp} \
    -n "Declarative debugging commands"
echo "The following commands are available from within the" \
    "declarative debugger:" "$(./commands ${tmp})" | \
    fold -w72 -s | sed "s/^/     /"                              >> mdb_doc
echo end                                                         >> mdb_doc
../util/info_to_mdb decl ${tmp} >> mdb_doc

debug_cmd_path="debug debugger"
for section in interactive forward backward browsing breakpoint \
    i/o parameter help declarative misc exp developer
do
    case ${section} in
        interactive)    category=queries ;;
        i/o)            category=io_tabling ;;
        declarative)    category=mdb_dd ;;
        *)              category=$section ;;
    esac
    info -f ./mercury_user_guide.info -o ${tmp} ${debug_cmd_path} ${section}
    ../util/info_to_mdb ${category} ${tmp} >> mdb_doc
done

mv mdb_doc ${tmp}
./squeeze < ${tmp} > mdb_doc
