#!/bin/sh

set -e

# enter the current directory
cd "$(dirname $0)"
export PANDOC_DIR=pandoc/cl


if [ -z $1 ]; then
  export target=pdf
else
  export target=$1
fi

if [ -z $2 ]; then
  export kind=user
else
  export kind=$2
fi

if [ $kind = 'user' ]; then
  export files="
    $kind/intro.md
    $kind/concept.md
    $kind/interface.md
    $kind/person.md
    $kind/person-vendee.md
    $kind/activite.md
    $kind/document.md
    $kind/thirdparty.md
    $kind/parcours.md
    $kind/menage.md
    $kind/notifications.md
    $kind/choisir_une_adresse.md
  "
elif [ $kind = 'admin' ]; then
  export files="
    $kind/generation-documents.md
  "
else
  echo "kind '$kind' is not valid";
  exit 1;
fi

export ARGS="
  --from markdown
  --metadata-file ./$kind/metadata.yaml
  --lua-filter "${PANDOC_DIR}/format-link.lua"
"
export PDF_TEMPLATE="./pandoc/template/eisvogel.tex"
export LATEX_ARGS="
  --template "${PDF_TEMPLATE}"
  --lua-filter "${PANDOC_DIR}/boxes.lua"
"


if [ $target = "latex" ]; then
  pandoc $ARGS $LATEX_ARGS \
    --to latex \
    $files;
elif [ $target = "pdf" ]; then
  pandoc $ARGS $LATEX_ARGS \
    --to pdf \
    --pdf-engine xelatex \
    -o "./$kind-manual.pdf" \
    $files
elif [ $target = "html" ]; then
  # check target directory exists
  if [ ! -d "./build/html" ]; then
    echo "create build/html directory"
    mkdir -p "./build/html"
  fi

  pandoc $ARGS \
   --to html \
   -o ./build/html/index.html \
  $files
fi

