# functions

# Function to remove duplicates from a specified environment variable
dedup_env_var() {
    local var_name="$1"
    local var_value=$(eval echo "\$$var_name")

    # Use awk to remove duplicate entries
    local deduped_value=$(echo "$var_value" | awk -v RS=: -v ORS=: '!seen[$0]++')
    # Remove trailing colon
    deduped_value=${deduped_value%:}

    # Export the deduplicated value back to the environment variable
    export $var_name="$deduped_value"
}

# Function to find the .venv directory
function find_venv() {
  local dir="$PWD"

  while [[ "$dir" != "/" ]]; do
    if [[ -d "$dir/.venv" ]]; then
      echo "true $dir/.venv"
      return 0
    fi
    dir=$(dirname "$dir")
  done

  echo "false"
  return 1
}

# Function that uses the .venv directory found by find_venv
function python_venv() {
  local result
  result=$(find_venv)

  if [[ "$result" == true* ]]; then
    local venv_path="${result#true }"
    source "$venv_path/bin/activate"
    # echo "activated $venv_path"
  else
    # echo "No .venv directory found"

    # Check if a virtual environment is currently active
    if [[ -n "$VIRTUAL_ENV" ]]; then
      deactivate
      # echo "deactivated venv"
    fi
  fi
}


# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# If you come from bash you might have to change your $PATH.
export PATH="$HOME/bin:/usr/local/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

ZSH_THEME="powerlevel10k/powerlevel10k"

zstyle ':omz:update' mode reminder  # just remind me to update when it's time
zstyle ':omz:update' frequency 7
ENABLE_CORRECTION="true"

COMPLETION_WAITING_DOTS="true"

# see 'man strftime' for details.
HIST_STAMPS="%F %T "

plugins=(git zsh-autosuggestions zsh-syntax-highlighting virtualenv)

source $ZSH/oh-my-zsh.sh

# User configuration

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

export PATH="/usr/local/cuda-12.4/bin:$PATH"
export PATH="/home/suppl/.local/bin:$PATH"

export LD_LIBRARY_PATH="/usr/local/cuda-12.4/lib64:$LD_LIBRARY_PATH"
export PYENV_ROOT="$HOME/.pyenv"
[[ -d "$PYENV_ROOT/bin" ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

autoload -U add-zsh-hook
add-zsh-hook chpwd python_venv

export VSCODE_ROOT="/mnt/c/Program Files/Microsoft VS Code"
export PATH="$VSCODE_ROOT/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"

eval "$(zoxide init --cmd cd zsh)"

# dedup env vars
dedup_env_var PATH
dedup_env_var LD_LIBRARY_PATH
