#!/bin/sh

# Helper script to export LaTeX templates for new documents
# Copyright (c) 2009-2022 Benjamin Mako Hill <mako@atdot.cc>
# Released under the GPLv3 or later.

set -e

SOURCEDIR="$HOME/tex/beamer-mako"
CURDIR=$(pwd)

if [ -n "$1" ]
then
    if [ -e "$1" ]; then
        echo "error: '$1' already exists"
        exit 1
    fi

    WORK_DIR=$(mktemp -d)
    cd "$SOURCEDIR"
    git archive --format=tar HEAD template | tar x --strip=1 -C "$WORK_DIR"
    cd "$CURDIR"
    mv "$WORK_DIR" "$1"

    mv "$1/example.tex" "$1/$(basename "$1").tex"

    # create a symlink farm for the fonts
    cd "$1"
    rm -rf ./fonts/*

    # first create the directories
    for fontdir in "${SOURCEDIR}/template/fonts/"*; do
        mkdir "./fonts/$(basename "${fontdir}")"
        # then the files
        for fontfile in "${fontdir}/"*; do
            ln -s "${fontfile}" "./fonts/$(basename "${fontdir}")/$(basename "${fontfile}")"
        done
    done

    cd "$CURDIR"
else
    echo "specify a directory where the template should go"
fi
