#!/usr/bin/env bash

# these are all paths used across many of the dotfiles and should be assumed to
# be loaded and properly set by every script - this means you are responsible
# for making sure they're loaded!
export XDG_CONFIG_HOME="$HOME/.config"
export DOTFILES_PATH="$XDG_CONFIG_HOME/dotfiles"
export ENV_DOTFILES_PATH="$DOTFILES_PATH/.env"
export EDFP="$ENV_DOTFILES_PATH" # shorter alias
NICE_HOME="$HOME"

# TODO: better logic for auto-detecting alternative home directories?
# 1. check dirname(basename $HOME)) matches username
# 2. check /home/$username
[[ $(basename "${HOME}") = "usr" ]] && NICE_HOME="$(realpath "$HOME/..")"
[[ $(basename "${HOME}") = ".home" ]] && NICE_HOME="$(realpath "$HOME/..")"
# TODO: nice home explicitly definable on a per-device (env) basis

export NICE_HOME
export NOTES_DIR="$NICE_HOME/doc/notes"

# set our PATH
source "$DOTFILES_PATH/shell/bash/paths"

# stop parsing on a non-interactive shell
[ -z "$PS1" ] && return

# load our key binds
case $- in
	*i*) bind -f "$DOTFILES_PATH/shell/inputrc";;
	*) ;;
esac

# import our aliases
source "$DOTFILES_PATH/shell/bash/aliases"

# import our autocompletions
source "$DOTFILES_PATH/shell/bash/autocompletions"

# load our vconsole colors
if [ "${TERM%%-*}" = 'linux' ] && [[ $- == *i* ]]; then
	BASE16_SHELL="$DOTFILES_PATH/scripts/colors/vconsole"
	[[ -s "$BASE16_SHELL" ]] && source "$BASE16_SHELL"
fi

# load our terminal colors
BASE16_SHELL="$DOTFILES_PATH/scripts/colors/shell"
[[ -s "$BASE16_SHELL" ]] && source "$BASE16_SHELL"

# disable ctrl-s terminal freeze
[[ $- == *i* ]] && stty -ixon

# allow ** recursive wildcard globbing
shopt -s globstar

# import our prompt
source "$DOTFILES_PATH/shell/bash/prompt"

# prevents binds or commands pulling from history from insta-sending, and
# instead places them in the readline for editing
shopt -s histverify

# always _append_ to bash history
shopt -s histappend

# prevents some Java GUI apps from not working or rendering properly due to
# using wacky window managers
export _JAVA_AWT_WM_NONREPARENTING=1

LS_COLORS='ow=01;36;40'
export LS_COLORS

# less tab size of 2 spaces
LESS="-x2"
export LESS

# set our EDITOR to neovim if we've got it
export EDITOR="vim"
if command -v nvim >/dev/null 2>&1; then
	alias vim="nvim"
	alias ovim="\\vim"
	export EDITOR="nvim"
fi

export TERMINAL="urxvtc"

export BROWSER="firefox-developer-edition"

# "unlimited" history
export HISTFILESIZE="10000000"
export HISTSIZE="10000000"

# ignore duplicates and commands starting with space (" ")
export HISTCONTROL=ignoreboth

# ignore certain commands
HISTIGNORE='ls:ll:la'

# ensure command history is comprised of single lines
shopt -s cmdhist

# load a per-device config last so anything can be overridden
if [ -a "$EDFP/bash" ]; then
	source "$EDFP/bash"
fi

# we assume the user uses "$HOME" to just store their mess of dotfiles and other
# nonsense that clutters it up and that they have a preferred starting
# directory where they keep the stuff they actually care about
# we only do this if the user is opening a shell at $HOME
if [ "$PWD" = "$HOME" ]; then
	cd "$NICE_HOME" || cd || return
fi

# TODO: check if fd command exists
export FZF_DEFAULT_COMMAND='fd --type f --hidden --follow --exclude .git'

export ERL_AFLAGS="-kernel shell_history enabled -kernel shell_history_file_bytes 1024000"

_make_paths

[ -f ~/.fzf.bash ] && source ~/.fzf.bash