# aitken.com default .bashrc

# --- Exit if non-interactive
case $- in
    *i*) ;;
    *) return ;;
esac

umask 022

if command -v lesspipe >/dev/null 2>&1; then
    eval "$(SHELL=/bin/sh lesspipe)"
elif command -v lesspipe.sh >/dev/null 2>&1; then
    eval "$(SHELL=/bin/sh lesspipe.sh)"
fi


# --- History Settings
HISTCONTROL=ignoredups:ignorespace
HISTSIZE=10000
HISTFILESIZE=20000
HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S '
#PROMPT_COMMAND="history -n; history -a"
PROMPT_COMMAND="history -a"


# --- Shell options
shopt -s cdspell
shopt -s checkwinsize
shopt -s cmdhist
shopt -s globstar
shopt -s histappend
shopt -s lithist
shopt -s no_empty_cmd_completion


# --- Empty for now, but put any OS-specific commands here.
case "$OSTYPE" in
    darwin*)
        PLATFORM='macOS'
        ;;
    freebsd*)
        PLATFORM='FreeBSD'
        ;;
    linux*)
        PLATFORM='Linux'
        ;;
    *)
        PLATFORM='Unknown'
        ;;
esac
export PLATFORM


# --- Environment settings that require bash
set_manwidth() {
    local cols=${COLUMNS:-80}
    [[ $cols -eq 0 ]] && cols=80

    MANWIDTH=$(( cols - 10 ))
    (( MANWIDTH > 120 )) && MANWIDTH=120
    (( MANWIDTH < 40 )) && MANWIDTH=40
    export MANWIDTH
}
trap set_manwidth SIGWINCH
set_manwidth


# --- Set a useful prompt of the form: "<user>@<host> <path>\n\$ "
#
# PS1_TITLE: Window title of the form: 📁 <user>@<host> <path>
# C_USERHOST: Color of the <user>@<host> portion of the prompt
# C_PATH: Color of the <path> portion of the prompt
# C_PROMPT: Color of the command prompt (% or #)
#
PS1_TITLE='' && [[ "$TERM" =~ ^(xterm|rxvt|screen|tmux) ]] && PS1_TITLE=$'\\[\\e]2;📁 \\u@\\h: ${PWD}\\a\\]'
C_RESET='\[\e[0m\]'
C_USERHOST='\[\e[1;38;5;36m\]'
C_PATH='\[\e[38;5;110m\]'
C_PROMPT='\[\e[38;5;226m\]'
PS1="${PS1_TITLE}\n${C_USERHOST}\u@\h:${C_RESET}${C_PATH}\${PWD}\n${C_RESET}${C_PROMPT}\\$ ${C_RESET}"


# --- Common command aliases/functions.
alias c='clear'
alias d='date'
alias diff='diff --color=auto'
alias df='df -h'
alias du='du -h'
eg() {
    shopt -s nocasematch
    while IFS='=' read -r key val; do
        [[ ! "$key" =~ $1 ]] && continue
        [[ ! "${key%%=*}" =~ "less_termcap" ]] && echo "${key}=${val}" && continue
        echo "${key}=$(x=$(declare -p ${key}); echo "${x#*=}")"
    done < <(env)
    shopt -u nocasematch
}
extract() {
    case "$1" in
        *.tar.gz|*.tgz) tar xzf "$1" ;;
        *.tar.bz2|*.tbz2) tar xjf "$1" ;;
        *.tar.xz) tar xJf "$1" ;;
        *.zip) unzip "$1" ;;
        *.gz) gunzip "$1" ;;
        *) echo "Don't know how to extract: $1" ;;
    esac
}
ff() {
	find . -iname "*$1*" -print
}
ffe() { 
	find . \( -path "./$1" -prune \) -o -iname "*$2*" -print
}
alias g='grep --color=auto'
alias ge='grep --color=auto -e'
alias gf='grep --color=auto -f'
alias gi='grep --color=auto -i'
alias gv='grep --color=auto -v'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias h='history'
alias l='$(type -p batcat bat | head -n1) -p --color=always'
ll() {
    command ls -AlFh --color=always "$@" | less -E
}
lld() {
    command ls -AlFhd --color=always "$@" | less -E
}
lls() {
    command ls -A --color=always "$@" | less -E
}
alias ls='ls --color=auto'
lt() {
    command ls -AlFht --color=always "$@" | less -E
}
mkcd() {
    mkdir -p "$1" && cd "$1"
}
alias m='make'
alias mc='make clean'
mktar() {
	tar cf "${1}.tar" "$1"
}
alias p='ping'
pushd() {
  builtin pushd "$@" > /dev/null
}
popd() {
  builtin popd "$@" > /dev/null
}
psg() {
    [[ -z "$1" ]] && command ps auxw && return 0
    command ps auxw | grep -i --color=always "[${1:0:1}]${1:1}"
}
alias src='. ~/.bash_profile'
command -v vim >/dev/null 2>&1 && alias vi=vim
alias vrc='${EDITOR:-vi} ~/.bashrc'


# --- Enable intelligent completion
for bc in \
    /opt/homebrew/etc/profile.d/bash_completion.sh \
    /usr/local/share/bash-completion/bash_completion.sh \
    /etc/bash_completion
do
    [ -r "$bc" ] && . "$bc" && break
done

