#!/usr/bin/env bash

# This file is used by `direnv` to install all Python and Node dependencies
# specified in the Makefile into the virtual env located in the `.gotmp/env` directory.

# Steps to set up:
# 1. Install direnv: https://direnv.net/docs/installation.html
# 2. Hook direnv into your shell: https://direnv.net/docs/hook.html
# 3. Create the global config: https://github.com/direnv/direnv/blob/master/man/direnv.toml.1.md
#    where `~/workspace/ddev/.envrc` is the path to this file.
#    (Alternatively, `.envrc` file can be loaded with `direnv allow .`)
#    $ cat ~/.config/direnv/direnv.toml
#    [global]
#    hide_env_diff = true
#    [whitelist]
#    exact = ["~/workspace/ddev/.envrc"]
# 4. After setting this up, when you `cd` into this directory, all required tools
#    will automatically be installed in `.gotmp/env` and sourced.
# 5. To update tools, run `make clean`, then `cd` out of and back into this directory.

PYTHON_ENV=.gotmp/env/python
NODE_ENV=.gotmp/env/node

if ! command -v python >/dev/null 2>&1 && ! command -v python3 >/dev/null 2>&1; then
    echo "Python is not available."
    exit 1
fi

if [[ ! -f "${PYTHON_ENV}/bin/activate" ]]; then
    echo "Initializing python venv..."
    python -m venv "${PYTHON_ENV}" || python3 -m venv "${PYTHON_ENV}"
fi

source "${PYTHON_ENV}/bin/activate"

if [[ ! -f "${NODE_ENV}/bin/activate" ]]; then
    echo "Initializing python nodeenv..."
    pip install nodeenv
    nodeenv --node=lts --prebuilt "${NODE_ENV}"
fi

source "${NODE_ENV}/bin/activate"

if [[ ! -f "${PYTHON_ENV}/bin/mkdocs" ]]; then
    echo "Installing mkdocs..."
    pip install -r docs/mkdocs-pip-requirements
fi

if [[ ! -f "${PYTHON_ENV}/bin/pyspelling" ]]; then
    echo "Installing pyspelling..."
    pip install pyspelling pymdown-extensions
fi

if [[ ! -f "${NODE_ENV}/bin/markdownlint" ]]; then
    echo "Installing markdownlint..."
    npm install -g markdownlint-cli
fi

if [[ ! -f "${NODE_ENV}/bin/linkspector" ]]; then
    echo "Installing linkspector..."
    npm install -g @umbrelladocs/linkspector
fi

if [[ ! -f "${NODE_ENV}/bin/textlint" ]]; then
    echo "Installing textlint..."
    npm install -g textlint \
        textlint-filter-rule-comments \
        textlint-rule-no-todo \
        textlint-rule-stop-words \
        textlint-rule-terminology
fi
