#!/bin/sh

TRYIT_SERVER="https://faust.grame.fr/manual/"

# Generating html version of markdown
PHTML=$(pandoc --standalone --toc --toc-depth=4 --metadata pagetitle="Faust Manual" --mathjax src/introduction.md src/quick-start.md src/overview.md src/compiling.md src/syntax.md src/compiler.md src/embedding.md src/osc.md src/http.md src/midi.md src/architectures.md src/mathdoc.md src/acknowledgments.md src/tuto-juce.md src/tuto-new-archs.md)

# Detecting line of end of header added by pandoc
LINECUT=$(echo "$PHTML" | awk '/<ul>/{ print NR; exit }')

# Getting rid of pandoc header
#! PHTML=$(echo "$PHTML" | tail -n +$LINECUT | head -n -2)
PHTML=$(echo "$PHTML" | tail -n +$LINECUT | awk 'NR > 2 { print }')

# Adding elements for CSS, etc.
PHTML=$(echo "<div class=\"container-fluid\"><div class=\"row faust-doc\"><nav id=\"TOC\" class=\"col-3 faust-doc-content\"><div style=\"height: 100%;overflow-y: scroll;\"><ul class=\"nav flex-column\">$PHTML")
PHTML=$(echo "$PHTML" | awk '/<h1 id=\"introduction\">/{print "<main role=\"main\" class=\"col-9 ml-sm-auto px-4 faust-doc-content\">\n<div data-spy=\"scroll\" data-target=\"#TOC\" data-offset=\"0\" style=\"height: 100%;overflow-y: scroll;\">"}1' | awk '/<\/nav>/{print "</div>"}1')

# Extracting Faust files to be sent to the editor and generating correspind diags
EXFAUSTDIR="img/src/exfaust"
FAUSTCODES=$(echo "$PHTML" | awk '/<!-- faust-run -->/,/<!-- \/faust-run -->/')
CNT=0
while [ ! -z "$FAUSTCODES" ]
do
  CURRENT_FAUST_FILE="$EXFAUSTDIR$CNT/exfaust$CNT.dsp"
  # Removing existing dir, in case...
  rm -rf $EXFAUSTDIR$CNT 
  # Creating dir for code and SVG
  mkdir $EXFAUSTDIR$CNT
  # Formating code from file
  # TODO: files could be trimed to get rid of empty line at beginning and end
  echo "$FAUSTCODES" | awk '1;/<!-- \/faust-run -->/{exit}' | awk '{gsub(/<!-- faust-run -->/,"")}1' | awk '{gsub(/<!-- \/faust-run -->/,"")}1' | awk '{gsub(/<pre><code>/,"")}1' | awk '{gsub(/<\/code><\/pre>/,"")}1' | awk '{gsub(/&quot;/,"\"")}1' | awk '{gsub(/&lt;/,"<")}1' | awk '{gsub(/&gt;/,">")}1' | awk '{gsub(/&#39;/,"\x27")}1' | awk '{gsub(/&amp;/,"\x26")}1' > $CURRENT_FAUST_FILE
  # Generating SVG
  faust2svg $CURRENT_FAUST_FILE
  # Cleaning current example
  CODE_LENGTH=$(cat $CURRENT_FAUST_FILE | wc -l)
  FAUSTCODES=$(echo "$FAUSTCODES" | tail -n +$((CODE_LENGTH+1)))
  # Formating Faust runnable examples in html
  FAUST_HEADER="<div class=\"faust-run\"><a href=\"$EXFAUSTDIR$CNT/exfaust$CNT-svg/process.svg\" target=\"_blank\"><img src=\"$EXFAUSTDIR$CNT/exfaust$CNT-svg/process.svg\" class=\"mx-auto d-block\"></a>"
  PHTML=$(echo "$PHTML" | awk -v var="$FAUST_HEADER" '/<!-- faust-run -->/ && n==0 {sub(/<!-- faust-run -->/,var);++n}{print}')
  FAUST_FOOTER="<a href=\"https://faust.grame.fr/editor?code=$TRYIT_SERVER$CURRENT_FAUST_FILE\"><button type=\"button\" class=\"btn btn-primary\">Try it Yourself >></button></a></div>"
  PHTML=$(echo "$PHTML" | awk -v var="$FAUST_FOOTER" '/<!-- \/faust-run -->/ && n==0 {sub(/<!-- \/faust-run -->/,var);++n}{print}')
  # Going to next code...
  ((CNT++))
done

# Add the header and the footer of the future Faust website
#echo "$PHTML</div></div></div></main>" | cat lib/header-faust.html - lib/footer.html > index.html
# Add the header and the footer to make a standalone HTML page
echo "$PHTML</div></div></div></main>" | cat lib/header-doc.html - lib/footer.html > index.html
