# If not running interactively, don't do anything
case $- in
	*i*) ;;
	*) return;;
esac

# Some stuff shouldn't be in this file
[ -e ~/.bash_secrets ] && source ~/.bash_secrets

# History stuff
export HISTCONTROL=ignoreboth
shopt -s histappend
export HISTSIZE=1000000
export HISTFILESIZE=10000000
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

# Opts
shopt -s checkwinsize
bind "set completion-ignore-case on"
bind "set show-all-if-ambiguous on"

# Can we just pretend literally everything is xterm-256color
export TERM=xterm-256color

# Default stuff
export EDITOR=nvim

# Color config and aliases
if [ -x /usr/bin/dircolors ]; then
	test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
	alias ls='ls --color=auto'
	alias grep='grep --color=auto'
	alias fgrep='fgrep --color=auto'
	alias egrep='egrep --color=auto'
fi

# Aliases
alias vim=nvim
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias rscp='rsync -rclh --progress'
alias gdiff='diff -urN --color=always'
alias gcomm='git log | head -n 1'

# Copy stdin to clipboard
cbcopy() {
	if [ -z "$WAYLAND_DISPLAY" ]; then
		sed -z '$ s/\n$//' | xclip -i -selection clipboard
	else
		sed -z '$ s/\n$//' | wl-copy
	fi
}

# Get clipboard data
cbget() {
	if [ -z "$WAYLAND_DISPLAY" ]; then
		xclip -o -selection clipboard
	else
		wl-paste
	fi
}

# Upload to p.mort.coffee
bin() {
	if [ -z "$1" ]; then
		curl --silent --upload-file - https://p.mort.coffee | tee >(cbcopy)
	else
		curl --silent --upload-file "$1" https://p.mort.coffee | tee >(cbcopy)
	fi
}

# Create a temp dir, spawn a shell there, delete when done
tmp() {
	local dir="$(mktemp -d "/tmp/$USER.tmp.XXXXXXXX")"
	(cd "$dir"; $SHELL)
	rm -rf "$dir"
}

# Make .. go up once, ... up twice, etc
dots() {
	for _ in $(seq 1 $1); do
		cd ..
	done
}
dots_acc="."
for i in $(seq 1 10); do
	dots_acc="$dots_acc."
	alias "$dots_acc"="dots $i"
done

# Connect to a tmux session over ssh
sst() {
	if [ -z "$1" ] || [ -z "$2" ]; then
		echo "Usage: $0 <host> <tmux session>"
		return 1
	fi
	ssh -t "$1" "tmux attach -t '$2'"
}

# Go up, until the cwd ends with "$1"
to() {
	if [ -z "$1" ]; then
		echo "Usage: $0 <path fragment>"
		return 1
	fi

	if ! pwd | grep "$1/" >/dev/null; then
		echo "$1: not in the cwd"
		return 1
	fi

	while :; do
		wd="$(pwd)"

		# 'pwd' never seems to return paths with multiple successive slashes,
		# so checking if "$1//" is contained in "$wd//" is a "reliable" way to
		# check if "$wd" ends with "$1" using only posix sh.
		if [ "$wd" = "/" ] || printf "%s" "$wd//" | grep "$1//" >/dev/null; then
			return
		fi
		cd ..
	done
}

# Env vars
export PATH="/usr/lib/go-1.13/bin:$HOME/go/bin:$HOME/.cargo/bin:$HOME/.local/bin:$PATH"

# Enable programmable completion features
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

#prompt_git() {
#	local x=$?
#
#	local branch="$(git branch 2>/dev/null | grep '^\*' | sed 's/..//')"
#	if [ -n "$branch" ]; then
#		printf '\001\e[91m\002%s' "$branch"
#		if ! git diff --quiet; then
#			printf '\001\e[93m\e[1m\002*'
#		fi
#		printf ' '
#	fi
#
#	return $x
#}
#
#prompt_dollar() {
#	local x=$?
#
#	if [ $x = 0 ]; then
#		printf '\001\e[32m\002$\001\e[0m\002 '
#	else
#		printf '\001\e[31m\002$\001\e[0m\002 '
#	fi
#
#	return $x
#}
#
#PS1='\[\e[0;31m\]\u\[\e[m\]@\[\e[0;32m\]\h\[\e[m\] \[\e[0;36m\]\w\[\e[m\] $(prompt_git)$(prompt_dollar)'
PS1='$(starstruck --bash -e $?)'

# Load cargo
[ -e "$HOME/.cargo/env" ] && source "$HOME/.cargo/env"

# This should be the last part.
if [ -e "$HOME/.bashrc-local" ]; then
	source "$HOME/.bashrc-local"
fi

# If anything appears below this line, move it to .bashrc-local.
