#!/bin/sh
#
# My shell profile
#
# Copyright (c) 2017-2022 Jacob Babor
# Licensed under the terms of the MIT license
#

## SHELLCHECK
# Yeah whatever I don't care that you can't find these
# shellcheck disable=1091
# shellcheck disable=1090
# I'm well aware of when functions are defined vs used
# Those choices are deliberate
# shellcheck disable=2139
# shellcheck disable=2016
# Some variables will not be used here, but in the shell
# shellcheck disable=2034
# Quit being pedantic
# shellcheck disable=1117

# Environment variables
_baseshell="$(basename -- "$0")"

# Patch PATH
for dir in \
	"$HOME/.bin" \
	"$HOME/.local/bin" \
	"/usr/local/opt/coreutils/libexec/gnubin" \
	"/usr/local/opt/gnu-sed/libexec/gnubin" \
	"/usr/local/opt/grep/libexec/gnubin" \
	"/usr/local/opt/util-linux/bin" \
	"/usr/local/opt/util-linux/sbin"
do
	if [ -d "$dir" ]; then
		PATH="$dir:$PATH"
	fi
done

# Grab bash_completion, if it exists
[ -f "/etc/profile.d/bash_completion.sh" ] && . "/etc/profile.d/bash_completion.sh"

# Source ~/.functions, if it exists
[ -r "$HOME/.functions" ] \
	&& [ "$_baseshell" != "sh" ] \
	&& [ "$_baseshell" != "dash" ] \
	&& . "$HOME/.functions"

# Define a require function
has() {
	[ -z "$1" ] && return 1
	command -v "$1" > /dev/null 2>&1
}

# Source in homebrew configuration, if we have it installed
brewfix=/usr/local
# This patch is needed on Apple silicon
if [ -d /opt/homebrew ]; then
	brewfix="/opt/homebrew"
fi
if [ -f "$brewfix"/bin/brew ]; then
	eval "$("$brewfix"/bin/brew shellenv)"
	# Add coreutils to path, if we can
	if [ -d "$brewfix"/opt/coreutils/libexec/gnubin ]; then
		export PATH="$brewfix/opt/coreutils/libexec/gnubin:$PATH"
	fi
fi
unset brew

# If we have toolbox, assume we're on Wayblue and set up an alias
if has toolbox; then
	alias hyprpm='toolbox run -c hyprpm env HYPRLAND_INSTANCE_SIGNATURE="$HYPRLAND_INSTANCE_SIGNATURE" hyprpm'
fi

# Grab pip completion, if it exists
if has pip; then
	case "$_baseshell" in
		*bash)
			if ! [ -f "$HOME/.pip-completion-bash" ]; then
				pip completion --bash > "$HOME/.pip-completion-bash"
				echo ".profile: Created pip completion for bash"
			fi
			. "$HOME/.pip-completion-bash"
			;;
		zsh)
			if ! [ -f "$HOME/.pip-completion-zsh" ]; then
				pip completion --zsh > "$HOME/.pip-completion-zsh"
				echo ".profile: Created pip completion for zsh"
			fi
			. "$HOME/.pip-completion-zsh"
			;;
		*)
			;;
	esac
fi

# Grab dircolors, if it exists
if has dircolors; then
	dircolorsfile="$HOME/.config/dircolors"
	if [ -r "$dircolorsfile" ]; then
		eval "$(dircolors "$dircolorsfile")"
	else
		eval "$(dircolors -b)"
	fi
fi

# Set up our text editor
for editor in vim vi nano; do
	if has "$editor"; then
		export EDITOR="$editor"
		break
	fi
done

# Now for a shitton of aliases
if has podman and ! has docker; then
	alias docker="podman"
fi
if has git; then
	# Thanks Bash-it!
	alias g='git'
	alias ga='git add'
	alias gall='git add -A'
	alias gap='git add -p'
	alias gb='git branch'
	alias gbD='git branch -D'
	alias gba='git branch -a'
	alias gbd='git branch -d'
	alias gbm='git branch -m'
	alias gbt='git branch --track'
	alias gc='git commit -v'
	alias gca='git commit -v -a'
	alias gcam="git commit -v -am"
	alias gcb='git checkout -b'
	alias gci='git commit --interactive'
	alias gcl='git clone'
	alias gclean='git clean -fd'
	alias gcm='git commit -v -m'
	alias gco='git checkout'
	alias gcob='git checkout -b'
	alias gcom='git checkout master'
	alias gcount='git shortlog -sn'
	alias gcp='git cherry-pick'
	alias gcpd='git checkout master; git pull; git branch -D'
	alias gcpx='git cherry-pick -x'
	alias gcsam="git commit -S -am"
	alias gct='git checkout --track'
	alias gd='git diff'
	alias gdel='git branch -D'
	alias gds='git diff --staged'
	alias gdv='git diff -w "$@" | vim -R -'
	alias get='git'
	alias gexport='git archive --format zip --output'
	alias gf='git fetch --all --prune'
	alias gft='git fetch --all --prune --tags'
	alias gftv='git fetch --all --prune --tags --verbose'
	alias gfv='git fetch --all --prune --verbose'
	alias ggs="gg --stat"
	alias ggui="git gui"
	alias gh='cd "$(git rev-parse --show-toplevel)"'
	alias gl='git pull'
	alias gll='git log --graph --pretty=oneline --abbrev-commit'
	alias glum='git pull upstream master'
	alias gm="git merge"
	alias gmu='git fetch origin -v; git fetch upstream -v; git merge upstream/master'
	alias gmv='git mv'
	alias gp='git push'
	alias gpatch="git format-patch -1"
	alias gpo='git push origin'
	alias gpom='git push origin master'
	alias gpp='git pull && git push'
	alias gpr='git pull --rebase'
	alias gpristine='git reset --hard && git clean -dfx'
	alias gpu='git push --set-upstream'
	alias gpuo='git push --set-upstream origin'
	alias gpuoc='git push --set-upstream origin $(git symbolic-ref --short HEAD)'
	alias gr='git remote'
	alias gra='git remote add'
	alias grm='git rm'
	alias grv='git remote -v'
	alias gs='git status'
	alias gsl="git shortlog -sn"
	alias gss='git status -s'
	alias gst="git stash"
	alias gstb="git stash branch"
	alias gstd="git stash drop"
	alias gstl="git stash list"
	alias gstp="git stash pop"
	alias gsu='git submodule update --init --recursive'
	alias gt="git tag"
	alias gta="git tag -a"
	alias gtd="git tag -d"
	alias gtl="git tag -l"
	alias gtls='git tag -l | sort -V'
	alias gup='git fetch && git rebase'
	alias gus='git reset HEAD'
	alias gwc="git whatchanged"
	# Add uncommitted and unstaged changes to the last commit
	alias gcaa="git commit -a --amend -C HEAD"
	# From http://blogs.atlassian.com/2014/10/advanced-git-aliases/
	# Show commits since last pull
	alias gnew="git log HEAD@{1}..HEAD@{0}"
	# Show untracked files
	alias gu='git ls-files . --exclude-standard --others'
fi
if has sed && has find; then
	replace() {
		if [ $# -ne 2 ]; then
			echo "replace: Requires two arguments"
			return 1
		fi
		find . -type f -exec sed -i "s/$1/$2/g" {} \;
	}
fi
if has sudo; then
	case $_baseshell in
		*bash|*zsh)
			export SUDO_PROMPT=$'[\e[34msudo\e[0m as %U]: Password for %p: '
			;;
		*)
			export SUDO_PROMPT='[sudo as %U]: Password for %p: '
	esac
fi
if has tree; then
	treeargs='-qF --dirsfirst'
	alias tree="tree $treeargs"
	alias t="tree $treeargs -L 2"
	alias tl="tree $treeargs -pughL 2"
	alias tp="tree $treeargs -pL 2"
	alias ts="tree $treeargs -hL 2"
	unset treeargs
fi

# Aliases for common utilities
if [ "$(uname)" = "Linux" ] || has brew; then
	# Assume we have GNU coreutils
	lsarguments='-Fh --color=auto --group-directories-first'
	alias l="ls -l --file-type $lsarguments"
	alias la="ls -A --file-type $lsarguments"
	alias ls="ls $lsarguments"
	alias ll="ls -Al --file-type $lsarguments"
	unset lsarguments
else
	# Else only assume POSIX/BSD
	lsarguments='-F'
	alias l="ls -l $lsarguments"
	alias la="ls -A $lsarguments"
	alias ls="ls $lsarguments"
	alias ll="ls -Ahl $lsarguments"
fi

# Oneoffs and weird oneliners
if [ "$(uname)" = "Linux" ]; then
	# Shows realtime and low-nice processes
	alias rtps="LIBPROC_HIDE_KERNEL=1 ps -eo pid,class,rtprio,ni,stat,comm --sort=+pcpu | awk '\$4!=0.0 {print}' | awk 'NR>1'"
fi

# Set up a default PS1 for bash
_ps1bash() {
	exitcode="$?"
	r="\[\e[0m\]"
	bg_red="\[\e[41m\]"
	bg_yellow="\[\e[43m\]"
	bg_blue="\[\e[44m\]"
	fg_black="\[\e[30m\]"
	fg_red="\[\e[31m\]"
	fg_green="\[\e[32m\]"
	fg_yellow="\[\e[33m\]"
	fg_blue="\[\e[34m\]"
	fg_magenta="\[\e[35m\]"
	fg_grey="\[\e[37m\]"
	fg_bold="\[\e[1m\]"

	# Add hostname prefix in SSH sessions
	unset _prefix
	# Get the system hostname
	if [ -f /etc/hostname ]; then
		_hostname="$(</etc/hostname)"
	else
		_hostname="$(hostname)"
	fi
	# Alert if in an SSH session
	if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
		_prefix="${fg_bold}${fg_red}${_hostname}${r}${fg_red}:${r}${_prefix}"
	elif [ -n "$TOOLBOX_PATH" ]; then
		_prefix="${fg_bold}${fg_magenta}$(. /run/.containerenv && echo "$name")${r}${fg_magenta}:${r}${_prefix}"
	elif [ "$USER" != "salt" ]; then
		_prefix="${fg_bold}${fg_yellow}$USER${r}${fg_yellow}:${r}${_prefix}"
	fi
	# If we're in a PROJECT, post that envvar
	if [ -n "$PROJECT" ]; then
		_prefix="${_prefix}${fg_bold}${fg_blue}$PROJECT${r}${fg_blue}:${r}"
	fi
	# Color PWD
	_prefix="${_prefix}${fg_blue}"
	# Show the tilde instead of $HOME
	_cpwd='\w'
	# Show read-only status in certain directories
	if [ "$UID" = 0 ]; then
		# Root gets the usual "#" prompt
		_prompt="${fg_red}#${r}"
	elif ! [ -d "$PWD" ]; then
		# Something very bad has happened to our PWD, probably got deleted
		_prompt="${bg_red}${fg_black}!${r}"
	elif ! [ -r "$PWD" ]; then
		# Guess we lost privileges
		_prompt="${fg_red}"'$'"${r}"
	elif ! [ -x "$PWD" ]; then
		# Missing execution perms
		if [ -w "$PWD" ]; then
			# Fixable
			_prompt="${bg_blue}${fg_black}"'$'"${r}"
		else
			# Unfixable
			_prompt="${bg_yellow}${fg_black}"'$'"${r}"
		fi
	elif ! [ -w "$PWD" ]; then
		# Can't write is really common but also good to know
		_prompt="${fg_magenta}"'~'"${r}"
	else
		# Otherwise we should be fine
		_prompt="${fg_green}"'$'"${r}"
	fi
	# Alert us if the last command failed
	unset _fail
	if ! [ "$exitcode" = "0" ]; then
		_fail="${fg_bold}${fg_red}?"
	fi
	# shellcheck disable=2059
	PS1="[${_prefix}${_cpwd}${r}]${_fail}${r}${_prompt}${r} "
}

# And export our PS1
case "$_baseshell" in
	zsh)
		# Don't do this on ZSH
		# I have a custom theme for that
		;;
	*bash)
		export PS1=""
		export PROMPT_COMMAND=_ps1bash
		;;
	*)
		export PS1='[\e[31m\w\e[0m]\e[32m\$\e[0m '
		;;
esac

# Clean up
unset -v _baseshell
unset -f has

# Source in a site-specific profile
localprofile="$HOME/.local/profile"
if [ -f "$localprofile" ]; then
	. "$localprofile"
fi