#!/bin/bash
# Only publish from the main repository's master branch
if [ "$TRAVIS_REPO_SLUG" != "RubenVerborgh/AsyncIterator" ] || [ "$TRAVIS_BRANCH" != "master" ] || [ "$TRAVIS_PULL_REQUEST" != "false" ]; then
  exit
fi

echo -e "Publishing docs...\n"

# Update docs in repository
DOCS=$PWD/docs
pushd $HOME
git clone --quiet --branch=gh-pages https://${GH_TOKEN}@github.com/RubenVerborgh/AsyncIterator gh-pages 2>&1 > /dev/null
cd gh-pages
# Don't update if they are already at the latest version
if [[ `git log -1 --pretty=%B` == *$TRAVIS_COMMIT* ]]; then
  exit
fi
git rm -rf docs
cp -r $DOCS docs

# Push latest version of docs
git add docs
git config user.name  "Travis"
git config user.email "travis@travis-ci.org"
git commit -m "Update docs to $TRAVIS_COMMIT."
git push -fq origin gh-pages 2>&1 > /dev/null
popd
