#!/bin/bash

########################################################################
# .bash_prompt
# 
# Matthew's Git Bash Prompt https://github.com/matthewmccullough/dotfiles
# +
# python virtualenv
# +
# xterm dynamic title http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#s2
#
# define PROMPT_COMMAND to prompt_func
########################################################################
         RED="\[\033[0;31m\]"
      YELLOW="\[\033[0;33m\]"
       GREEN="\[\033[0;32m\]"
        BLUE="\[\033[0;34m\]"
   LIGHT_RED="\[\033[1;31m\]"
 LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_YELLOW="\[\033[1;33m\]"
       WHITE="\[\033[1;37m\]"
  LIGHT_GRAY="\[\033[0;37m\]"
  COLOR_NONE="\[\e[0m\]"

function _my_parse_git_branch {
  git rev-parse --git-dir &> /dev/null
  local git_status="$(LANG=C git status 2> /dev/null)"
  local branch_pattern="^On branch ([^${IFS}]*)"
  local remote_pattern="Your branch is (.*) '"
  local diverge_pattern="Your branch and (.*) have diverged"
  local state=""
  local remote=""
  local branch=""

  [[ ${git_status} =~ "Not a git repository" ]] && return
  
  if [[ ! ${git_status} =~ "working directory clean" ]]; then
    state="${RED}⚡"
  fi
  # add an else if or two here if you want to get more specific
  if [[ ${git_status} =~ ${remote_pattern} ]]; then
    if [[ ${BASH_REMATCH[1]} == "ahead of" ]]; then
      remote="${YELLOW}↑"
    else
      remote="${YELLOW}↓"
    fi
  fi
  if [[ ${git_status} =~ ${diverge_pattern} ]]; then
    remote="${YELLOW}↕"
  fi
  if [[ ${git_status} =~ ${branch_pattern} ]]; then
    branch=${BASH_REMATCH[1]}
    #branch=$(__git_ps1)
    echo "${BLUE}[${GREEN}(${branch})${remote}${state}${BLUE}]${COLOR_NONE}"
  fi
  # echo "DEBUG[branch:$branch|remote:$remote|state:$state]"
}

function prompt_func() {
    previous_return_value=$?;

	## BASH : enable shared history
    ## DONT DO THAT : DANGEROUS
    #history -a; history -c; history -r

    # the prompt craft
    local prompt=''

    #The lowercase w is the full current working directory
    # prompt="${TITLEBAR}${BLUE}[${RED}\w${GREEN}$(_my_parse_git_branch)${BLUE}]${COLOR_NONE}"

    # [user$host]
    prompt="${prompt}${COLOR_NONE}[${YELLOW}\u${COLOR_NONE}@${WHITE}\h${COLOR_NONE}]"

    # python virtualenv
    test -n "$VIRTUAL_ENV" && prompt="${prompt} (env:${BLUE}$(basename $VIRTUAL_ENV)${COLOR_NONE})"

    # git status
    test -n "$(_my_parse_git_branch)" && prompt="${prompt}[git:${GREEN}$(_my_parse_git_branch)${COLOR_NONE}]"

    # add -- \w (git branch) if new minute or cd
    # [[ "x$previous_path" != "x$(pwd)" ]] ||  [[  "$previous_minute" != "$(date +%M)" ]] && prompt="$prompt $(_my_parse_git_branch) -- $(date +%a\ %d\ %B)"

    # date
    # prompt="$prompt -- $(date +%a\ %d\ %B)"

    # hour
    prompt="$prompt ${LIGHT_YELLOW}$(date +%R)${COLOR_NONE}"
    # add, at least the time
    # prompt="${prompt} $(date +%X)"
    
    # PWD
    prompt="${prompt} \w"

    # if test $previous_return_value -eq 0
    # then
    #     PS1="${prompt} ${GREEN}\$${COLOR_NONE} "
    # else
    #     PS1="${prompt} ${RED}(err=${previous_return_value}) \$${COLOR_NONE} "
    # fi

    # test right align FIXME
    # PS1=$(printf "%${COLUMNS}s                                   \r%s" "${prompt}" "${GREEN}\$${COLOR_NONE} ")

    # red/green if exit 1/0
    if test $previous_return_value -eq 0
    then
        PS1="${prompt} ${GREEN}\$${COLOR_NONE} "
    else
        PS1="${prompt} ${RED}(err=${previous_return_value}) \$${COLOR_NONE} "
    fi

    previous_minute="$(date +%M)"
    previous_path=$(pwd)

    # set xterm -like title
    echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
}

PROMPT_COMMAND=prompt_func
# end .bash_prompt