#!/usr/bin/env bash

# Automatically generated by nengo-bones, do not edit this file directly

NAME=$0
COMMAND=$1
STATUS=0  # used to exit with non-zero status if any command fails

exe() {
    echo "\$ $*";
    # remove empty spaces from args
    args=( "$@" )
    for i in "${!args[@]}"; do
      [ -n "${args[$i]}" ] || unset "args[$i]"
    done
    "${args[@]}" || { echo -e "\033[1;31mCOMMAND '${args[0]}' FAILED\033[0m"; STATUS=1; }
}

if [[ ! -e nengo_spa ]]; then
    echo "Run this script from the root directory of this repository"
    exit 1
fi

if [[ "$COMMAND" == "install" ]]; then
    exe pip install "matplotlib" "$SCIPY"

    exe pip install \
        "sphinx>=3.0.0,<3.1.0" \
        "jupyter>=1.0.0" \
        "nbsphinx>=0.2.13" \
        "numpydoc>=0.6.0" \
        "git+https://github.com/nengo/nengo-sphinx-theme#egg=nengo-sphinx-theme"
    exe pip install -e ".[docs]"
elif [[ "$COMMAND" == "before_script" ]]; then
    :
elif [[ "$COMMAND" == "script" ]]; then

    if [ -d "../nengo-spa-docs" ]; then exe rm -Rf ../nengo-spa-docs; fi
    exe git clone -b gh-pages-release https://github.com/nengo/nengo-spa.git ../nengo-spa-docs
    RELEASES=$(find ../nengo-spa-docs -maxdepth 1 -type d -name "v[0-9].*" -printf "%f,")

    FAILED_FILE="$TRAVIS_JOB_NUMBER.failed"
    rm -f "$FAILED_FILE"
    if [[ "$TRAVIS_BRANCH" == "$TRAVIS_TAG" ]]; then
        RELEASES="$RELEASES$TRAVIS_TAG"
        sphinx-build -b html docs ../nengo-spa-docs/"$TRAVIS_TAG" -vW --keep-going -A building_version="$TRAVIS_TAG" -A releases="$RELEASES" || touch "$FAILED_FILE"
    else
        sphinx-build -b html docs ../nengo-spa-docs -vW --keep-going -A building_version=latest -A releases="$RELEASES" || touch "$FAILED_FILE"
    fi

    if [[ -e "$FAILED_FILE" ]]; then
        echo -e "\033[1;31mCOMMAND 'sphinx-build' FAILED\033[0m"
        STATUS=1
    fi

    exe sphinx-build -b linkcheck -vW -D nbsphinx_execute=never docs docs/_build
    exe sphinx-build -b doctest -vW -D nbsphinx_execute=never docs docs/_build

elif [[ "$COMMAND" == "before_cache" ]]; then
    :
elif [[ "$COMMAND" == "after_success" ]]; then
    :
elif [[ "$COMMAND" == "after_failure" ]]; then
    :
elif [[ "$COMMAND" == "before_deploy" ]]; then
    :
elif [[ "$COMMAND" == "after_deploy" ]]; then
    :
elif [[ "$COMMAND" == "after_script" ]]; then
    FAILED_FILE="$TRAVIS_JOB_NUMBER.failed"
    if [[ -e "$FAILED_FILE" ]]; then
        echo "Skipping docs upload because build failed"
        exit 1
    fi

    if [[ "$TRAVIS_EVENT_TYPE" == "cron" ]]; then
        echo "Skipping docs upload because this is a cron build"
        exit 0
    fi

    cd ../nengo-spa-docs || exit
    git config --global user.email "info@appliedbrainresearch.com"
    git config --global user.name "NengoBones"
    git add --all

    if [[ "$TRAVIS_BRANCH" == "$TRAVIS_TAG" ]]; then
        exe git commit -m "Documentation for release $TRAVIS_TAG"
        exe git push -q "https://$GH_TOKEN@github.com/nengo/nengo-spa.git" gh-pages-release
    elif [[ "$BRANCH_NAME" == "master" ]]; then
        exe git commit -m "Last update at $(date '+%Y-%m-%d %T')"
        exe git push -fq "https://$GH_TOKEN@github.com/nengo/nengo-spa.git" gh-pages-release:gh-pages
    elif [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then
        exe git commit -m "Documentation for branch $TRAVIS_BRANCH"
        exe git push -fq "https://$GH_TOKEN@github.com/nengo/nengo-spa.git" gh-pages-release:gh-pages-test
    fi
elif [[ -z "$COMMAND" ]]; then
    echo "$NAME requires a command like 'install' or 'script'"
else
    echo "$NAME does not define $COMMAND"
fi

if [[ "$COMMAND" != "script" && -n "$TRAVIS" && "$STATUS" -ne "0" ]]; then
    travis_terminate "$STATUS"
fi
exit "$STATUS"
