# vim: ft=sh:noml:

propmt_dirt() {
  s=''
  git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
  repoUrl="$(git config --get remote.origin.url)"
  if echo "${repoUrl}" | grep -q 'chromium/src.git'; then
    s="${s}*"
  else
    # Check for uncommitted changes in the index.
    if ! git diff --quiet --ignore-submodules --cached; then
      s="${s}+"
    fi
    # Check for unstaged changes.
    if ! git diff-files --quiet --ignore-submodules --; then
      s="${s}!"
    fi
    # Check for untracked files.
    # shellcheck disable=SC2312
    if [ -n "$(git ls-files --others --exclude-standard)" ]; then
      s="${s}?"
    fi
    # Check for stashed files.
    if git rev-parse --verify refs/stash >/dev/null 2>&1; then
      s="${s}$"
    fi
  fi
  [ -n "${s}" ] && s="[${s}]"
  printf '%b\n' "${s}"
}

# check if the directory contains git then check the branch
prompt_git() {
  branchName=''
  # Check if the current directory is in a Git repository.
  git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
  # Check for what branch we’re on.
  # Get the short symbolic ref. If HEAD isn’t a symbolic ref, get a
  # tracking remote branch or tag. Otherwise, get the
  # short SHA for the latest commit, or give up.
  branchName="$(git symbolic-ref --quiet --short HEAD 2>/dev/null \
    || git describe --all --exact-match HEAD 2>/dev/null \
    || git rev-parse --short HEAD 2>/dev/null \
    || echo '(unknown)')"
  printf '%b\n' "(${branchName}"
}

# append the last parentheisis at the back of git branch status
prompt_closingBracket() {
  git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
  printf '%b\n' ")"
}

# call git check
git_branch() {
  prompt_git | sed 's/()//'
}

# bash propmt
PS1='\[\e[m\]'                                   # clear all colors
PS1="${PS1}\[\e[32m\]\u"                         # username green
PS1="${PS1}\[\e[35m\]@"                          # @ purple
PS1="${PS1}\[\e[34m\]\h"                         # : blue
PS1="${PS1}\[\e[m\]:"                            # white
PS1="${PS1}\[\e[36m\]\w"                         # working dir cyan
PS1="${PS1}\[\e[33m\]$(git_branch)"              # git branch brown
PS1="${PS1}\[\e[31m\]$(propmt_dirt)"             # git status red
PS1="${PS1}\[\e[33m\]$(prompt_closingBracket)\n" # git closing bracket brown
PS1="${PS1}\[\e[32m\]└─>"                        # next line indicator green
PS1="${PS1}\[\e[m\] "                            # end of prompt clear the color
