#!/usr/bin/env bash

# SPDX-License-Identifier: 0BSD
# Author: Imbus

# Simple, unobtrusive, dependency-free, minimal, resilient, self-contained

# Source global definitions
if [ -f /etc/bashrc ]; then
	# shellcheck source=/etc/bashrc
	source /etc/bashrc
fi

# Enable vi mode
set -o vi

export FZF_CTRL_R_OPTS="--reverse"

# These are placed differently depending on environment
FZFBINDS_CANDIDATES=(
	/usr/share/fzf/key-bindings.bash
	/usr/share/fzf/shell/key-bindings.bash        # Fedora
	/usr/share/doc/fzf/examples/key-bindings.bash # Debian
)

# Source the first available candidate
for FZFBINDS in "${FZFBINDS_CANDIDATES[@]}"; do
	# shellcheck source=/dev/null
	[ -f "$FZFBINDS" ] && source "$FZFBINDS" && break
done

# Shopt sets settings related to bash
shopt -s nocasematch  # Case insensitive matching
shopt -s autocd       # Type '$ dirname' to enter dirname (omitting cd)
shopt -s checkwinsize # Resize efter every command
shopt -s globstar     # Enable syntax like **/*.txt for recursive matching
shopt -s nullglob     # **/*.txt returns null if none found
shopt -s nocaseglob   # **/*.TxT
shopt -s cmdhist      # Stores multi-line commands in the history as a single entry.
shopt -s histappend   # Append to the history file, not overwrite it

# Write history immediately
export HISTCONTROL=ignoredups:ignorespace # Avoid duplicate entries and leading spaces
export HISTSIZE=10000                     # Increase history size if necessary
export HISTFILESIZE=10000                 # Increase history file size if necessary

# Write to history immediately after each command
PROMPT_COMMAND=(
	'history -a' # Append history immediately after each command
	'history -n' # Read the new history from the history file
)

if [ -f /var/run/reboot-required ] || [ -f /run/reboot-required ]; then
	echo 'A reboot is required...'
fi

# Bind sets readline-specific settings. These are also set in ~/.inputrc
bind 'set completion-ignore-case on'

# Enable prefix-based history search
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'

add_to_path() {
	for dir in "$@"; do
		if ! [[ ":$PATH:" == *":$dir:"* ]]; then
			PATH="$dir:$PATH"
		fi
	done
}

# User-specific environment
add_to_path \
	"$HOME/.local/bin" \
	"$HOME/bin" \
	"$HOME/scripts" \
	"$HOME/.go/bin" \
	"$HOME/.cargo/bin" \
	"$HOME/.zig" \
	"$HOME/.xpack-riscv-none-elf-gcc/bin"

export GOPATH="$HOME/.go"

if [ -f "$HOME/.bash_aliases" ]; then
	# shellcheck source=/dev/null
	source "$HOME/.bash_aliases"
fi

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# User specific aliases and functions
if [ -d ~/.bashrc.d ]; then
	for rc in ~/.bashrc.d/*; do
		if [ -f "$rc" ]; then
			# shellcheck source=/dev/null
			source "$rc"
		fi
	done
fi

start() {
	if [ $# -eq 0 ]; then
		echo "Usage: start <file_or_directory>"
		return 1
	fi

	for target in "$@"; do
		if [ -e "$target" ]; then
			nohup xdg-open "$target" >/dev/null 2>&1 &
			disown >/dev/null 2>&1
		else
			echo "start: '$target' not found."
		fi
	done
}

# Make manpages more readable
export MANWIDTH=120

if [ -f "$HOME/.cargo/env" ]; then
	# shellcheck source=/dev/null
	source "$HOME/.cargo/env"
fi

unset rc
