# Ensure XDG_CONFIG_HOME is set
: "${XDG_CONFIG_HOME:=$HOME/.config}"

# Set default pager and editor globally
export PAGER="less -i -N -S -R"
export EDITOR="nvim"
export BROWSER="firefox"

# Set platform-specific Homebrew paths
if [[ "$(uname -m)" == "arm64" ]]; then
    # ARM Apple Silicon
    HOMEBREW_PREFIX="/opt/homebrew"
else
    # Intel
    HOMEBREW_PREFIX="/usr/local"
fi

# Ensure Homebrew bin/sbin are in the PATH
if [[ -d "$HOMEBREW_PREFIX" && ! "$PATH" =~ (^|:)${HOMEBREW_PREFIX}/bin(:|$) ]]; then
    export PATH="${HOMEBREW_PREFIX}/bin:${HOMEBREW_PREFIX}/sbin:$PATH"
fi

# Ensure PYENV_ROOT is set and its bin directory is in PATH
if [[ -n "$PYENV_ROOT" && ! "$PATH" =~ (^|:)${PYENV_ROOT}/bin(:|$) ]]; then
    export PATH="$PYENV_ROOT/bin:$PATH"
fi

export MODULAR_HOME="/Users/$(whoami)/.modular"
export PATH="/Users/$(whoami)/.modular/pkg/packages.modular.com_mojo:$PATH"

# Add ~/.local/bin to PATH if it exists
if [[ -d "$HOME/.local/bin" && ! "$PATH" =~ (^|:)${HOME}/.local/bin(:|$) ]]; then
    export PATH="$HOME/.local/bin:$PATH"
fi

# Add RVM to PATH if it exists
if [[ -d "$HOME/.rvm/bin" && ! "$PATH" =~ (^|:)${HOME}/.rvm/bin(:|$) ]]; then
    export PATH="$PATH:$HOME/.rvm/bin"
fi

# Add Go binaries to PATH if Go is installed
if command -v go &> /dev/null; then
    # Set GOPATH and add Go binaries to PATH
    export GOPATH="${GOPATH:-$HOME/.go}"
    if [[ ! "$PATH" =~ (^|:)${GOPATH}/bin(:|$) ]]; then
        export PATH="$PATH:${GOPATH}/bin"
    fi
fi

# Add Cargo (Rust) binaries to PATH if they exist
if [[ -d "$HOME/.cargo/bin" && ! "$PATH" =~ (^|:)${HOME}/.cargo/bin(:|$) ]]; then
    export PATH="$HOME/.cargo/bin:$PATH"
fi

# nvm setup (Node Version Manager)
if [[ -d "$HOME/.nvm" && ! "$PATH" =~ (^|:)${HOME}/.nvm(:|$) ]]; then
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # Load nvm
    [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # nvm bash completion
fi

# Add macOS-specific command-line utilities (GNU coreutils) to PATH if installed via Homebrew
if [[ -d "${HOMEBREW_PREFIX}/opt/coreutils/libexec/gnubin" && ! "$PATH" =~ (^|:)${HOMEBREW_PREFIX}/opt/coreutils/libexec/gnubin(:|$) ]]; then
    export PATH="${HOMEBREW_PREFIX}/opt/coreutils/libexec/gnubin:$PATH"
fi

# Add directories to PATH if they exist and are not already present
dirs=("${HOMEBREW_PREFIX}/bin" "${HOMEBREW_PREFIX}/sbin" "/usr/local/bin" "/usr/local/sbin")
for dir in "${dirs[@]}"; do
    if [[ -d "$dir" && ! "$PATH" =~ (^|:)$dir(:|$) ]]; then
        export PATH="$dir:$PATH"
    fi
done

# Ensure /usr/local/opt/openssl@1.1/bin is in the PATH if installed
if [[ -d "/usr/local/opt/openssl@1.1/bin" && ! "$PATH" =~ (^|:)/usr/local/opt/openssl@1.1/bin(:|$) ]]; then
    export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
fi

# Ensure /usr/local/opt/llvm/bin is in the PATH if installed
if [[ -d "/usr/local/opt/llvm/bin" && ! "$PATH" =~ (^|:)/usr/local/opt/llvm/bin(:|$) ]]; then
    export PATH="/usr/local/opt/llvm/bin:$PATH"
fi

