#! /bin/sh

PAGES_BRANCH=gh-pages
DOC_DIR=doc

set -e

git reset -q HEAD

# Create the tree object
git add -f $DOC_DIR
tree=$(git write-tree --prefix=$DOC_DIR)

# Get the parent commit for the tree. If the branch does not yet exist, create
# it
msg="generated from $head_sha"
head_sha=$(git show-ref -s -h HEAD)
if git show-ref -q --verify refs/heads/$PAGES_BRANCH; then
    parent=$(git show-ref -s refs/heads/$PAGES_BRANCH)

    # Check that some things changes since last time
    last_tree=$(git cat-file commit refs/heads/$PAGES_BRANCH | grep tree | awk '{print $2}')
    if test "x$last_tree" = "x$tree"; then
        echo "no changes to commit"
    else
        echo "updating $PAGES_BRANCH"
        commit=$(echo $msg | git commit-tree $tree -p $parent)
    fi
else
    echo "creating initial commit on the $PAGES_BRANCH branch"
    commit=$(echo $msg | git commit-tree $tree)
fi

if test -n "$commit"; then
    # And finally update the tip of the branch
    echo $commit > .git/refs/heads/$PAGES_BRANCH
fi

git reset -q HEAD

