#!/usr/bin/env bash
#
# Bootstrap for https://github.com/theintonerzero/university-latex-template
#
#   mkdir cse3cap-a2 && cd cse3cap-a2
#   curl -fsSL https://dl.darkovski.dev/git/university-latex-template/install.sh | bash
#
# Copies the template into whatever directory you run it from and throws
# the repository's .git away. The result is a fresh start for one
# assignment rather than a checkout tracking somebody else's history, so
# run `git init` yourself when you want to version your own work.
#
# Use -fsSL rather than -s on the curl: without -f, curl prints an error
# page to stdout on a 404 and bash cheerfully executes it.
#
# Piped into bash this runs under whatever `bash` is on PATH, which on
# macOS is still 3.2. Nothing here may assume anything newer.

set -euo pipefail

REPO_URL="https://github.com/theintonerzero/university-latex-template.git"
DEST="$PWD"

say() { printf '\033[1;34m==>\033[0m %s\n' "$1"; }
die() { printf '\033[1;31mError:\033[0m %s\n' "$1" >&2; exit 1; }

# ---------------------------------------------------------------------------
# Prerequisites
# ---------------------------------------------------------------------------
if ! command -v git >/dev/null 2>&1; then
    case "$(uname -s)" in
    Darwin) hint="xcode-select --install" ;;
    Linux)
        if   command -v dnf     >/dev/null 2>&1; then hint="sudo dnf install -y git"
        elif command -v apt-get >/dev/null 2>&1; then hint="sudo apt-get install -y git"
        else hint="install git with your package manager"
        fi
        ;;
    *) hint="install git" ;;
    esac
    die "git is required but not installed. Try: $hint"
fi

[ -w "$DEST" ] || die "$DEST is not writable."

# ---------------------------------------------------------------------------
# Fetch
# ---------------------------------------------------------------------------
# Clone to a scratch directory first. Cloning straight into $PWD would
# demand an empty directory, and the collision check below is deliberately
# narrower than that: it only objects to the names the template itself
# brings, and leaves anything else already here alone.
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

say "Fetching template"
git clone --quiet --depth 1 "$REPO_URL" "$tmp/repo"
rm -rf "$tmp/repo/.git"

# dotglob so .gitignore comes across too; without it the LaTeX build
# artifacts would land untracked-but-unignored in a later `git init`.
shopt -s dotglob nullglob
entries=("$tmp"/repo/*)
[ "${#entries[@]}" -gt 0 ] || die "The template repository is empty. Its layout has changed."

# ---------------------------------------------------------------------------
# Copy in, refusing to overwrite
# ---------------------------------------------------------------------------
conflicts=()
for entry in "${entries[@]}"; do
    name="${entry##*/}"
    if [ -e "$DEST/$name" ]; then
        # += rather than ("${conflicts[@]}" "$name"): expanding an empty
        # array is an unbound-variable error under set -u in bash 3.2.
        conflicts+=("$name")
    fi
done

if [ "${#conflicts[@]}" -gt 0 ]; then
    # Clobbering is never the right guess here. Either this is the wrong
    # directory, or the template is already in it and has been edited.
    die "$DEST already contains: ${conflicts[*]}
       Run this in an empty directory, or move those out of the way first."
fi

say "Writing into $DEST"
cp -R "${entries[@]}" "$DEST/"

for entry in "${entries[@]}"; do
    printf '      %s\n' "${entry##*/}"
done

echo
say "Done. Compile with: python3 compile.py"
