#!/bin/bash
#

# DEFINITIONS #####################################################################################
COLOR_BACKGROUND="\033[48;2;48;64;64m"              # RGB BACKGROUND
COLOR_FOR_AS_BACKGROUND="\033[38;2;48;64;64m"       # RGB FOREGROUND LIKE BACKGROUND

COLOR_DARKGREEN="\033[0;32m"                        # PROMPT LINES
COLOR_NORMAL="\033[0m"                              # RESET

SYMBOL_START="$COLOR_NORMAL$COLOR_FOR_AS_BACKGROUND\Ue0b2$COLOR_BACKGROUND "
SYMBOL_END="$COLOR_BACKGROUND $COLOR_NORMAL$COLOR_FOR_AS_BACKGROUND\Ue0b0"
SYMBOL_SPLIT=" "
SYMBOL_BRANCH="\Ue0a0\x00"
SYMBOL_NEW="…\x00"
SYMBOL_AHEAD="↑\x00"
SYMBOL_BEHIND="↓\x00"
SYMBOL_CHANGED="✚\x00"
SYMBOL_COMMITED="●\x00"
SYMBOL_STASH="⚑\x00"

COLOR_BRANCH_CLEAN="\033[38;2;0;192;0m"
COLOR_BRANCH_CHANGED="\033[38;2;255;192;8m"
COLOR_BRANCH_OUTDATED="\033[38;2;152;152;152m"
COLOR_BRANCH_COMMIT="\033[38;2;208;168;224m"
COLOR_BRANCH_BRANCH="\033[38;2;140;208;240m"
COLOR_NEW="\033[38;2;224;48;8m"
COLOR_AHEAD="\033[38;2;192;192;192m"
COLOR_BEHIND="$COLOR_AHEAD"
COLOR_CHANGED="\033[38;2;224;128;8m"
COLOR_COMMITED="\033[38;2;8;224;160m"
COLOR_STASH="\033[38;2;0;32;224m"
COLOR_URL="\033[38;2;0;224;192m"

BRANCH_VAL="unknown"
AHEAD_VAL="0"
BEHIND_VAL="0"
NEW_VAL="0"
CHANGED_VAL="0"
COMMITED_VAL="0"


# COMMAND LINE ARGUMENTS ##########################################################################
usage="Usage: $0 [-p | output for prompt] [-s | short output]"
while getopts "hps" options; do
  case $options in
    p ) PROMPT="True";;
    s ) SHORT="True";;
    h ) echo $usage
        exit 0
        ;;
   \? ) echo $usage
        exit 1
        ;;
  esac
done

# GIT COMMANDS ####################################################################################
GIT_URL="$(LANGUAGE='en_US.UTF-8 git' git config --get remote.origin.url 2> /dev/null)"
GIT_STATUS="$(LANGUAGE='en_US.UTF-8 git' git status -sb 2> /dev/null)"
GIT_FILES="$(LANGUAGE='en_US.UTF-8 git' git  status -s 2> /dev/null)"
BRANCH_VAL="$(LANGUAGE='en_US.UTF-8 git' git branch 2> /dev/null | sed --quiet 's/* \(.*\)/\1/p')"
STASH_VAL=$(LANGUAGE='en_US.UTF-8 git' git stash list 2> /dev/null | wc -l)

# decision for git output (path is repository)
if [ -z "$GIT_STATUS" ]; then
  # No GIT repository
  exit
fi

# GIT MAGIC #######################################################################################
# BEHIND_VAL, AHEAD_VAL
BEHIND_VAL=$(echo $GIT_STATUS | grep -Po "behind\s+\K\w+")
AHEAD_VAL=$(echo $GIT_STATUS | grep -Po "ahead\s+\K\w+")

# NEW_VAL, MODIFIED_VAL, COMMITED_VAL
GIT_STATUS_DETAILS=""
if [[  ! -z "$GIT_FILES" ]]; then
    while IFS= read -r line ;do
        FILES_STATUS=${line:0:2}
        FILES_STATUS_COMMITED=${FILES_STATUS:0:1}
        FILES_STATUS_CHANGED=${FILES_STATUS:1:1}
        FILES_NAME=$(echo $line | awk '{print $2}')
        # NEW FILE increase
        if [[ $FILES_STATUS == "??" ]]; then
            ((NEW_VAL++))
        fi

        # CHANGED
        if [[ $FILES_STATUS_CHANGED != "?" && $FILES_STATUS_CHANGED != " " && $FILES_STATUS_CHANGED != "" ]]; then
            ((CHANGED_VAL++))
        fi

        # COMMITED
        if [[ $FILES_STATUS_COMMITED != "?" && $FILES_STATUS_COMMITED != " " && $FILES_STATUS_COMMITED != "" ]]; then
            ((COMMITED_VAL++))
        fi

    done <<< "$GIT_FILES"
fi

# GIT STATUS COLOR ################################################################################
if [[ $COMMITED_VAL > 0 || $CHANGED_VAL > 0 ]]; then
    COLOR_BRANCH="$COLOR_BRANCH_CHANGED"
elif [[ $BEHIND_VAL > 0 || $AHEAD_VAL > 0  || $NEW_VAL > 0 ]]; then
    COLOR_BRANCH="$COLOR_BRANCH_OUTDATED"
elif [[ $BRANCH_VAL != "master" && $BRANCH_VAL != "main" ]]; then
    if [[ $BRANCH_VAL == *"HEAD detached at"* ]]; then
        COLOR_BRANCH=$COLOR_BRANCH_COMMIT
        echo $BRANCH_VAL
        BRANCH_VAL=$(echo $BRANCH_VAL | awk '{print $4}')
        echo $BRANCH_VAL
        BRANCH_VAL=${BRANCH_VAL::-1}
        echo $BRANCH_VAL
    else
        COLOR_BRANCH=$COLOR_BRANCH_BRANCH
    fi
else
    COLOR_BRANCH=$COLOR_BRANCH_CLEAN
fi

# GIT GATHERING ###################################################################################
BRANCH="$COLOR_BRANCH$COLOR_BACKGROUND$SYMBOL_BRANCH$BRANCH_VAL"
BEHIND="$COLOR_BEHIND$COLOR_BACKGROUND$SYMBOL_BEHIND$BEHIND_VAL"
AHEAD="$COLOR_AHEAD$COLOR_BACKGROUND$SYMBOL_AHEAD$AHEAD_VAL"
NEW="$COLOR_NEW$COLOR_BACKGROUND$SYMBOL_NEW$NEW_VAL"
CHANGED="$COLOR_CHANGED$COLOR_BACKGROUND$SYMBOL_CHANGED$CHANGED_VAL"
COMMITED="$COLOR_COMMITED$COLOR_BACKGROUND$SYMBOL_COMMITED$COMMITED_VAL"
STASH="$COLOR_STASH$COLOR_BACKGROUND$SYMBOL_STASH$STASH_VAL"

# GIT OUTPUT ######################################################################################
GIT_STATUS_SUMMARY="$BRANCH"
if [[ $BEHIND_VAL > 0 ]]; then
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$BEHIND
fi
if [[ $AHEAD_VAL > 0 ]]; then
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$AHEAD
fi
if [[ $COMMITED_VAL > 0 ]]; then
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$COMMITED
fi
if [[ $CHANGED_VAL > 0 ]]; then
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$CHANGED
fi
if [[ $NEW_VAL > 0 ]]; then 
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$NEW
fi
if [[ $STASH_VAL > 0 ]]; then 
    GIT_STATUS_SUMMARY=$GIT_STATUS_SUMMARY$SYMBOL_SPLIT$STASH
fi

if [[ $PROMPT == "True" ]]; then
    if [[ $SHORT == "True" ]]; then
        echo -ne " $SYMBOL_START$GIT_STATUS_SUMMARY$SYMBOL_END"
    else
        echo -e "$COLOR_DARKGREEN├──$SYMBOL_START$GIT_STATUS_SUMMARY $COLOR_NORMAL$COLOR_BACKGROUND- $COLOR_URL$GIT_URL$SYMBOL_END"
        if [[ $GIT_PROMPT_DETAILS == "on" && ! -z "$GIT_FILES" ]]; then
            while IFS= read -r line ;do
                FILES_STATUS=${line:0:2}
                FILES_STATUS_CHANGED=${FILES_STATUS:1:1}
                if [[ $FILES_STATUS_CHANGED != "?" && $FILES_STATUS_CHANGED != " " ]]; then
                    echo -e "$COLOR_DARKGREEN│ $COLOR_BRANCH$line"
                fi
            done <<< "$GIT_FILES"
        fi
        echo -ne "$COLOR_NORMAL\b"
    fi
else
    echo -e "$SYMBOL_START$GIT_STATUS_SUMMARY $COLOR_NORMAL$COLOR_BACKGROUND- $COLOR_URL$GIT_URL$SYMBOL_END"
    if [[ ! -z "$GIT_FILES" ]]; then
        while IFS= read -r line ;do
            if [[ $line != "??"* ]]; then
                echo -e " $COLOR_BRANCH$line"
            fi
        done <<< "$GIT_FILES"
    fi
    echo -ne "$COLOR_NORMAL"
fi
