#!/bin/sh
# Regenerate the roff manpages (basex*.1) from their txt2man sources (basex*.txt).
# Not run during the package build; run manually after editing a *.txt source and
# commit the regenerated *.1.  Override the .TH date with e.g. DATE="1 January 2026".
set -e

if [ ! -x /usr/bin/txt2man ]; then
  echo 'txt2man is used to produce manpages (please consider installing it).'
  echo '# apt install txt2man'
  exit 1
fi

DATE="${DATE:-$(date -u +'%d %B %Y')}"

for i in basex basexgui basexclient basexserver; do
  # Prepend a coding declaration so man-db's preconv treats the (UTF-8) page as
  # UTF-8 regardless of locale; otherwise non-ASCII (e.g. "Grün") is mangled.
  {
    printf '.\\" -*- coding: UTF-8 -*-\n'
    /usr/bin/txt2man -d "$DATE" -s1 -t"$i" -v"The XML Database" "./$i.txt"
  } > "./$i.1"
done
