#
# This file is licensed under the MIT No Attribution license.
#

. /prelude.bash.tmp

# Use each release's own configuration substitutions and manual lists.
# Only the formatter is shared; the documentation comes from this release.
export TZ=UTC
SOURCE_DATE_EPOCH=$(cat source-date-epoch.tmp)
export SOURCE_DATE_EPOCH

make_subdir() {
  declare directory=$1
  shift
  make -C "$directory" \
    -f "$PWD/Makefile.comm" -f "$PWD/Makefile.cfg" \
    -f Makefile.sub -f "$PWD/Makefile.man" \
    srcdir=. top_srcdir="$PWD" top_builddir="$PWD" "$@"
}

mkdir -p /manuals.tmp
# A few release archives accidentally contain their publisher's cache.
rm -f config.cache
if [[ -f Makefile.am ]]; then
  CFLAGS='-g -O2 -std=gnu17' ./configure --without-x
  cat > archive.mk <<'MAKE'
include Makefile
archive_manuals: $(man1_MANS) $(man5_MANS) $(man7_MANS) src/devices/xditview/gxditview.1 src/utils/xtotroff/xtotroff.1
	for p in $^; do cp "$$p" /manuals.tmp/ || exit; done
MAKE
  # New releases expand .so includes while preparing man pages.
  # Use the installed formatter's soelim, as when cross compiling.
  make -f archive.mk -o soelim SOELIMBIN=soelim archive_manuals
elif [[ -f Makefile.comm ]]; then
  CC='gcc -std=gnu89' CCC='g++ -std=gnu++98' CXX='g++ -std=gnu++98' ./configure
  # Makefile.comm is read before the per-directory MAN1/MAN5/MAN7 lists.
  cat >> Makefile.comm <<'MAKE'

.SECONDEXPANSION:
archive_manuals: $$(MANPAGES)
	for p in $(MAN1); do cp "$$p" /manuals.tmp/$(NAMEPREFIX)$${p%.n}.1 || exit; done
	for p in $(MAN5); do cp "$$p" /manuals.tmp/$${p%.n}.5 || exit; done
	for p in $(MAN7); do cp "$$p" /manuals.tmp/$${p%.n}.7 || exit; done
MAKE
  make Makefile.cfg
  if [[ -f contrib/groffer/Makefile.sub ]] && grep -q '^ROFF2MODES=' contrib/groffer/Makefile.sub; then
    make do=all arch/misc
    make_subdir contrib/groffer groffer
    # The generated templates would otherwise acquire today's man date.
    touch -d "@$SOURCE_DATE_EPOCH" contrib/groffer/groffer.man contrib/groffer/roff2*.man
    # These releases also support a shell implementation with its own
    # manual; the default installation chooses the Perl implementation.
    if [[ -f contrib/groffer/shell/groffer.man ]]; then
      cp -p contrib/groffer/shell/groffer.man contrib/groffer/groffer-shell.man
      make_subdir contrib/groffer groffer-shell.n
      cp contrib/groffer/groffer-shell.n /manuals.tmp/groffer-shell.1
    fi
  fi
  dirs=()
  for f in **/Makefile.sub; do
    if grep -q '^MAN[157][ =]' "$f"; then dirs+=("${f%/*}"); fi
  done
  make do=archive_manuals "${dirs[@]}"
  # Configure omits these recursion targets when X11 is unavailable.
  for directory in src/devices/xditview src/utils/xtotroff; do
    if [[ -f $directory/Makefile.sub ]]; then
      make_subdir "$directory" archive_manuals
    fi
  done
else
  make man
  cat > man/archive.mk <<'MAKE'
include Makefile
.SECONDEXPANSION:
archive_manuals: $$(MANPAGES)
	for p in $(MAN1PAGES); do cp "$$p" /manuals.tmp/$${p%.n}.1 || exit; done
	for p in $(MAN5PAGES); do cp "$$p" /manuals.tmp/$${p%.n}.5 || exit; done
	for p in $(MAN7PAGES); do cp "$$p" /manuals.tmp/$${p%.n}.7 || exit; done
MAKE
  make -C man -f archive.mk archive_manuals
  if [[ -f mm/Makefile ]]; then
    make -C mm all
    cp mm/groff_mm.n /manuals.tmp/groff_mm.7
    cp mm/groff_mmse.n /manuals.tmp/groff_mmse.7
  fi
fi
# Before X11's build joined the main makefiles, this page used Imake.
for x in xditview/gxditview.man src/xditview/gxditview.man; do
  if [[ -f $x ]]; then
    cp "$x" /manuals.tmp/gxditview.1
  fi
done
manual_count=$(find /manuals.tmp -type f | wc -l)
printf 'PREPARED %s manuals\n' "$manual_count"
