#!/usr/bin/env bash
set -e;

MYSELF=$(realpath "$0");
MYDIR=$(dirname "$MYSELF");

PRE_IMPORT=${PRE_IMPORT:-/components/md-pre.html}
POST_IMPORT=${POST_IMPORT:-/components/md-post.html}

find "${1-$MYDIR}" -iname '*.md' | while read -r line; do
    # target=${line//\.md/.html}
    target=$(sed 's/\.md$/\.html/' <<< "$line")

    echo "$line -> $target";
    echo "<!--# include virtual=\"$PRE_IMPORT\" -->" > "$target"
    pandoc --mathml "$line" -o - >> "$target"
    echo "<!--# include virtual=\"$POST_IMPORT\" -->" >> "$target"
done