#!/bin/sh

# Copyright (c) 2017 rehashedsalt/vintagesalt
# Licensed under the terms of the MIT license

## POSIX NOTICE
# This script, or at least the parts expected to be run by a standard sh
# implementation, should be fully POSIX-compliant. If it is not, open a bug
# report at gitlab.com/rehashedsalt/home and I'll take care of it.

## SHELLCHECK
# Not finding these sources is none of my concern; they're out of scope
# 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
desiredpath="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/games:/usr/games:$HOME/.bin:$HOME/.local/bin"
IFS=":"
for path in $desiredpath; do
	PATH="${PATH#*$path}"
done
unset IFS
while [ "${PATH#:}" != "$PATH" ]; do
	PATH="${PATH#:}"
done
PATH="$desiredpath:$PATH"

# 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
}

# 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

# Set up go, if we have it
if has go; then
	export GOPATH="$HOME/.local/go"
	[ "${PATH#*$GOPATH}" = "$PATH" ] && export PATH="$PATH:$GOPATH/bin"
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
alias e='$EDITOR'

# Now for a shitton of aliases
if ! has define; then
	if has mate-dictionary; then
		alias define='mate-dictionary -n --look-up'
	fi
fi
if has emerge; then
	alias e-depclean='sudo emerge -a --depclean'
	alias e-inst='sudo emerge -a --jobs --tree --quiet-build y'
	alias e-upgrade='sudo emerge -DNUua --jobs --tree --quiet-build y --with-bdeps=y --keep-going --backtrack=1000 @world'
	alias e-newuse='sudo emerge -Uva --jobs --tree --quiet-build y @world'
	alias e-search='emerge -s'
	alias e-sync='sudo emerge --sync'
	if has eclean; then
		alias e-cleanup='sudo eclean -d distfiles && sudo eclean -d packages'
	fi
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 instantmusic; then
	alias song='instantmusic -p -s'
	alias songp='instantmusic -s'
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

# Dotfile management aliases
if [ -d "$HOME/.dotfiles" ]; then
	dotcmd='git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
	alias dot="$dotcmd"
	alias dotupdate="\
		printf 'Pulling...\n'; \
		$dotcmd pull
		printf 'Updating submodules...\n'; \
		$dotcmd submodule update --init; \
		printf 'Checking out masters...\n'; \
		$dotcmd submodule -q foreach --recursive 'git checkout -q master && git pull' && \
		$dotcmd status"
	unset dotcmd
fi

# Aliases for common utilities
if [ "$(uname)" = "Linux" ]; then
	# Assume we have GNU coreutils
	lsarguments='--color=auto --group-directories-first'
	alias l="ls -hlF  --file-type $lsarguments"
	alias la="ls -AF  --file-type $lsarguments"
	alias ls="ls -F $lsarguments"
	alias ll="ls -AhlF  --file-type $lsarguments"
	unset lsarguments

	alias rm='rm -I'
else
	# Else only assume POSIX/BSD
	lsarguments='-F'
	alias l="ls -$lsarguments"
	alias la="ls -A $lsarguments"
	alias ls="ls $lsarguments"
	alias ll="ls -Ahl $lsarguments"
fi
alias cp='cp -i'
alias d='du -hs'
alias todo='$EDITOR "$HOME/Documents/todo"'

# Set up a default PS1
# This *should* work for all terminals. I know it works on ksh
_ps1() {
	exitcode="$?"
	r="\e[0m"
	fg_blue="\e[34m"
	fg_red="\e[31m"
	fg_green="\e[32m"
	fg_yellow="\e[33m"
	fg_grey="\e[37m"
	fg_bold="\e[1m"

	# Add hostname prefix in SSH sessions
	# Yes, shellcheck. I get it. That's what the or is for
	#shellcheck disable=2039
	if [ "$HOSTNAME" = "phys-saltshaker" ] || [ "$(hostname)" = "phys-saltshaker" ]; then
		prefix="${fg_bold}${fg_grey}ss${r}${fg_grey}:${r}"
	fi
	if [ "$SSH_CLIENT" ]; then
		prefix="${fg_bold}${fg_red}$(hostname)${r}${fg_red}:${r}${prefix}"
	fi
	# Append "vim" in Vim
	if [ -n "$VIM_TERMINAL" ]; then
		prefix="${fg_bold}${fg_green}*${r}"
	fi
	# Change PWD color depending on the shell
	case $0 in
		*bash)
			prefix="${prefix}${fg_blue}"
			;;
		/bin/ksh)
			prefix="${prefix}${fg_green}"
			;;
		*)
			;;
	esac
	# Show the tilde instead of $HOME
	if [ "${PWD#$HOME}" != "$PWD" ]; then
		cpwd="~${PWD#$HOME}"
	else
		cpwd="$PWD"
	fi
	# Alert us if the last command failed
	unset fail
	if ! [ "$exitcode" = "0" ]; then
		fail="${fg_bold}${fg_red}?"
	fi
	# shellcheck disable=2059
	printf "[${prefix}${cpwd}${r}]${fail}${r}${fg_green}\$${r} "
}

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

# Clean up
unset -v _baseshell
unset -f has