#!/bin/sh

# forked from https://gist.github.com/motemen/8595451

set -eux

remote=$(git config remote.origin.url)
described_rev=$(git rev-parse HEAD | git name-rev --stdin)

pages_dir="./out"

if [ ! -d "$pages_dir" ]
then
    echo "Error: $pages_dir directory does not exist"
    exit 1
fi

cd "$pages_dir"

cdup=$(git rev-parse --show-cdup)
if [ "$cdup" != '' ]
then
    git init
    git config user.email saltycrane@gmail.com
    git remote add --fetch origin "$remote"
fi

if git rev-parse --verify origin/gh-pages > /dev/null 2>&1
then
    git checkout gh-pages
else
    git checkout --orphan gh-pages
fi

git add .
git commit -m "github pages built at $described_rev"
git push origin gh-pages
