# PrtmPhlps's Dotfiles
# Source: https://github.com/PrtmPhlp/Dotfiles
#  _               _
# | |__   __ _ ___| |__  _ __ ___
# | '_ \ / _` / __| '_ \| '__/ __|
# | |_) | (_| \__ \ | | | | | (__
# |_.__/ \__,_|___/_| |_|_|  \___|

#S:------------------------------------------------
case "$OSTYPE" in
linux*)
    # Nitch
    if command -v nitch &>/dev/null; then
        nitch
    fi
    HISTCONTROL=ignoreboth
    shopt -s histappend
    HISTSIZE=10000
    HISTFILESIZE=20000

    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi

    case "$(hostname)" in
        pve)                PROMPT_NAME=proxmox ;;
        pve2)               PROMPT_NAME=pve2 ;;
        DietPi)             PROMPT_NAME=Raspberry ;;
        srv-prod-ubuntu-1)  PROMPT_NAME=pve1 ;;
        srv-prod-ubuntu-2)  PROMPT_NAME=pve2 ;;
        srv-prod-ubuntu-3)  PROMPT_NAME=pve3 ;;
        *)                  PROMPT_NAME="$(whoami)" ;;
    esac
    # bash theme - partly inspired by https://github.com/ohmyzsh/ohmyzsh/blob/master/themes/robbyrussell.zsh-theme
    __bash_prompt() {
        local userpart='`export XIT=$? \
        && [ -n "${PROMPT_NAME}" ] && echo -n "\[\033[0;32m\]@${PROMPT_NAME} " || echo -n "\[\033[0;32m\]\u " \
        && [ "$XIT" -ne "0" ] && echo -n "\[\033[1;31m\]➜" || echo -n "\[\033[0m\]➜"`'
        local gitbranch='`\
        if [ "$(git config --get devcontainers-theme.hide-status 2>/dev/null)" != 1 ] && [ "$(git config --get codespaces-theme.hide-status 2>/dev/null)" != 1 ]; then \
            export BRANCH=$(git --no-optional-locks symbolic-ref --short HEAD 2>/dev/null || git --no-optional-locks rev-parse --short HEAD 2>/dev/null); \
            if [ "${BRANCH}" != "" ]; then \
                echo -n "\[\033[0;36m\](\[\033[1;31m\]${BRANCH}" \
                && if [ "$(git config --get devcontainers-theme.show-dirty 2>/dev/null)" = 1 ] && \
                    git --no-optional-locks ls-files --error-unmatch -m --directory --no-empty-directory -o --exclude-standard ":/*" > /dev/null 2>&1; then \
                        echo -n " \[\033[1;33m\]✗"; \
                fi \
                && echo -n "\[\033[0;36m\]) "; \
            fi; \
        fi`'
        local lightblue='\[\033[1;34m\]'
        local removecolor='\[\033[0m\]'
        PS1="${userpart} ${lightblue}\w ${gitbranch}${removecolor}\$ "
        unset -f __bash_prompt
    }
    __bash_prompt
    export PROMPT_DIRTRIM=4
    ;;
    darwin*)
    export PS1="\[\033[38;5;229m\]\u\[$(tput sgr0)\]:\[$(tput sgr0)\]\[\033[38;5;39m\][\[$(tput sgr0)\]\w\[$(tput sgr0)\]\[\033[38;5;39m\]]\[$(tput sgr0)\] \t \\$ \[$(tput sgr0)\]"
    ;;
esac


if command -v fzf &>/dev/null; then
    eval "$(fzf --bash)"
fi

alias c="clear"

# Editor config
if command -v nvim &>/dev/null; then
    export EDITOR=nvim
    alias vim="nvim"
else
    export EDITOR=vim
fi

# Directory aliases
alias home='cd ~'
alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'

# eza
if command -v eza &>/dev/null; then
    alias ls="eza --icons=auto"
    alias la="eza -ahl --icons=auto --git --ignore-glob='.DS_Store'"
    alias lla="eza -a --icons=auto --git"
    alias lal="clear ; eza -a --icons=auto --group -s type -r"
    alias lat="eza -ahl --icons=auto --git --tree -L 2 --ignore-glob='.git|node_modules|.venv'"
    alias clat="clear ; eza -ahl --icons=auto --git --tree -L 2 --ignore-glob='.git|node_modules|.venv'"
    alias cla="clear; eza -ahl --icons=auto --git --ignore-glob='.DS_Store'"
elif command -v exa &>/dev/null; then
    alias ls="exa --icons"
    alias la="exa -ahl --icons"
    alias lla="exa -a --icons"
    alias lal="clear ; exa -a --icons --group -s type -r"
    alias lat="exa -ahl --icons --tree -L 2"
    alias clat="clear ; exa -ahl --icons --tree -L 2"
else
    alias la="ls -ahl"
    alias lal="clear ; la"
fi

# DOCKER
alias d="docker"
alias dc="docker compose"
alias dcup="docker compose up -d"
alias dcdown="docker compose down"
alias dclg="docker compose logs -f"
alias dcul="docker compose up -d && docker compose logs -f"
alias dcre="docker compose up -d --force-recreate --remove-orphans"
alias dcrel="docker compose up -d --force-recreate --remove-orphans && docker compose logs -f"
alias dcub="docker compose up -d --build"

# GIT
alias g="git"

alias ga="git add"
alias gco="git commit"

alias gpu="git push"
alias gp="git pull"

alias gb="git branch"
alias gl='git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit'
alias gst='awk -vOFS='\'''\'' '\''NR==FNR {all[i++] = $0; difffiles[$1] = $0; next;} ! ($2 in difffiles) {print; next;} {gsub($2, difffiles[$2]); print;} END {if (NR != FNR) {exit;} for (i in all) {print all[i];}}'\'' <(git diff --color --stat=$(($(tput cols) - 3)) HEAD | sed '\''$d; s/^ //'\'') <(git -c color.status=always status -sb)'

alias gia="git add -A"
alias gc="git clone"
alias gcauto="git add -A && git commit -m 'Automatic commit' && git push"
# alias gc='f(){ git clone "$1" && cd "$(basename $1 .git)"; unset -f f; }; f'

[[ -f ~/.bashrc_local ]] && source ~/.bashrc_local || true
