#!/bin/bash
#-----------------------------------------------------------------------------
#
# \brief    create html and pdf documentation from markdown files.
#
#-----------------------------------------------------------------------------

set -e -u

cd ..

O=$PWD/documentation
mkdir -p $O
OUT=documentation/index.html
# FILTER=--filter doc/filter_panflute.py
FILTER=

echo "### processing README.md to html"
pandoc -c doc/pandoc.css -s README.md -o $OUT --metadata pagetitle="with_pandoc" \
    $FILTER \
    -M pathToProjectRoot=http://google.de/

mkdir -p $O/doc
cp doc/*.css $O/doc
cp doc/background.jpg $O/doc

for file in doc/*.md; do
   bare=$(basename $file)
   bare=${bare%.md}
   echo "### processing $file"
    pandoc -c pandoc.css -s doc/${bare}.md -o ${O}/doc/${bare}.html --metadata pagetitle="with_pandoc" \
      $FILTER \
      -M pathToProjectRoot=http://google.de/

    sed -i 's/.md">/.html">/g' $O/${bare}.html
    sed -i 's/.md#/.html#/g' $O/${bare}.html
done

sed -i 's/.md">/.html">/g' $OUT
sed -i 's/.md#/.html#/g' $OUT

echo "### processing README.md to pdf (pre-html)"
pandoc -c doc/pdf.css -s README.md -o ${O}/pdf.html --metadata pagetitle="with_pandoc" \
    -M pathToProjectRoot=http://google.de/

echo "### processing README.md to pdf"
wkhtmltopdf --enable-local-file-access ${O}/pdf.html ${O}/canio.pdf
rm -f ${O}/pdf.html

#---fin.----------------------------------------------------------------------
