#!/bin/sh

# Sets things up for deployment.

usage() {
    echo >&2 "Usage: "$0" [-h] [--] <deploy-branch> <deploy-dir>"
    exit 2
}

getopts 'h' _ && usage
[ "$1" = '--' ] && shift
[ $# -ne 2 ] && usage

if [ ! -e .git ]; then
    echo >&2 "not the root of a git repo"
    exit 1
fi

set -e

branch=$(git branch --show-current)

revert() {
    git checkout -q "$branch"
}

# Prepare the deployment branch.
git switch --orphan="$1" && git commit --allow-empty -m "Root commit"

revert

# Create the deployment directory and use it the a work tree of the
# deployment branch.
git worktree add "$2" "$1" || true

# Ensure that the deployment directory is not tracked "from the outside".
bin/ignore "$2"
