# Override in .profile.local for custom repo
DOTFILES_LOCAL_REPOSITORY="https://gitbox.branderson.io/branderson/dotfiles-local"

# Programs expected to be installed by tools
# These are the name on $PATH, not the package name
dependencies=(
    tree
    nvim
    ag
    fzf
    node
    cowsay
    fortune
)

# Load dotfiles directory into DOTFILES_DIR
# Dotfiles directory should be in environment or ~/.dotfiles-config
if [ -f "$HOME/.dotfiles-config" ]; then
    source "$HOME/.dotfiles-config"
fi

# PATHs
## Earlier paths in PATH checked first
if [ -d /usr/games ]; then
    export PATH="/usr/games:$PATH"
fi
if [ -d /opt/homebrew/opt/coreutils/libexec/gnubin ]; then
    export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
fi
if [ -d /opt/homebrew/opt/python@3.13/libexec/bin ]; then
    export PATH="/opt/homebrew/opt/python@3.13/libexec/bin:$PATH"
fi
if [ -d /opt/homebrew/bin ]; then
    export PATH="/opt/homebrew/bin:$PATH"
fi
## Keep user bin directories first in PATH so they're preferred
if [ -d "$DOTFILES_DIR/bin" ]; then
    export PATH="$DOTFILES_DIR/bin:$PATH"
fi
## OS-specific binaries
if [[ $OSTYPE == 'darwin'* ]]; then
    if [[ -d "$DOTFILES_DIR/macbin" ]]; then
        export PATH="$DOTFILES_DIR/macbin:$PATH"
    fi
elif [[ $OSTYPE == 'linux'* ]]; then
    if [[ -d "$DOTFILES_DIR/linbin" ]]; then
        export PATH="$DOTFILES_DIR/linbin:$PATH"
    fi
fi
if [ -d "$HOME/.local/bin" ]; then
    export PATH="$HOME/.local/bin:$PATH"
fi
if [ -d "$HOME/bin" ]; then
    export PATH="$HOME/bin:$PATH"
fi

source "$HOME/.bash_functions"

# Set up brew if on macOS
if [[ $OSTYPE == 'darwin'* ]]; then
    if [ -f /opt/homebrew/bin/brew ]; then
        eval "$(/opt/homebrew/bin/brew shellenv)"
    fi
fi

# Prefer alacritty terminal if installed
if program_installed alacritty; then
    export TERMINAL=alacritty
fi

# Prefer neovim, if installed
if program_installed nvim; then
    export EDITOR=nvim
    alias vim=nvim
fi

if program_installed powerline; then
    # Make powerline support Python virtualenvs
    export POWERLINE_COMMAND="$DOTFILES_DIR/scripts/powerline-wrapper.sh"
fi

# alias make='nocorrect make'
# alias git='nocorrect git'
# Crontab
#alias crontab-e='vim ~/.crontab && crontab ~/.crontab'

#alias sl="sl -laF"

# Put flatpak apps in XDG
if program_installed flatpak; then
    export XDG_DATA_DIRS="/var/lib/flatpak/exports/share:$XDG_DATA_DIRS"
fi

# pyenv paths
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

# Make sure ssh-agent is available 
if [ -n "$LOAD_SSH_KEYS" ] && [ "$LOAD_SSH_KEYS" -eq 1 ]; then
    if program_installed ssh-add && [ ! -z "$SSH_AUTH_SOCK" ] && [ -d "$SSH_AUTH_SOCK" ] > /dev/null 2>&1 ; then
        # Add SSH keys to agent if not already added
        ## Find existing keys, supporting one layer of folder organization
        public_keys=($(find "$HOME/.ssh/" -maxdepth 2 -name "*.pub"))
        if [ ${#public_keys[@]} -gt 0 ]; then
            for public_key in "${public_keys[@]}"; do
                if [[ $(uname -s) == Linux* ]]; then
                    # Add SSH keys only if not already present
                    ssh-add -l | grep -q `ssh-keygen -lf "$public_key"  | awk '{print $2}'` || ssh-add -k
                    break
                elif [[ $(uname -s) == Darwin* ]]; then
                    # Add SSH keys only if not already present
                    ssh-add -l | grep -q `ssh-keygen -lf "$public_key"  | awk '{print $2}'` || ssh-add --apple-use-keychain
                    break
                fi
            done
        fi
    fi
fi

if program_installed i3; then
    # Set QT environment variables
    export QT_QPA_PLATFORMTHEME="qt5ct"
fi

# Extra domains dejima's firewall should allow beyond its defaults
export ALLOWED_DOMAINS=""

# Gitea host, used by bin/gitea-pr and dejima's git-push-guard hook
export GITEA_SSH_HOST="gitbox.bradina.localdomain"
export GITEA_API="https://gitbox.branderson.io/api/v1"
