#!/bin/sh
set -e

if [ "`git status -s`" ]
then
    echo "The working directory is dirty. Please commit any pending changes."
    exit 1;
fi

echo "Deleting old publication"
rm -rf docs/public
mkdir docs/public
git worktree prune
rm -rf .git/worktrees/public/

echo "Checking out gh-pages branch into public"
git worktree add -B gh-pages docs/public origin/gh-pages

echo "Removing existing files"
rm -rf docs/public/*

echo "Generating site"
cd docs && HUGO_ENV="production" hugo --gc

echo "Updating gh-pages branch"
cd public && git add --all && git commit -m "Publishing to gh-pages (hack/publish/gh-pages)"

echo "Pushing to github"
git push --all