#!/usr/bin/env bash
# This script sets iTerm2 user-defined variables describing the state of the git
# repo in the current directory.
#
# For more information on the status bar, see:
# https://www.iterm2.com/3.3/documentation-status-bar.html
#
# Installation instructions for this script:
#
# bash: Place this in .bashrc.
# --------------------------------------------------------------------------------------
# function iterm2_print_user_vars() {
#   it2git
# }

# fish: Place this in ~/.config/fish/config.fish after the line
#       "source ~/.iterm2_shell_integration.fish".
# --------------------------------------------------------------------------------------
# function iterm2_print_user_vars
#   it2git
# end

# tcsh: Place this in .tcshrc
# --------------------------------------------------------------------------------------
# alias get_current_branch "bash -c '((git branch 2> /dev/null) | grep \* | cut -c3-)'"
# alias _iterm2_user_defined_vars 'it2git'

# zsh:Place this in .zshrc after "source /Users/georgen/.iterm2_shell_integration.zsh".
# --------------------------------------------------------------------------------------
# iterm2_print_user_vars() {
#   it2git
# }

GIT_BINARY=/usr/bin/git

dirty() {
    # Outputs "dirty" or "clean"
    OUTPUT=$("$GIT_BINARY" status --porcelain --ignore-submodules -unormal 2>/dev/null)
    if (($?)); then
        echo "clean"
        return
    fi
    if [ -z "$OUTPUT" ]; then
        echo "clean"
    else
        echo "dirty"
    fi
}

counts() {
    OUTPUT=$("$GIT_BINARY" rev-list --left-right --count HEAD...@'{u}' 2>/dev/null)
    if (($?)); then
        echo "error"
        return
    fi
    echo "$OUTPUT"
}

branch() {
    OUTPUT=$("$GIT_BINARY" symbolic-ref -q --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null)
    if (($?)); then
        return
    fi
    echo "$OUTPUT"
}

adds() {
    "${GIT_BINARY}" ls-files --others --exclude-standard | wc -l
}

deletes() {
    "${GIT_BINARY}" ls-files --deleted --exclude-standard | wc -l
}
function iterm2_begin_osc {
  printf "\033]"
}

function iterm2_end_osc {
  printf "\007"
}

function iterm2_set_user_var() {
  iterm2_begin_osc
  printf "1337;SetUserVar=%s=%s" "$1" $(printf "%s" "$2" | base64 | tr -d '\n')
  iterm2_end_osc
}

git_poll () {
    DIRECTORY="$1"
    cd "$DIRECTORY"
    DIRTY=$(dirty)
    COUNTS=$(counts)
    PUSH_COUNT=$(cut -f1 <<< "$COUNTS")
    PULL_COUNT=$(cut -f2 <<< "$COUNTS")
    BRANCH=$(branch)

    iterm2_set_user_var gitBranch "$BRANCH"
    iterm2_set_user_var gitDirty "$DIRTY"
    iterm2_set_user_var gitPushCount "$PUSH_COUNT"
    iterm2_set_user_var gitPullCount "$PULL_COUNT"
    iterm2_set_user_var gitAdds "$ADDS"
    iterm2_set_user_var gitDeletes "$DELETES"
}

"$GIT_BINARY" rev-parse --git-dir 2>/dev/null >/dev/null
if (($?)); then
    iterm2_set_user_var gitBranch ""
    iterm2_set_user_var gitDirty ""
    iterm2_set_user_var gitPushCount ""
    iterm2_set_user_var gitPullCount ""
    iterm2_set_user_var gitAdds ""
    iterm2_set_user_var gitDeletes ""
else
    git_poll "$PWD"
fi


