################################################################################
#                                                                              #
#   ● ○ ○ ○ ●                                                                  #
#   ● ● ○ ● ●   Mike Ochmann                                                   #
#   ● ○ ● ○ ●   https://mike-ochmann.de                                        #
#   ● ○ ○ ○ ●                                                                  #
#   ● ○ ○ ○ ●                                                                  #
#                                                                              #
#   My configuration for the Bourne Again Shell                                #
#   Version 1.5.2                                                              #
#                                                                              #
################################################################################


# Do not use this .bashrc if bash is not running in interactive mode
case $- in
    *i*) ;;
      *) return;;
esac

# HISTORY
HISTCONTROL=ignoreboth:erasedups  # don't put duplicate lines or lines starting with space in the history
HISTIGNORE="ls:bg:fg:history"     # Hist ignores exact match
HISTTIMEFORMAT="%F %T "           # add timestamp
HISTSIZE=10000                    # History size
HISTFILESIZE=20000                # History file size

shopt -s histappend               # Append to the history file, don't overwrite it
shopt -s cmdhist                  # Save multi-line histroy as one line
shopt -s checkwinsize             # Update columns/lines after commands
shopt -sq autocd                  # Can change dir without `cd`
shopt -s cdspell                  # Fixes minor spelling errors in cd paths
shopt -s no_empty_cmd_completion  # Stops empty line tab comp
shopt -sq dirspell                # Tab comp can fix dir name typos


# PATH
export PATH="/usr/local/opt/ncurses/bin:$PATH"
export PATH="/usr/local/bin/:/Users/mochmann/.cargo/bin:$PATH"
export PATH="/usr/local/Cellar:$PATH"
export PATH="$PATH:/Users/mochmann/.dotnet/tools"
export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"
export PATH="/usr/local/opt/libpq/bin:$PATH"
export CC=clang
export CXX=clang++
export PATH="/usr/local/lib/python3.9/site-packages:$PATH"
if [[ "$OSTYPE" == "darwin"* ]]
then
	export PATH="/Applications/Emacs.app/Contents/MacOS:$PATH"
fi


# COLORS

## enabling directory colors if possible
if [ -x /usr/bin/dircolors ] || [ -x /usr/local/bin/gdircolors ]
	then
	## `dircolors` is called `gdircolors` if installed via homebrew
	if [[ "$OSTYPE" == "darwin"* ]]
		then
		dircolors() {
			gdircolors "$@"
		}
		shopt -s expand_aliases
	fi
	if [ ! -f ~/.dircolors ]
		then
		dircolors -p > ~/.dircolors
	fi
	test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
	if [[ "$OSTYPE" == "darwin"*  ]]
		then
		alias ls="ls -G"
	else
		alias ls="ls --color=auto"
		alias grep="grep --color=auto"
		alias fgrep="fgrep --color=auto"
		alias egrep="egrep --color=auto"
		#alias dir="dir --color=auto"
		#alias vdir="vdir --color=auto"
	fi
fi

__prompt_command() {
	local EXIT="$?"
	## promp colors

	## Regular                  Bold                        Underline                   High Intensity              BoldHigh Intensity          Background High Intensity   Backgrounds
	local Bla='\[\e[0;30m\]';	local BBla='\[\e[1;30m\]';	local UBla='\[\e[4;30m\]';	local IBla='\[\e[0;90m\]';	local BIBla='\[\e[1;90m\]';	local On_Bla='\e[40m';	local On_IBla='\[\e[0;100m\]';
	local Red='\[\e[0;31m\]';	local BRed='\[\e[1;31m\]';	local URed='\[\e[4;31m\]';	local IRed='\[\e[0;91m\]';	local BIRed='\[\e[1;91m\]';	local On_Red='\e[41m';	local On_IRed='\[\e[0;101m\]';
	local Gre='\[\e[0;32m\]';	local BGre='\[\e[1;32m\]';	local UGre='\[\e[4;32m\]';	local IGre='\[\e[0;92m\]';	local BIGre='\[\e[1;92m\]';	local On_Gre='\e[42m';	local On_IGre='\[\e[0;102m\]';
	local Yel='\[\e[0;33m\]';	local BYel='\[\e[1;33m\]';	local UYel='\[\e[4;33m\]';	local IYel='\[\e[0;93m\]';	local BIYel='\[\e[1;93m\]';	local On_Yel='\e[43m';	local On_IYel='\[\e[0;103m\]';
	local Blu='\[\e[0;34m\]';	local BBlu='\[\e[1;34m\]';	local UBlu='\[\e[4;34m\]';	local IBlu='\[\e[0;94m\]';	local BIBlu='\[\e[1;94m\]';	local On_Blu='\e[44m';	local On_IBlu='\[\e[0;104m\]';
	local Pur='\[\e[0;35m\]';	local BPur='\[\e[1;35m\]';	local UPur='\[\e[4;35m\]';	local IPur='\[\e[0;95m\]';	local BIPur='\[\e[1;95m\]';	local On_Pur='\e[45m';	local On_IPur='\[\e[0;105m\]';
	local Cya='\[\e[0;36m\]';	local BCya='\[\e[1;36m\]';	local UCya='\[\e[4;36m\]';	local ICya='\[\e[0;96m\]';	local BICya='\[\e[1;96m\]';	local On_Cya='\e[46m';	local On_ICya='\[\e[0;106m\]';
	local Whi='\[\e[0;37m\]';	local BWhi='\[\e[1;37m\]';	local UWhi='\[\e[4;37m\]';	local IWhi='\[\e[0;97m\]';	local BIWhi='\[\e[1;97m\]';	local On_Whi='\e[47m';	local On_IWhi='\[\e[0;107m\]';
	local Mag='\[\e[0;95m\]';

	local RCol='\[\e[0m\]'	# Text Reset

	PS1="${debian_chroot:+(${Red}$debian_chroot${RCol})}"

	if [ $UID -eq "0" ]
		then
		PS1+="${BRed}\u${RCol}"
	else
		PS1+="${BGre}\u${Mag}"
	fi
	PS1+="${Pur}@\h "
	local PSCol="$BBlu"
	
	PS1+="${PSCol}\w${RCol}"	# Current working dir
	if [ $EXIT != 0 ]
		then
		## can add `kill -l $?` to test to filter backgrounded
		PS1+=" [${BRed}${EXIT}${RCol}]"	# Add exit code, if non 0
	fi

	## Backgrounded running jobs
	local BKGJBS=$(jobs -r | wc -l | tr -d ' ')
	if [ ${BKGJBS} -gt 2 ]
		then
		PS1+=" ${Red}[bg:${BKGJBS}]${RCol}"
	elif [ ${BKGJBS} -gt 0 ]
		then
		PS1+=" ${Yel}[bg:${BKGJBS}]${RCol}"
	fi

	## Stopped Jobs
	local STPJBS=$(jobs -s | wc -l | tr -d ' ')
	if [ ${STPJBS} -gt 2 ]
		then
		PS1+=" ${Red}[stp:${STPJBS}]${RCol}"
	elif [ ${STPJBS} -gt 0 ]
		then
		PS1+=" ${Yel}[stp:${STPJBS}]${RCol}"
		if  [ $UID -eq "0" ]
			then
			PS1+=" ${BRed}# ${RCol}"	## End of PS1
		else
			PS1+=" ${BBlu}\$ ${RCol}"	## End of PS1
		fi
	fi

	local branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
	if [ "$branch" != "" ]
		then
		branch="${Whi}⎇${Cya} $branch"
	fi
	PS1+=" ${branch}${Yel} ❯ ${RCol}"
}

#PS1='[\u]@\H \w [\$?] \\$ \[$(tput sgr0)\]'
export TERM=xterm-256color
export PROMPT_COMMAND=__prompt_command  # Func to gen PS1 after CMDs

## directory coloring
export LS_COLORS='di=35:ln=36:so=30;46:pi=30;43:ex=33:bd=30;45:cd=30;42:su=31;40:sg=30;41:tw=35:ow=35'


# INTERNALS

# enable bash completion if nessecary
if ! shopt -oq posix
	then
	if [ -f /usr/share/bash-completion/bash_completion ]
		then
		. /usr/share/bash-completion/bash_completion
	elif [ -f /etc/bash_completion ]; then
		. /etc/bash_completion
	fi
fi

## git completion for macOS
if [[ "$OSTYPE" == "darwin"* ]]
	then
	[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion || {
    	[ -f "$(brew --prefix)/etc/bash_completion.d/git-completion.bash" ] && \
        . $(brew --prefix)/etc/bash_completion.d/git-completion.bash
	}
fi

[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"

# FUNCTIONS
cd() {
	builtin cd "$@" && ls
}

man() {
	env \
	LESS_TERMCAP_mb=$(printf "\e[1;31m") \
	LESS_TERMCAP_md=$(printf "\e[1;31m") \
	LESS_TERMCAP_me=$(printf "\e[0m") \
	LESS_TERMCAP_se=$(printf "\e[0m") \
	LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
	LESS_TERMCAP_ue=$(printf "\e[0m") \
	LESS_TERMCAP_us=$(printf "\e[1;32m") \
	man "$@"
}

## goes up `n` directories
function up {
	[ "${1/[^0-9]/}" == "$1" ] && {
		local ups=""
		for i in $(seq 1 $1)
			do
			ups=$ups"../"
		done
		cd $ups
	} || echo "usage: up INTEGER"
}

alias passgen="cat /dev/urandom | strings | grep -o '[a-zA-Z0-9]' | head -n 30 | tr -d '\n'; echo"
ytAudio() {
	youtube-dl -x --audio-format mp3 $1
}

multipass() {
		  for n in $(seq 1 $1)
	do 
		echo $(passgen)
	done
}
nslookupr() {
	nslookup $(nslookup $1 | grep Address | sed -n 2p | cut -d " " -f2)
}
digfull() {
	dig +nocmd $1 any +multiline +noall +answer
}

sshttp() {
	if [ -z "$3" ]
		then
		port=80
	else
		port=$3
	fi

	ssh -L 9000:$1:$port $2
}

sshttps() {
	sshttp $1 $2 443
}

# ENVIRONMENT
NPM_PACKAGES="${HOME}/.npm-packages"

export PATH="$NPM_PACKAGES/bin:$PATH"
export EDITOR=vim

if  command -v manpath &> /dev/null 
	then
	unset MANPATH # delete if you already modified MANPATH elsewhere in your config
	export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
fi

export LDFLAGS="-L/usr/local/opt/libffi/lib"
export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"


# *ENV STUFF
if [ -x /usr/local/bin/anyenv ]
	then
	eval "$(anyenv init -)"
fi
if [ -x /usr/local/bin/rbenv ]
	then
	eval "$(rbenv init -)"
fi

# ALIASES
alias ll="ls -lisa"
alias la="ls -A"
alias l="ls -CF"
alias histgrep='history | grep $@'
alias batt="pkill -f indicator-power-service"
alias pgcp="/home/miko/bash/pgcopy"
alias sqlplus="rlwrap -c sqlplus"
alias atvim="vim"
alias nano="vim"
alias fullupdate="sudo apt update && sudo apt upgrade && sudo apt dist-upgrade"
alias ygrep=ytAudio
alias sv="sudo vim"
alias reload="source ~/.bashrc"
alias editrc="vim ~/.bashrc && reload"
alias shutdowncause="log show --predicate 'eventMessage contains \"Previous shutdown cause\"' --last 24h"
alias gl="glab"

# PROPELLER TMUX SSH
#if [ -z "$TMUX" ] && [ $(dpkg-query -W -f='${Status}' tmux 2>/dev/null | grep -c "ok installed") -eq 1 ] && [ -n "$SSH_TTY" ] && [ -z "$NO_AUTO_TMUX" ] && [[ $- =~ i ]]
if [ -z "$TMUX" ] && command -v tmux &> /dev/null && [ -n "$SSH_TTY" ] && [ -z "$NO_AUTO_TMUX" ] && [[ $- =~ i ]]
	then
	tmux attach-session -t $USER || tmux new-session -s $USER
	exit
fi