#!/bin/bash
# processing command line arguments
# more than a wrapper for pandoc
# create_html.sh [RESEARCH_BUILD] [RESEARCH_TOPICS]
# [RESEARCH_BUILD] Where the htmls are created
# [RESEARCH_TOPICS] where the .mds for translation are located
# Define variables
MATHJAX_URL='https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'

# Check commandline arguments
if [ $# -lt 1 ]
then
    RESEARCH_BUILD=`pwd`"/build"
    RESEARCH_TOPICS=`pwd`
    echo -e "No building directory was specified."
    BUILD_SUFFIX="_research"
elif [ $# -lt 2 ] 
then
    RESEARCH_BUILD="$1"
    RESEARCH_TOPICS=$RESEARCH_BUILD
    BUILD_SUFFIX="_research"
    echo -e "No extra topics directory was specified."
elif [ $# -lt 3 ] 
then
    RESEARCH_BUILD="$1"
    RESEARCH_TOPICS="$2"
    BUILD_SUFFIX="_research"
else
    RESEARCH_BUILD="$1"
    RESEARCH_TOPICS="$2"
    BUILD_SUFFIX=""
fi
mkdir -p $RESEARCH_BUILD
echo -e "Building into directory $RESEARCH_BUILD ..."
echo -e "Using .md-files from $RESEARCH_TOPICS ..."

# specify URL to avoid use of local library
# see https://docs.mathjax.org/en/latest/web/start.html#ways-of-accessing-mathjax
# and https://www.jsdelivr.com/package/npm/mathjax

# create plain _research.html to be integrated into other files
for f in $RESEARCH_TOPICS/*.md
do
    filename=$(basename -- "$f")
    echo "Creating " $RESEARCH_BUILD/"${filename%.md}"$BUILD_SUFFIX.html " ..."
    pandoc  $f -o  $RESEARCH_BUILD/"${filename%.md}"$BUILD_SUFFIX.html \
    --mathjax=$MATHJAX_URL \
    -c css/base.css \
    -c css/extra.css \
    -c "css/print.css" \
    -c "css/superfish.css" \
    -c "css/superfish-vertical.css" \
    -c "css/base_mode.css"
done

