#!/bin/sh

set -e

# odpdown-init: initializes a directory with an empty odpdown presentation

THEMEDIR="/usr/share/odpdown-tools/themes"
DATADIR="/usr/share/odpdown-tools"

source $THEMEDIR/default.sh

destdir=$1
theme=$2

usage()
  {
  echo "usage: $0 <directory> [theme]"
  echo "       $0 -l"
  }

if [ -z "$destdir" ]; then
  echo "No directory specified, aborting." 1>&2
  usage
  exit 1
fi

if [ -z "$theme" ]; then
  theme=$THEME # default theme set in default.sh
fi

if [ ! -d "${THEMEDIR}/${theme}" ]; then
  echo "${theme}: no such theme. List available themes with '$0 -l'" 1>&2
  exit 1
fi

if [ "$destdir" = "-l" ]; then
  for theme in $(find ${THEMEDIR} -maxdepth 1 -type d | tail -n +2)
    do
    echo "$(basename "$theme")    $(head -n 1 ${theme}/description.txt)"
    done
  exit 0
fi

if [ -e "$destdir" -a ! -d "$destdir" ]; then
  echo "$destdir exists and is not a directory, aborting."
  usage
  exit 1
fi

if [ -e "$destdir"/metadata.mk ]; then
  echo "$destdir appears to have been initialized by $(basename $0) already, aborting."
  exit 1
fi

mkdir -p "$destdir"

tar -C "${DATADIR}/makefiles" -cf - . | tar -C "$destdir" -xf - --exclude=description.txt
tar -C "${THEMEDIR}/${theme}" -cf - . | tar -C "$destdir" -xf - --exclude=description.txt

echo "Empty[*] presentation with theme '$theme' created in directory"
echo
echo "  '$destdir'."
echo
echo "Next steps:"
echo
echo "  1. Edit ${destdir}/metadata.mk to set a presentation base file"
echo "     name (white space not recommended; keep it short)"
echo
echo "  2. Edit the title slide of"
echo "             ${destdir}/templates/template-4-3.odp"
echo "         and ${destdir}/templates/template-16-9.odp"
echo
echo "  3. Write your slides in ${destdir}/slides/main.md (and files"
echo "     included from there)"
echo
echo "  4. Drop any images you may have in the appropriate subdirectory"
echo "     of ${destdir}/img (see ${destdir}/img/README.md for details)"
echo
echo "  5. Compile your slides by running 'make' in ${destdir}"
echo
echo "[*] The presentation is not entirely empty since it already"
echo "    contains some sample slides. If you like, you can skip"
echo "    straight ahead to step 5 and compile this sample"
echo "    presentation."
echo
