#!/bin/sh
. ./01_SetVariables.sh
rm -f "$ROOTDIR"/doc/*html
cd "$WIP"/HandBook/ || exit 1
for handbookpo in *.HandBook.po; do
	ll_TT="${handbookpo%.*.*}"
	echo "generating $ll_TT.adoc..." 
	rm -f "$ll_TT".adoc
	po4a-translate -M UTF-8 -m "$WIP"/sources/HandBook/HandBook.adoc -f asciidoc -p "$handbookpo" -l "${handbookpo%po}"adoc
	handbook="${handbookpo%po}"adoc
	rm -f "${ll_TT}".adoc
done
rm -f en_US.adoc
cp "$WIP"/sources/HandBook/HandBook.adoc en_US.HandBook.adoc
for handbook in *.HandBook.adoc; do
	ll_TT="${handbook%.*.*}"
	echo "$ll_TT ..."
	sleep 1
	{
	echo \
":toc: left
:toclevels: 1
:toc-title: 
" >> "${ll_TT}".adoc
	# Chapter Overview of Slint installation
	sed -n '\@// Overview@,\@// Usage@p' "$handbook"|head -n -1
	# Chapter Usage of the installer
	sed -n "\@// Usage_installer@,\@// Encryption@p" "$handbook"|head -n -1
	# Chapter Prepare partitions for Slnt
	sed -n '\@// Prepare@,\@// Installation@p' "$handbook"
	# Chapter Encryption
	sed -n "\@// Encryption@,\@// Speakup@p" "$handbook"|head -n -1
	# Chapter Speakup
	sed -n "\@// Speakup@,\@// First_steps@p" "$handbook"| head -n -1
	# Chapter first steps
	sed -n "\@// First_steps@,\@// Usage@p" "$handbook"|head -n -1
	sed -n '\@// Glossary@,$p' "$handbook"
	} >> "${ll_TT}".adoc
	# Chapter Glossary.
	# Normalize the headers' levels not the same in the installer doc as in the HandBook
	sed 's/^== /==== /' "${ll_TT}".adoc > bof && mv bof "${ll_TT}".adoc
	sed 's/^===== /=== /' "${ll_TT}".adoc > bof && mv bof "${ll_TT}".adoc
	sed 's/^==== /== /' "${ll_TT}".adoc > bof && mv bof "${ll_TT}".adoc
	asciidoctor "${ll_TT}".adoc
	mv "${ll_TT}".html "$ROOTDIR"/doc || exit
done
cd "$CWD" || exit 1
