#!/bin/sh
set -e

build_slides() {
  ./index.py
  for YAML in *.yml; do
    ./markmaker.py $YAML > $YAML.html || {
      rm $YAML.html
      break
    }
  done
  if [ -n "$SLIDECHECKER" ]; then
    for YAML in *.yml; do
      ./appendcheck.py $YAML.html
    done
  fi
  zip -qr slides.zip . && echo "Created slides.zip archive."
}


case "$1" in
once)
  build_slides
  ;;

forever)
  set +e
  # check if entr is installed
  if ! command -v inotifywait >/dev/null; then
    echo >&2 "First install 'inotifywait' with apt, brew, etc."
    exit
  fi

  while true; do
    inotifywait -e modify -e delete -e create -r .
    build_slides
  done
  ;;

*)
  echo "$0 <once|forever>"
  ;;
esac
