#! /bin/bash
#
# generateDocs - creates HTML and plaintext documentation from
# reStructuredText format files. -Geo
#
#
# Copyright (C) 2004 - 2025 Eggheads Development Team
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.

show_usage() {
        echo "Usage: `basename $0` [-v version]"
        echo ""
        echo "  -h, --help - Print this help and exit."
        echo "  -v, --ver  - Update document version string."
        exit 1
}

do_ver=0
if test "x${1}" = "x-v" || test "x${1}" = "x--ver"; then
        do_ver=1
        if test "x${2}" = "x"; then
                show_usage
        fi
        ver=$2
fi


BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source .venv/bin/activate

echo "*** Searching for pandoc..."
PANDOC="$(which pandoc)"
if [ $? -eq 0 ]; then 
  echo "Found."
else
  echo "Not found. Pandoc can be found at pandoc.org or via apt-get install pandoc. Exiting..."
  exit 1
fi

echo "*** Searching for panflute..."
PANDOC="$(pip3 show panflute)"
if [ $? -eq 0 ]; then
  echo "Found."
else
  echo "Not found. Panflute can be installed via apt-get install python3-panflute. Exiting..."
  exit 1
fi


echo "*** Searching for sphinx-build..."
SPHINX="$(which sphinx-build)"
if [ $? -eq 0 ]; then
  echo "Found."
else
  echo "Not found. Sphinx can be found at https://www.sphinx-doc.org or via apt-get install sphinx-common. Exiting..."
  exit 1
fi


if [ $do_ver -eq 1 ]; then
  echo "*** Updating docs to version ${ver}..."
  sed -e "s:version = '.*':version = '${ver}':" \
      -e "s:release = '.*':release = '${ver}'": doc/sphinx_source/conf.py > doc/sphinx_source/conf.py_
  if grep "version = '${ver}'" doc/sphinx_source/conf.py_ && grep "release = '${ver}'" doc/sphinx_source/conf.py_; then
    mv doc/sphinx_source/conf.py_ doc/sphinx_source/conf.py
    echo "Updated."
  else
    echo "Documentation version update failed. Aborting..."
    exit 1
  fi
fi


mkdir -p tmpdocs

# The |version|, |majversion|, |fullversion| and |dlink| substitutions are
# defined in conf.py's rst_prolog, which only Sphinx reads. Give pandoc the
# same definitions (plain-text flavored) so the .txt docs don't end up with
# blanks where the version should be.
DOCVER=$(sed -n "s/^version = '\(.*\)'/\1/p" $BASEDIR/../doc/sphinx_source/conf.py)
MAJVER=$(echo "$DOCVER" | cut -d. -f1-2)
if [ -z "$DOCVER" ]; then
  echo "Could not read version from doc/sphinx_source/conf.py. Exiting..."
  exit 1
fi
cat << EOF > tmpdocs/prolog.rst
.. |version| replace:: ${DOCVER}
.. |release| replace:: ${DOCVER}
.. |fullversion| replace:: ${DOCVER}
.. |majversion| replace:: ${MAJVER}
.. |dlink| replace:: the current version of Eggdrop (https://ftp.eggheads.org/pub/eggdrop/source/${MAJVER}/eggdrop-${DOCVER}.tar.gz)
EOF

echo "*** Generating plaintext docs..."
for i in $( find $BASEDIR/../doc/sphinx_source/ -type f -name "*.rst" ); do
  FILENAME=$(basename "$i")
  FILENAME="${FILENAME%.*}"
  echo $FILENAME
  # The definitions go after the document: in front, they would stop pandoc
  # from promoting the first heading to the title and shift every heading
  # level. :substitution-code: is a Sphinx extension; pandoc leaves
  # |fullversion| inside it untouched, so replace that one by hand.
  pandoc -f rst -t plain --filter ./misc/docsfilter.py $i tmpdocs/prolog.rst | \
    sed "s/|fullversion|/${DOCVER}/g" > tmpdocs/$FILENAME.txt
done
rm -f tmpdocs/prolog.rst

echo "*** Generating HTML files..."
( cd $BASEDIR/../doc/sphinx_source/ && make -B html )

echo "*** Stupid housecleaning..."
mv tmpdocs/about.txt $BASEDIR/../doc/ABOUT
mv tmpdocs/legal.txt $BASEDIR/../doc/LEGAL
mv tmpdocs/install.txt $BASEDIR/../INSTALL
mv tmpdocs/readme.txt $BASEDIR/../README
mv tmpdocs/upgrading.txt $BASEDIR/../UPGRADING
mv tmpdocs/included.txt $BASEDIR/../doc/modules/MODULES
mv tmpdocs/writing.txt $BASEDIR/../doc/modules/writing-modules
mv tmpdocs/assoc.txt $BASEDIR/../doc/modules/mod.assoc
mv tmpdocs/blowfish.txt $BASEDIR/../doc/modules/mod.blowfish
mv tmpdocs/channels.txt $BASEDIR/../doc/modules/mod.channels
mv tmpdocs/compress.txt $BASEDIR/../doc/modules/mod.compress
mv tmpdocs/console.txt $BASEDIR/../doc/modules/mod.console
mv tmpdocs/ctcp.txt $BASEDIR/../doc/modules/mod.ctcp
mv tmpdocs/dns.txt $BASEDIR/../doc/modules/mod.dns
mv tmpdocs/filesys.txt $BASEDIR/../doc/modules/mod.filesys
mv tmpdocs/ident.txt $BASEDIR/../doc/modules/mod.ident
mv tmpdocs/irc.txt $BASEDIR/../doc/modules/mod.irc
mv tmpdocs/notes.txt $BASEDIR/../doc/modules/mod.notes
mv tmpdocs/pbkdf2.txt $BASEDIR/../doc/modules/mod.pbkdf2
mv tmpdocs/python.txt $BASEDIR/../doc/modules/mod.python
mv tmpdocs/seen.txt $BASEDIR/../doc/modules/mod.seen
mv tmpdocs/server.txt $BASEDIR/../doc/modules/mod.server
mv tmpdocs/share.txt $BASEDIR/../doc/modules/mod.share
mv tmpdocs/transfer.txt $BASEDIR/../doc/modules/mod.transfer
mv tmpdocs/twitch.txt $BASEDIR/../doc/modules/mod.twitch
mv tmpdocs/uptime.txt $BASEDIR/../doc/modules/mod.uptime
mv tmpdocs/woobie.txt $BASEDIR/../doc/modules/mod.woobie
mv tmpdocs/firstscript.txt $BASEDIR/../doc/FIRST-SCRIPT
mv tmpdocs/bans.txt $BASEDIR/../doc/BANS
mv tmpdocs/botnet.txt $BASEDIR/../doc/BOTNET
mv tmpdocs/core.txt $BASEDIR/../doc/core.settings
mv tmpdocs/features.txt $BASEDIR/../FEATURES
mv tmpdocs/ipv6.txt $BASEDIR/../doc/IPV6
mv tmpdocs/ircv3.txt $BASEDIR/../doc/IRCv3
mv tmpdocs/partyline.txt $BASEDIR/../doc/PARTYLINE
mv tmpdocs/patch.txt $BASEDIR/../doc/PATCH-HOWTO
mv tmpdocs/pbkdf2info.txt $BASEDIR/../doc/PBKDF2
mv tmpdocs/tcl-commands.txt $BASEDIR/../doc/tcl-commands.doc
mv tmpdocs/twitch-tcl-commands.txt $BASEDIR/../doc/twitch-tcl-commands.doc
mv tmpdocs/tls.txt $BASEDIR/../doc/TLS
mv tmpdocs/tricks.txt $BASEDIR/../doc/TRICKS
mv tmpdocs/twitchinfo.txt $BASEDIR/../doc/TWITCH
mv tmpdocs/users.txt $BASEDIR/../doc/USERS
mv tmpdocs/accounts.txt $BASEDIR/../doc/ACCOUNTS
mv tmpdocs/autoscripts.txt $BASEDIR/../doc/AUTOSCRIPTS
rm -rf tmpdocs
rm -rf $BASEDIR/../doc/html/_sources
rm -rf $BASEDIR/../doc/doctrees/
rm -rf $BASEDIR/../doc/html/.buildinfo


echo "Yahtzee!"
