#!/usr/bin/env bash

# Source this file from bash or zsh to activate the checkout-managed DAY-EC
# conda environment.

__daylily_activate_is_sourced() {
  if [ -n "${ZSH_VERSION:-}" ]; then
    case "${ZSH_EVAL_CONTEXT:-}" in
      *:file|*:file:*) return 0 ;;
    esac
    return 1
  fi

  if [ -n "${BASH_VERSION:-}" ]; then
    [ "${BASH_SOURCE[0]}" != "$0" ]
    return
  fi

  return 1
}

if ! __daylily_activate_is_sourced; then
  echo "Error: this script must be sourced." >&2
  echo "Usage: source ./activate" >&2
  exit 1
fi

__daylily_activate_script_path() {
  if [ -n "${BASH_VERSION:-}" ]; then
    printf '%s\n' "${BASH_SOURCE[0]}"
    return 0
  fi

  if [ -n "${ZSH_VERSION:-}" ]; then
    eval 'printf "%s\n" "${(%):-%N}"'
    return 0
  fi

  return 1
}

__daylily_activate_conda_is_function() {
  case "$(type conda 2>/dev/null)" in
    *"shell function"*|*"function"*) return 0 ;;
  esac
  return 1
}

__daylily_activate_conda_exe() {
  if [ -n "${CONDA_EXE:-}" ] && [ -x "${CONDA_EXE}" ]; then
    printf '%s\n' "${CONDA_EXE}"
    return 0
  fi

  if [ -n "${BASH_VERSION:-}" ]; then
    type -p conda
    return
  fi

  if [ -n "${ZSH_VERSION:-}" ]; then
    whence -p conda
    return
  fi

  command -v conda
}

__daylily_activate_load_conda() {
  if ! command -v conda >/dev/null 2>&1; then
    return 1
  fi

  if __daylily_activate_conda_is_function; then
    return 0
  fi

  local conda_exe conda_shell conda_hook conda_base
  conda_exe="$(__daylily_activate_conda_exe 2>/dev/null || true)"
  if [ -z "${conda_exe}" ]; then
    return 1
  fi

  if [ -n "${BASH_VERSION:-}" ]; then
    conda_shell="bash"
  elif [ -n "${ZSH_VERSION:-}" ]; then
    conda_shell="zsh"
  else
    conda_shell="posix"
  fi

  conda_hook="$("${conda_exe}" "shell.${conda_shell}" hook 2>/dev/null || "${conda_exe}" shell.posix hook 2>/dev/null || true)"
  if [ -n "${conda_hook}" ]; then
    eval "${conda_hook}"
    return 0
  fi

  conda_base="$("${conda_exe}" info --base 2>/dev/null || true)"
  if [ -n "${conda_base}" ] && [ -f "${conda_base}/etc/profile.d/conda.sh" ]; then
    . "${conda_base}/etc/profile.d/conda.sh"
    return 0
  fi

  return 1
}

__daylily_activate_refresh_command_cache() {
  hash -r 2>/dev/null || true
  rehash 2>/dev/null || true
}

__daylily_activate_dayec_exists() {
  conda env list 2>/dev/null | awk '{print $1}' | grep -qx 'DAY-EC'
}

__daylily_activate_repo_root="$(
  cd "$(dirname "$(__daylily_activate_script_path)")" && pwd
)"
export DAYLILY_EC_REPO_ROOT="${__daylily_activate_repo_root}"
export DAYLILY_EC_ACTIVE="1"

__daylily_activate_load_conda || {
  echo "Error: conda is not available in this shell." >&2
  return 1
}

__daylily_activate_created_dayec=0
if ! __daylily_activate_dayec_exists; then
  __daylily_activate_env_yaml="${DAYLILY_EC_REPO_ROOT}/environment.yaml"
  if [ ! -f "${__daylily_activate_env_yaml}" ]; then
    echo "Error: DAY-EC bootstrap spec not found at ${__daylily_activate_env_yaml}." >&2
    return 1
  fi

  echo "Creating DAY-EC from ${__daylily_activate_env_yaml} ..." >&2
  conda env create -n DAY-EC -f "${__daylily_activate_env_yaml}" || return 1
  __daylily_activate_created_dayec=1
fi

conda activate DAY-EC || {
  echo "Error: failed to activate the DAY-EC conda environment." >&2
  return 1
}
__daylily_activate_refresh_command_cache

if [ "${__daylily_activate_created_dayec}" = "1" ]; then
  echo "Installing daylily-ephemeral-cluster into DAY-EC from ${DAYLILY_EC_REPO_ROOT} ..." >&2
  python -m pip install --editable "${DAYLILY_EC_REPO_ROOT}" || {
    echo "Error: failed to install daylily-ephemeral-cluster into DAY-EC." >&2
    return 1
  }
  __daylily_activate_refresh_command_cache
  echo "Installed daylily-ephemeral-cluster into DAY-EC from ${DAYLILY_EC_REPO_ROOT}." >&2
fi

unset __daylily_activate_created_dayec
unset __daylily_activate_env_yaml
unset __daylily_activate_repo_root
unset -f __daylily_activate_is_sourced
unset -f __daylily_activate_script_path
unset -f __daylily_activate_conda_is_function
unset -f __daylily_activate_conda_exe
unset -f __daylily_activate_load_conda
unset -f __daylily_activate_refresh_command_cache
unset -f __daylily_activate_dayec_exists

echo "DAY-EC activated."
