#!/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 abr_control ]]; then
    echo "Run this script from the root directory of this repository"
    exit 1
fi


shopt -s globstar

if [[ "$COMMAND" == "install" ]]; then
    :


    exe pip install "jupyter>=1.0.0" "pylint>=1.9.2" "codespell>=1.12.0" "gitlint>=0.1.2" "collective.checkdocs>=0.2" "flake8>=3.7.7"
elif [[ "$COMMAND" == "before_script" ]]; then
    :
elif [[ "$COMMAND" == "script" ]]; then

    exe pylint abr_control --rcfile=setup.cfg --jobs=0
    exe flake8 abr_control
    if ls docs/**/*.ipynb &>/dev/null; then
        exe jupyter nbconvert \
            --log-level WARN \
            --to python \
            --TemplateExporter.exclude_input_prompt=True \
            --TemplateExporter.exclude_markdown=True \
            -- docs/**/*.ipynb
        # Remove style issues introduced in the conversion:
        #   s/# $/#/g replaces lines with just '# ' with '#'
        #   /get_ipython()/d removes lines containing 'get_ipython()'
        #   $ d removes a trailing newline
        for nb_file in docs/**/*.ipynb; do
            sed -i \
                -e 's/# $/#/g' \
                -e '/get_ipython()/d' \
                -e '$ d' \
                -- "${nb_file%.ipynb}.py"
        done
    fi
    if [[ "$(python -c 'import sys; print(sys.version_info >= (3, 6))')" == "True" ]]; then
        exe black --check abr_control
    fi
    exe pylint docs --rcfile=setup.cfg --jobs=0 --disable=missing-docstring,trailing-whitespace,wrong-import-position,unnecessary-semicolon
    exe flake8 docs --extend-ignore=E402,E703,W291,W293,W391
    for nb_file in docs/**/*.ipynb; do
        rm "${nb_file%.ipynb}.py"
    done
    exe codespell -q 3 \
        --skip="./build,*/_build,*-checkpoint.ipynb,./.eggs,./.git,*/_vendor" \
        --ignore-words-list="DOF,dof,hist,nd,compiletime"
    exe shellcheck -e SC2087 .ci/*.sh
    # undo single-branch cloning
    git config --replace-all remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
    git fetch origin master
    N_COMMITS=$(git rev-list --count HEAD ^origin/master)
    for ((i=0; i<N_COMMITS; i++)) do
        git log -n 1 --skip "$i" --pretty=%B \
            | grep -v '^Co-authored-by:' \
            | gitlint -vvv || STATUS=1
    done
    exe python setup.py checkdocs

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
    :
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"
