# -*- mode: sh -*-
# shellcheck shell=bash

### Configuration

# History
shopt -s histappend

# Disable C-s / C-q for XOFF/XON
stty start undef
stty stop undef

### Load stuff

# Machine local scripts
if [ -f "${HOME}/.config/bash/rc.local" ]; then
    # shellcheck source=/dev/null
    . "${HOME}/.config/bash/rc.local"
fi

# Functions
# shellcheck source=bash/functions
. "${HOME}/.config/bash/functions"
if [ -f "${HOME}/.config/bash/functions.work" ]; then
    # shellcheck source=/dev/null
    . "${HOME}/.config/bash/functions.work"
fi

# Aliases
# shellcheck source=bash/aliases
. "${HOME}/.config/bash/aliases"

# Environment
loadenv "${HOME}/.config/environment.d/"*.conf

# Ensure histfile directory exists
mkdir -p "$(dirname "${HISTFILE}")"

# Ensure GPG directory exists
if [ ! -d "${GNUPGHOME}"  ]; then
    mkdir -m 0700 "${GNUPGHOME}"
fi

# Set up PS1

# shellcheck source=bash/prompt
. "${HOME}/.config/bash/prompt"

case "${TERM}" in
    *-256color|eat-truecolor) mb-fancy-prompt ;;
    *)                        mb-lofi-prompt  ;;
esac

# Eat terminal
if [ -v INSIDE_EMACS ] && [ -d "${EAT_SHELL_INTEGRATION_DIR}" ]; then
    echo "Loading eat integration ..."
    # shellcheck source=/dev/null
    source "${EAT_SHELL_INTEGRATION_DIR}/bash"
fi

if [ "${TERM}" = "eat-truecolor" ]; then
    function ssh {
        env TERM=xterm-256color ssh "${@}"
    }
fi

# Completions
for comp in "${HOME}/.config/bash_completion.d/"*; do
    if [ -f "${comp}" ] ; then
        # shellcheck source=/dev/null
        . "${comp}"
    fi
done

### Complete Aliases
if [ "$(type -t _complete_alias)" = "function" ]; then
    for a in $(alias | cut -d "=" -f 1 | cut -d " " -f 2); do
        complete -F _complete_alias "${a}"
    done
fi

### uv

# Add uv tool man-files to MANPATH
MANPATH="$(uv-manpaths | paste -sd ':'):"
export MANPATH

### Minikube

# Hack around minikube not supporting the XDG base directory spec
mkdir -p "${MINIKUBE_HOME}/minikube"
ln -snf "${MINIKUBE_HOME}/"{,.}minikube
ln -snf "${HOME}/.config/minikube" "${MINIKUBE_HOME}/.minikube/config"

### direnv
eval "$(direnv hook bash)"

if [ -n "${EAT_SHELL_INTEGRATION_DIR}" ]; then
    direnv reload 2> >(grep -v "direnv: error .envrc not found" >&2)
fi

### FZF
. /usr/share/fzf/shell/key-bindings.bash
