#!/bin/bash

function signal() {
	echo "$@" > ~/.signal
}
function silent() {
	rm ~/.signal
}

# Check for an interactive session
[ -z "$PS1" ] && return


# https://wiki.archlinux.org/index.php/Color_Bash_Prompt#Wolfman.27s
##################################################
# Fancy PWD display function
##################################################
# The home directory (HOME) is replaced with a ~
# The last pwdmaxlen characters of the PWD are displayed
# Leading partial directory names are striped off
# /home/me/stuff		  -> ~/stuff			   if USER=me
# /usr/share/big_dir_name -> ../share/big_dir_name if pwdmaxlen=20
##################################################
bash_prompt_command() {
	PROMPTRET="$?"
	if [ $PROMPTRET == 0 ];
	then
		PROMPTRET=""
	else
	  PROMPTRET=" $PROMPTRET"
	fi
	if [ -e ~/.signal ]
	then
		SIGNAL=$(cat ~/.signal)
		[ -z "$SIGNAL" ] && SIGNAL="signal"
		SIGNAL="[$SIGNAL]"
	else
		SIGNAL=""
	fi

	end_time=$(date +%s)
	NEWF=''
	local mfiles=$(find . -maxdepth 1 -type f -newermt '5 sec ago')
	if [ -n "$mfiles" ]; then
		NEWF=" $(ls -rt $mfiles 2>/dev/null | tail -3 | xargs -n 1 basename 2>/dev/null | tr '\n' ' ' )"
	fi

	# How many characters of the $PWD should be kept
	local pwdmaxlen=30
	# Indicate that there has been dir truncation
	local trunc_symbol="..."
	local dir=${PWD##*/}
	pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
	NEW_PWD=${PWD/#$HOME/\~}
	local pwdoffset=$(( ${#NEW_PWD} - pwdmaxlen ))

	if [ ${pwdoffset} -gt "0" ]
	then
		NEW_PWD=${NEW_PWD:$pwdoffset:$pwdmaxlen}
		NEW_PWD=${trunc_symbol}/${NEW_PWD#*/}
	fi

	GIT_INFO=$(git_info)
}


git_info () {
	GITSTATUS=$(LC_ALL=C git status 2>&1 | tr -d '#' | awk '
	BEGIN {
        }
        /fatal:/		              { exit(1) }
        /On branch/	              { branch=$3 }
        /detached at/	            { branch="detached at " $4 }
        /working directory clean/ { clean=" clean" }
        /branch is up-to-date/    { commits="" }
        /branch is ahead/         { commits="+" $8}
        /branch is behind/        { commits="-" $7}
        /have diverged/           { commits=" diverged"}
        /to be committed/         { staged=1; clean = " " }
        /modified:/	              { modified="M" }
        /deleted:/		            { deleted="D" }
        /new file:/	              { newfile="N" }
        /Untracked/	              { untracked="?" }
        /not staged/	            { staged=0; clean = " " }
	END {
    print( "("branch commits clean untracked modified deleted newfile ") ")
	}
	')
	Q=$?
#	  print(EMW "(" EM branch W commits clean R untracked B modified R deleted G newfile EMW ") ")
	  	# There seems to be no way to expand colored values into PS1 w/o breaking the length magic,
	# so we re-set it every time

  PS1STR="";
	if [ $Q == 0 ]; then {
	    PS1STR="${GITSTATUS}";
  } fi
  echo -e $PS1STR
}

bash_prompt() {
	local EMG="\[\033[1;32m\]"
	local EMB="\[\033[1;34m\]"
  local PSIGN="$"
  trap 'echo -ne "\033[0;37m"' DEBUG
	PS1="[\u@\h::\${NEW_PWD}\${NEWF}]\${SIGNAL} ${EMB}\${GIT_INFO}${EMG} ${PSIGN} "
	BASE_PS1="$PS1" # for git_info
}



PROMPT_COMMAND=bash_prompt_command
bash_prompt
unset bash_prompt