#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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 -euo pipefail

MY_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

BREEZE_BINARY=breeze
COLOR_RED=$'\e[31m'
COLOR_RESET=$'\e[0m'
COLOR_YELLOW=$'\e[33m'

function manual_instructions() {
    echo "Please run those commands manually (you might need to restart shell between them):"
    echo
    echo "    pip -m install pipx"
    echo "    pipx ensurepath"
    echo "    pipx install -e '${MY_DIR}/dev/breeze/'"
    echo "    breeze setup-autocomplete --force"
    echo
    echo "   After that, both pipx and breeze should be available ono your path"
    exit
}

function check_breeze_installed() {
    set +e
    command -v "${BREEZE_BINARY}" >/dev/null
    local breeze_on_path=$?
    command -v "pipx" >/dev/null
    local pipx_on_path=$?
    set -e
    if [[ ${breeze_on_path} != "0" || ${pipx_on_path} != "0"  ]]; then
        echo
        if [[ ${pipx_on_path} != 0 ]]; then
            echo "${COLOR_RED}The 'pipx' is not on path. It should be installed and 'pipx' should be available on your PATH.${COLOR_RESET}"
            export TIMEOUT=0
            if "${MY_DIR}/scripts/tools/confirm" "Installing pipx?"; then
                python -m pip install pipx --upgrade
                echo
                echo "${COLOR_YELLOW}Please close and re-open the shell and retry. You might need to add 'pipx' to the PATH!${COLOR_RESET}"
                echo
                exit
            else
                manual_instructions
            fi
        fi
        if [[ ${breeze_on_path} != 0 ]]; then
            echo "${COLOR_RED}The '${BREEZE_BINARY}' is not on path. Breeze should be installed and 'breeze' should be available on your PATH!${COLOR_RESET}"
            export TIMEOUT=0
            if "${MY_DIR}/scripts/tools/confirm" "Installing breeze?"; then
                pipx ensurepath --force
                pipx install -e "${MY_DIR}/dev/breeze/" --force
                ${BREEZE_BINARY} setup-autocomplete --force --answer yes
                echo
                echo "${COLOR_YELLOW}Please close and re-open the shell and retry. Then rerun your last command!${COLOR_RESET}"
                echo
                exit
            else
                manual_instructions
            fi
        fi
    fi
}

check_breeze_installed

${BREEZE_BINARY} "${@}"
