#!/bin/bash
#
# Updates the API-Doc in the 'gh-pages' branch.
#
# Copyright 2015 Mohiva Organisation (license at mohiva dot com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -o nounset -o errexit

if [ "$TRAVIS_REPO_SLUG" == "mohiva/play-silhouette" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then
  echo ""
  echo "Starting API-Doc update process"

  GHPAGES_DIR="$HOME/.sbt/ghpages/com.mohiva/root"
  GIT_DIR="$GHPAGES_DIR/.git"

  mkdir -p "$(dirname ${GHPAGES_DIR})"
  git clone --quiet --branch=gh-pages https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git "$GHPAGES_DIR" > /dev/null
  git config credential.helper "store --file=$GIT_DIR/credentials"
  echo "https://${GH_TOKEN}:@github.com" > "$GIT_DIR/credentials"
  git config --global user.email "travis@travis-ci.org"
  git config --global user.name "travis-ci"
  scripts/sbt ghpages-push-site

  echo ""
  echo "Finished API-Doc update process"
else
  echo ""
  echo "Skipping API-Doc update"
fi
