# shellcheck shell=bash
# exports.sh — environment, XDG paths, history, colors, stow-friendly
# Repo path: bashrc/.config/bash/exports.sh -> stows to ~/.config/bash/exports.sh
# Sourced from ~/.bashrc via: source_shell_file "${XDG_CONFIG_HOME:-$HOME/.config}/bash/exports.sh"

[[ $- != *i* ]] && return 0

# Fallback if sourced standalone (normally provided by .bashrc)
declare -F has_tool >/dev/null 2>&1 || has_tool() { command -v "$1" >/dev/null 2>&1; }

#######################################################
# XDG BASE DIRECTORY SPECIFICATION
#######################################################

# set up XDG folders
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"

#######################################################
# HISTORY CONFIGURATION
#######################################################

# Expand the history size and specify history file location.
export TERM="${TERM:-xterm-256color}"
export HISTFILESIZE=10000
export HISTSIZE=10000
export HISTFILE="${XDG_STATE_HOME}/bash/history"
export HISTTIMEFORMAT="%F %T" # add timestamp to history
export HISTORY_IGNORE="(ls|cd|pwd|exit|sudo reboot|history|cd -|cd ..)"

# Don't put duplicate lines in the history and do not add lines that start with a space
export HISTCONTROL=erasedups:ignoredups:ignorespace

# Check the window size after each command and, if necessary, update the values of LINES and COLUMNS
shopt -s checkwinsize
shopt -s autocd
shopt -s cdspell
shopt -s cmdhist
shopt -s dotglob
shopt -s histappend
PROMPT_COMMAND='history -a'

# Ensure the history directory exists
[[ ! -d "${XDG_STATE_HOME}/bash" ]] && mkdir -p "${XDG_STATE_HOME}/bash"

#######################################################
# COLORS AND DISPLAY
#######################################################

# To have colors for ls and all grep commands such as grep, egrep and zgrep
export CLICOLOR=1
export LS_COLORS='no=00:fi=00:di=00;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:*.xml=00;31:'

# Color for manpages in less makes manpages a little easier to read
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'
