#!/usr/bin/env bash

set +e


SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
export GLOBUS_STREAMS_SOURCE_FILE=${SCRIPT_DIR}/globus-streams-env
export GLOBUS_STREAMS_PYENV=${SCRIPT_DIR}/streams-cli
ARCH=$(uname -m)
LIBDIR=${SCRIPT_DIR}/lib/${ARCH}
PYTHON_CLI_REPO="stable"
PIP_UNSTABLE=""


function echo_stderr() {
    echo "$@" >&2
}


function setup_python_venv {
    if [[ -e "${GLOBUS_STREAMS_PYENV}/bin/activate" ]]; then
       echo_stderr "CLI tools appear to be installed."
       echo_stderr "Run with -c to remove and reinstall or source the environment file to use the tools."
       return 0
    fi

    echo_stderr "Installing CLI tools for ..."
    python3 -mvenv ${GLOBUS_STREAMS_PYENV}
    . ${GLOBUS_STREAMS_PYENV}/bin/activate
    pip install ${PIP_UNSTABLE} --extra-index-url https://downloads.globus.org/globus-connect-server/${PYTHON_CLI_REPO}/wheels/ globus-streams-cli
}


function clean {
    rm -f ${GLOBUS_STREAMS_SOURCE_FILE}
    rm -rf ${GLOBUS_STREAMS_PYENV}
    exit 0
}


function write_out_env {
    setup_python_venv

    echo "export LD_LIBRARY_PATH=${LIBDIR}" > ${GLOBUS_STREAMS_SOURCE_FILE}
    echo "export GLOBUS_STREAMS_CLIENT_LIB=${LIBDIR}/libglobus_streams_client.so.0" >> ${GLOBUS_STREAMS_SOURCE_FILE}
    echo "source ${SCRIPT_DIR}/streams-cli/bin/activate" >> ${GLOBUS_STREAMS_SOURCE_FILE}
}


function details {
    echo_stderr "To active the environemnt run:"
    echo_stderr ""
    echo_stderr "source ${GLOBUS_STREAMS_SOURCE_FILE}"
    echo_stderr ""
    echo_stderr "To see more information on available tools run:"
    echo_stderr ""
    echo_stderr "globus-streams --help"
    echo_stderr ""
    echo_stderr "${SCRIPT_DIR}/bin/globus-streams-launch.sh -h"
    echo_stderr ""
}


function usage() {
    echo "Usage: $0 [-r testing|stable|unstable] [-c]"
    echo "  -c  Clean the configuration and remove installed tools"
    echo "  -r  Set the repository to pull python CLI tools from (default: stable)"
    exit 1
}


function help {
    echo_stderr ""
    echo_stderr "Setup an environment to use Globus Steaming client tools"
    echo_stderr "========================================================"
    echo_stderr "This script will install python tools into a virtual environment at ${GLOBUS_STREAMS_PYENV}. It will also install an environment configuration file that can be sourced in order to set needed environment variables."
    echo_stderr ""
    usage
    echo_stderr ""

    exit 1
}

while getopts "hcr:" opt; do
  case $opt in
    r)
        case "$OPTARG" in
            testing|unstable)
                PYTHON_CLI_REPO="$OPTARG"
                PIP_UNSTABLE="--pre"
                ;;

            stable)
                PYTHON_CLI_REPO="$OPTARG"
                ;;
            *)
                echo_stderr "Invalid repository type specified: $OPTARG"
                echo_stderr "Valid options are: testing, unstable, stable"
                exit 1
        esac
      ;;
    c)
      clean
      ;;
    h)
      help
      ;;
  esac
done
shift $((OPTIND - 1))

cd ${SCRIPT_DIR}

write_out_env
details
