#!/bin/bash

# Ensure that this script is sourced, not executed
# Note that if the script was executed, we're running inside bash!
if [[ -n $BASH_VERSION ]] && [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
    "$(dirname $0)/conda" ..deactivate -h
    exit 1
fi

# Determine the directory containing this script
if [[ -n $BASH_VERSION ]]; then
    _SCRIPT_LOCATION=${BASH_SOURCE[0]}
elif [[ -n $ZSH_VERSION ]]; then
    _SCRIPT_LOCATION=${funcstack[1]}
else
    echo "Only bash and zsh are supported"
    return 1
fi
_THIS_DIR=$(dirname "$_SCRIPT_LOCATION")

_NEW_PATH=$("$_THIS_DIR/conda" ..activateroot "$@")
if (( $? == 0 )); then
    export PATH=$_NEW_PATH
    unset CONDA_DEFAULT_ENV
    if (( $($_THIS_DIR/conda ..changeps1) )); then
        PS1=$CONDA_OLD_PS1
        unset CONDA_OLD_PS1
    fi
else
    return $?
fi

if [[ -n $BASH_VERSION ]]; then
    hash -r
elif [[ -n $ZSH_VERSION ]]; then
    rehash
else
    echo "Only bash and zsh are supported"
    return 1
fi
