#!/bin/sh

# This script is a pipe that translates configure output to ReST markup
# suitable for inclusion in the documentation.
#
# Note: Don't pipe configure into it, because that can alter the build in the
# middle of it. E.g., I've had part of "make install" in --prefix and part in
# the default /usr/local. Use config.log instead.
#
# I have very mixed feelings about this script. On one hand, we've defined a
# new markup language. On the other hand, I feel that writing down the
# dependencies really does need to be DRY, checking the run-time dependencies
# too has a lot of value, translating from prose docs to a configure script
# would be nearly impossible, and ReST is way too ugly to use as-is in
# terminal output.
#
# Steps:
#
#   1. Remove everything before "Building Charliecloud" (inclusive) to the
#      next log section (exclusive).
#   2. Remove "will build and install" paragraph.
#   3. Remove any "Warning:" paragraphs.
#   4. Remove results of tests: " ..." to EOL, ": yes", ": no".
#   5. Convert indentation to bullet lists.
#   6. Convert "foo(1)" to ":code:`foo`".

# shellcheck disable=SC2016
  awk '/^Building Charliecloud/,/^##/' | head -n-2 \
| awk -v RS='' '{gsub(/^  will build.*/, ""); print; print ""}' \
| awk -v RS='' '{gsub(/^ +Warning:.*/, ""); print; print ""}' \
| sed -r -e 's/ \.\.\..*$//' -e 's/ (yes|no)$//' \
         -e 's/^  //' -e 's/(^(  )+)/\1* /' -e 's/:$/:\n/' \
         -e 's/([a-zA-Z0-9-]+)\(1\)/:code:`\1`/g'
