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

. /prelude.bash.tmp

main() {

  declare    d
  declare    x
  declare    y

  #---------------------------------------------------------------------
  # Compile all Texinfo documents
  #---------------------------------------------------------------------

  for x in ./**/*.@(texi|texinfo|txi); do

    y=$(
      sed -n '
        /^@titlepage/ {
          p
          q
        }
      ' "$x"
    )
    if [[ ! "$y" ]]; then
      continue
    fi

    d=${x%/*}
    x=${x##*/}

    pushd "$d" >/dev/null

    y=${x/%.*/.html}

    texi2any \
      --html \
      --no-split \
      -o "$y" \
      "$x" \
    ;

    output "$y"

    popd >/dev/null

  done

  #---------------------------------------------------------------------
  # Compile all man pages
  #---------------------------------------------------------------------

  for x in ./**/*.1; do
    d=${x%/*}
    pushd "$d" >/dev/null
    x=./${x##*/}
    if [[ '' \
      || "$x" == ./._* \
      || "$x" == ./ChangeLog* \
      || "$x" == ./ansi2knr* \
    ]]; then
      popd >/dev/null
      continue
    fi
    y=$x.html
    groff -mandoc -T html "$x" >"$y.tmp1"
    sed '
      /^<!-- CreationDate:/ d
    ' <"$y.tmp1" >"$y.tmp2"
    mv -f "$y.tmp2" "$y"
    output "$y"
    popd >/dev/null
  done


  #---------------------------------------------------------------------

}; readonly -f main

main "$@"
