#!/bin/bash

orig="${PWD}"

if ! command -v asciidoctor &> /dev/null;
then
        exit 0
fi

set -e

for f in $(find . -type f -iname "README.adoc"); do
        filename=$(basename -- "${f}")
	docsdir=$(dirname -- "${f}")
        nosuffix="${filename%.*}"
	pfx="${docsdir}/${nosuffix}"

	# Render HTML, include in commit
	newf="${pfx}.html"
        asciidoctor -a ROOTDIR="${orig}/" -o "${newf}" "${f}"
	echo "Generated ${newf} from ${f}"
        git add "${newf}"

	# If asciidoctor-pdf is installed, render as PDF for local use
	# (Does not get added to commit, and *.pdf is in .gitignore for a reason)
        if command -v asciidoctor-pdf &> /dev/null;
	then
		newf="${pfx}.pdf"

        	asciidoctor-pdf -a ROOTDIR="${orig}/" -o "${newf}" "${f}"
	fi

	# If pandoc is installed, render to "GitHub-flavored Markdown" for better rendering on forks/mirrors
	# and marginally better rendering on https://pkg.go.dev/ and add to commit.
	#
	# <rant>
	# There is no such thing as "Markdown".
	# The closest thing you have to any sort of standard is https://daringfireball.net/projects/markdown/
	# but everybody and their mother adds their own "extensions"/"flavor", and sometimes even
	# change how formatting works compared to the Daring Fireball/John Gruber spec (the original creator of the "syntax").
	# Ergo "Markdown" inherently has no meaning.
	# It's one of the worst formatting languages out there - just because it's popular doesn't mean it's good.
	#
	# If you're writing docs, you should stick to one of these which have defined, canonical, standardized
	# syntax:
	# 	* AsciiDoc/AsciiDoctor
	# 		* Supports much more extensive formatting than any Markdown flavor I've seen
	# 		* Source/raw/unrendered still *quite* readable by human eyes
	# 		* Somewhat limited parsers/renderers
	# 		* https://asciidoc.org/
	# 		* https://asciidoctor.org/
	# 	* DocBook
	# 		* Supports even more extensive and flexible but exact formatting
	# 		* Great for publishing, though - especially if you need control over formatting/layout
	# 		* XML-based
	# 		* Harder to read in plaintext, but fairly doable (XML lends to decent mental rendering)
	# 		* Very wide support for parsing/rendering
	# 		* https://docbook.org/
	#	* LaTex
	#		* Allows for *very* extensive domain-specific ligature/representation (very common in mathematic/scientific literature)
	#		* But nigh unreadable by human eyes unless you've rather familiar with it
	#		* Parsing/rendering support about on-par with DocBook
	#		* https://www.latex-project.org/
	# </rant>
        if command -v pandoc &> /dev/null;
        then
		newf="${pfx}.md"

		set +e
                #asciidoctor -a ROOTDIR="${orig}/" -b docbook -o - "${f}" | pandoc -f docbook -t markdown_strict -o "${newf}"
                asciidoctor -a ROOTDIR="${orig}/" -b html -o - "${f}" | pandoc -f html -t gfm -o "${newf}"
		if [ $? -eq 0 ];
		then
			echo "Generated ${newf} from ${f}"
	                git add "${newf}"
		else
			echo "Failed to generate ${newf} from ${f}"
			git rm "${newf}" 2>/dev/null
		fi
		set -e
        fi
	cd ${orig}
done
echo "Regenerated docs"
