#!/usr/bin/env bash
# vim:syntax=sh:ts=4

# ----- PROMPT COLOURS ----- #
fgcolor () {
	#echo "\\[\\033[38;5;"$1"m\\]"
	printf '\033[38;5;%bm' "$1"
}
fgtcolor () {
	printf '\033[38;2;%b;%b;%bm' "$1" "$2" "$3"
}
bgcolor () {
	#echo "\\[\\033[48;5;"$1"m\\]"
	printf '\033[48;5;%bm' "$1"
}
#  This is Solaris bash compatible, so we cannot use + here for "at least once) but instead use the
#   stupid old asterisk (*). Well, here hetting "no digits" practically won't hurt.
MYUID="$(id|sed 's/.*uid=\([0-9]*\).*/\1/g')";export MYUID
#   ...because "empty" will hit the "any" condition in the end :)
OSMAJVER="$(grep '^VERSION_ID' /etc/os-release|awk -F= '{print $NF}'|sed 's/"//g'|awk -F\. '{print $1}')"
case "$MYUID" in
	0)
		FGC1="fgcolor 88"
		case "$OSNAME" in
			debian)
				# Debian 12 = emerald
				if [ "$OSMAJVER" -ge 12 ];then
							FGC3="fgcolor 36";FGC2="fgtcolor 155 15 64"
				else
							FGC3="fgcolor 38";FGC2="fgtcolor 155 15 64"
				fi
			;;
			arch)			FGC2="fgcolor 24";FGC3="fgcolor 240";;
			redhat)			FGC1="fgcolor 23";FGC2="fgcolor 88";FGC3="fgcolor 203";;
			fedora)			FGC1="fgcolor 160";FGC2="fgcolor 88";FGC3="fgcolor 203";;
			# with the advent of Rocky, we'll match the time to SUSE's blue and the hostname to
			# the colour of Geeko:
			#*suse|aix)		FGC2="fgcolor 22";FGC3="fgcolor 41";;
			*sles*)			FGC2="fgcolor 22";FGC3="fgcolor 83";;
			*suse*)
				case "$OSNAME" in
					*leap)	FGC2="fgcolor 214";FGC3="fgcolor 83";;
					*)		FGC2="fgcolor 30";FGC3="fgcolor 83";;
				esac
			;;
			aix)			FGC2="fgcolor 238";FGC3="fgcolor 83";;
			*centos*)		FGC2="fgcolor 25";FGC3="fgcolor 208";;
			*solaris)		FGC2="fgcolor 31";FGC3="fgcolor 124";;
			ubuntu)
				if [ "$OSMAJVER" -ge 24 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC2="fgcolor 125";FGC3="fgcolor 202"
					else
							FGC2="fgcolor 53";FGC3="fgcolor 184"
					fi
				elif [ "$OSMAJVER" -ge 22 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC2="fgcolor 208";FGC3="fgcolor 89"
					else
							FGC2="fgcolor 184";FGC3="fgcolor 53"
					fi
				else
							FGC2="fgcolor 130";FGC3="fgcolor 215"
				fi
			;;
			rasp*)			FGC2="fgcolor 253";FGC3="fgcolor 126";;
			rocky)			FGC2="fgcolor 22";FGC3="fgcolor 41";;
			*)				FGC2="fgcolor 240";FGC3="fgcolor 244";;
		esac
		if [ -n "$TERMUX_APK_RELEASE" ]; then
			if [ "$TERMUX_APK_RELEASE" == "F_DROID" ]; then
				FGC2="fgcolor 27";FGC3="fgcolor 35"
			fi
		fi
	;;
	*)
		FGC2="fgcolor 253"
		case "$OSNAME" in
			debian)
				if [ "$OSMAJVER" -ge 12 ];then
							FGC1="fgtcolor 155 15 64";FGC3="fgcolor 36"
				else
							FGC1="fgtcolor 155 15 64";FGC3="fgcolor 38"
				fi
			;;
			arch)			FGC1="fgcolor 24";FGC3="fgcolor 239";;
			redhat)			FGC1="fgcolor 23";FGC3="fgcolor 88";;
			fedora)			FGC1="fgcolor 88";FGC3="fgcolor 203";;
			#*suse|aix)		FGC1="fgcolor 22";FGC3="fgcolor 41";;
			*sles*)			FGC1="fgcolor 22";FGC3="fgcolor 83";;
			*suse*)
				case "$OSNAME" in
					*leap)	FGC1="fgcolor 214";FGC3="fgcolor 83";;
					*)		FGC1="fgcolor 30";FGC3="fgcolor 83";;
				esac
			;;
			aix)			FGC1="fgcolor 238";FGC3="fgcolor 83";;
			*centos*)		FGC1="fgcolor 25";FGC3="fgcolor 208";;
			*solaris)		FGC1="fgcolor 31";FGC3="fgcolor 124";;
			ubuntu)
				if [ "$OSMAJVER" -ge 24 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC1="fgcolor 125";FGC3="fgcolor 202"
					else
							FGC1="fgcolor 53";FGC3="fgcolor 184"
					fi
				elif [ "$OSMAJVER" -ge 22 ];then
					if printf "%b" "$TERM"|grep -P 'xterm|256' > /dev/null;then
							FGC1="fgcolor 89";FGC3="fgcolor 208"
					else
							FGC1="fgcolor 53";FGC3="fgcolor 184"
					fi
				else
							FGC1="fgcolor 130";FGC3="fgcolor 215"
				fi
			;;
			rasp*)			FGC1="fgcolor 53";FGC3="fgcolor 126";;
			rocky)			FGC1="fgcolor 22";FGC3="fgcolor 41";;
			*)				FGC1="fgcolor 240";FGC3="fgcolor 244";;
		esac
		if [ -n "$TERMUX_APK_RELEASE" ]; then
			if [ "$TERMUX_APK_RELEASE" == "F_DROID" ]; then
				FGC1="fgcolor 27";FGC3="fgcolor 35"
			fi
		fi
	;;
esac
# HYPERVISOR LAYER 8 SOFTWARE
if pveversion > /dev/null 2>&1;then
	FGC1="fgcolor 236";FGC3="fgcolor 130"
fi
# make it possible to draw user-defined colours
# shellcheck disable=SC1090,SC1091
[ -r "${BBD}/prompt.bash.local" ] && source "${BBD}/prompt.bash.local"

# ----- USER SEP. AND HOSTNAME ----- #
UHSEP="@"
[ -n "$TMUX_PANE" ]&&UHSEP="[T]"
[ -n "$BYOBU_WINDOW_NAME" ]&&UHSEP="[B]"
[ "$(hostname -s)" == "monitoring" ]&&PHN="$(hostname -f)"||PHN="$(hostname -s)"

# ----- ASSEMBLE THE PROMPT COMMAND ADDITION ----- #
__prompt_command() {
	local EXIT="$?"
	if [ "$EXIT" -lt 10 ];then
		local EXIT2="00$EXIT"
	elif [ "$EXIT" -lt 100 ];then
		local EXIT2="0$EXIT"
	else
		local EXIT2="$EXIT"
	fi
	case "$MYUID" in
		0)
			PS1="\[\\033[2m\\033[3m\]\\#\[\\033[0m\] \[$(tput bold)\]\[$($FGC1)\]\\u\[$($FGC2)\]@$PHN \[$($FGC3)\]\\w \[$(tput sgr0)\]\\n"
		;;
		*)
			PS1="\[\\033[2m\\033[3m\]\\#\[\\033[0m\] \[$(tput bold)\]\[$($FGC1)\]\t \[$($FGC2)\]\\u$UHSEP\[$($FGC3)\]$PHN \\w \[$(tput sgr0)\]\\n"
		;;
	esac
	# git-completion. Sources:
	# - https://github.com/git/git/tree/master/contrib/completion (main)
	# - https://github.com/felipec/git-completion (fork)
	# - Debian: easygit (outdated! you may want to use something like /git-prompt inside this repo)
	# - Arch: git-completion (AUR, with functionality tests) - search for ...share/git(-completion)?/prompt.sh
	# - Manual measure: this repository, /git-prompt
	# Why all this fuss? magicmonty takes control over the whole prompt, __git_ps1 only serves a part
	# of it. We don't like monoliths, we like modules; the latter is good practice.
	# also, see postexec --> one-time sourcing of git's prompt.sh
	if command -v __git_ps1 >/dev/null 2>&1;then
		GIT_PS1_SHOWCOLORHINTS=1; export GIT_PS1_SHOWCOLORHINTS
		PS1+="$(__git_ps1 "%s ")"
	fi
	if [ "$EXIT" -eq 0 ];then
		PS1+="\[\033[0;32m\]# "
	elif [ "$EXIT" -eq 255 ]||[ "$EXIT" -eq 127 ];then
		PS1+="\[\033[0;33m\]$EXIT2 # "
	elif [ "$EXIT" -eq 130 ];then
		 PS1+="\[\033[0;35m\]$EXIT2 # "
	else
		PS1+="\[\033[0;31m\]$EXIT2 # "
	fi
	PS1+="\[\033[0m\]"
}

# ----- ADD __prompt_command() TO $PROMPT_COMMAND ----- #
if ! printf "%b" "$PROMPT_COMMAND" | grep '__prompt_command' >/dev/null;then
	case "$TERM" in
		# adding in "screen" because tmux
		xterm|xterm-color|xterm-256color|rxvt*|screen-256color|screen)
			if [ -n "$PROMPT_COMMAND" ];then
				export PROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}"
			else
				export PROMPT_COMMAND="__prompt_command"
			fi
		;;
		*)
			if [ -n "$PROMPT_COMMAND" ];then
				export PROMPT_COMMAND="__prompt_command;${PROMPT_COMMAND}"
			else
				export PROMPT_COMMAND="__prompt_command"
			fi
		;;
	esac
fi
# ----- HISTORY SUBMISSION IN $PROMPT_COMMAND ----- #
if ! printf "%b" "$PROMPT_COMMAND"|grep 'history -a' >/dev/null;then
	if [ -n "$PROMPT_COMMAND" ];then
		export PROMPT_COMMAND="$PROMPT_COMMAND;history -a;history -c;history -r"
	else
		export PROMPT_COMMAND="history -a;history -c;history -r"
	fi
fi