# vim:ft=bash
# Put this file in $XDG_CONFIG_HOME/bash/profile
# This file is sourced by /etc/profile.d/xdg-compat.sh
# and will source user's XDG-compliant Bash Run Commands

# If no admin rights, just
# ln -sf $DOTFILES/tools/bash/profile ~/.bash_profile

# Before antidots
if [ -n "$BASH_VERSION" ] && [ -f "$HOME/.bash_profile" ]; then
    . "$HOME/.bash_profile"
fi

if [ -n "$BASH_VERSION" ] && [ -f "$XDG_CONFIG_HOME/bash/bashrc" ]; then
    . "$XDG_CONFIG_HOME/bash/bashrc"
fi

pathadd() {
    # 检查是否存在且不在 PATH 中
    if [[ -d "$1" && ":$PATH:" != *":$1:"* ]]; then
        # 检查是否是符号链接
        if [[ -L "$1" ]]; then
            # 检查符号链接指向的目标是否存在
            if [[ -e "$1" ]]; then
                PATH="$1:$PATH"
            fi
        else
            # 普通目录直接添加
            PATH="$1:$PATH"
        fi
    fi
}


# Dynamically Add Path
minimal_path=(
    "/bin"
    "/sbin"
    "/usr/bin"
    "/usr/sbin"
    "/usr/local/bin"
    "/usr/local/sbin"
    "$HOME/.local/bin"
    "$HOME/.local/sbin"
    "$HOME/.local/scripts"
)

export INPUTRC="$XDG_CONFIG_HOME"/readline/inputrc

for p in "${minimal_path[@]}"; do
    pathadd "$p"
done

if [ -d "/opt/homebrew/bin" ]; then # macOS
    export PATH="/opt/homebrew/bin:$PATH"
elif [ -d "/home/linuxbrew/.linuxbrew/bin" ]; then # Linux
    export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
fi

# macOS Specific
# This syntax is POSIX standard, for portability
if [ "$(uname)" = "Darwin" ]; then
    :   # Do nothing
fi

# Linux Specific
if [ "$(uname)" = "Linux" ]; then
    # CUDA
    export CUDA_CACHE_PATH="$XDG_CACHE_HOME"/nv
fi

# Azure CLI
if command -v az > /dev/null; then
    export AZURE_CONFIG_DIR="$XDG_DATA_HOME"/azure
fi
# Bun JS
# mv ~/.bun $XDG_DATA_HOME/bun
# ln -sf $XDG_DATA_HOME/bun/bin/bun ~/.local/bin/bun
if command -v bun > /dev/null; then
    export BUN_INSTALL="$XDG_DATA_HOME"/bun
    export PATH="$BUN_INSTALL/bin:$PATH"
    [ -s "$BUN_INSTALL/_bun" ] && source "$BUN_INSTALL/_bun"
fi
# Cargo
if command -v cargo > /dev/null; then
    export CARGO_HOME="$XDG_DATA_HOME"/cargo
    export PATH="$CARGO_HOME/bin:$PATH"
fi
# CGDB
if command -v cgdb > /dev/null; then
    export CGDB_DIR="$XDG_CONFIG_HOME"/cgdb
fi
# .NET
if command -v dotnet > /dev/null; then
    export DOTNET_CLI_HOME="$XDG_DATA_HOME"/dotnet
    export PATH="$DOTNET_CLI_HOME/.dotnet/tools:$PATH"
fi
# Docker
if command -v docker > /dev/null; then
    export DOCKER_CONFIG="$XDG_CONFIG_HOME"/docker
fi
# GnuPG
if command -v gpg > /dev/null; then
    export GNUPGHOME="$XDG_DATA_HOME"/gnupg
fi
# Go
export GOPATH="$XDG_DATA_HOME"/go
export PATH="$GOPATH/bin:$PATH"
# Julia
if command -v julia > /dev/null; then
    export JULIA_DEPOT_PATH="$XDG_DATA_HOME/julia:$JULIA_DEPOT_PATH"
fi
# Node.js
if command -v node > /dev/null; then
    export NODE_REPL_HISTORY="$XDG_STATE_HOME"/node/repl_history
    export TS_NODE_REPL_HISTORY="$XDG_STATE_HOME"/node/ts_node_repl_history
    export NPM_CONFIG_USERCONFIG="$XDG_CONFIG_HOME"/npm/npmrc
    export NPM_CONFIG_INIT_MODULE="$XDG_CONFIG_HOME"/npm/config/npm-init.js
    export NPM_CONFIG_CACHE="$XDG_CACHE_HOME"/npm
    export NPM_CONFIG_TMP="$XDG_RUNTIME_DIR"/npm
fi
# Parallel
if command -v parallel > /dev/null; then
    export PARALLEL_HOME="$XDG_CONFIG_HOME"/parallel
fi
# Python
# Works only with Python 3.13.0a3 and later
if command -v python3 > /dev/null; then
    export PYTHON_HISTORY="$XDG_DATA_HOME"/python/history
fi
# GNU Screen
if command -v screen > /dev/null; then
    export SCREENRC="$XDG_CONFIG_HOME"/screen/screenrc
    export SCREENDIR="${XDG_RUNTIME_DIR}/screen"
fi
# Ruby Gem
# Ruby Gem
if command -v gem > /dev/null; then
    for dir in "$HOME/.local/share/gem/ruby/"*/bin; do
        if [ -d "$dir" ]; then
            export PATH="$dir:$PATH"
        fi
    done
fi
# Spacemacs
if command -v emacs > /dev/null; then
    export SPACEMACSDIR="$XDG_CONFIG_HOME"/spacemacs
fi
# tldr
# Works only with C client (did not verify)
if command -v tldr > /dev/null; then
    export TLDR_CACHE_DIR="$XDG_CACHE_HOME"/tldr
fi
# W3M
if command -v w3m > /dev/null; then
    export W3M_DIR="$XDG_DATA_HOME"/w3m
fi
# Wakatime
if command -v wakatime > /dev/null; then
    export WAKATIME_HOME="$XDG_CONFIG_HOME/wakatime"
fi
# Wget
if command -v wget > /dev/null; then
    alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"'
fi
# zsh .zcompdump
# compinit -d "$XDG_CACHE_HOME"/zsh/zcompdump-"$ZSH_VERSION"
# Vcpkg
if command -v vcpkg > /dev/null; then
    export VCPKG_ROOT="$XDG_DATA_HOME"/vcpkg
fi


export PADER="less -R"
export EDITOR="nvim"
export VISUAL="nvim"
