## bashrc file
## Errol Sanckatar
## errol@sancaktar.net
##

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# HomeBrew

## Check for HomeBrew
if [[ -f "/opt/homebrew/bin/brew" ]]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
    export HOMEBREW_NO_ENV_HINTS=1
    ensure_brew_packages $brew_packages
fi

function ensure_brew_packages {
    local packages=("$@")
    local cellar_dir="/opt/homebrew/Cellar"
    local caskroom_dir="/opt/homebrew/Caskroom"

    for package in "${packages[@]}"; do
        if [ -d "$cellar_dir/$package" ]; then
            #echo "$package (formula) is already installed."
            continue
        elif [ -d "$caskroom_dir/$package" ]; then
            #echo "$package (cask) is already installed."
            continue
        else
            echo "Installing $package..."
            if brew info --cask $package &>/dev/null; then
                brew install --cask --force $package
            else
                brew install --force $package
            fi
        fi
    done
}

brew_packages=(
    "opentofu"
    "docker"
    "docker-completion"
    "helm"
    "ipython"
    "jq"
    "k9s"
    "kubernetes-cli"
    "neovim"
    "nmap"
    "stow"
    "terragrunt"
    "tmux"
    "font-hack-nerd-font"
    "alacritty"
    "spotify"
    "tailscale"
    "visual-studio-code"
    "fzf"
    "ripgrep"
    "fd"
    "spotify_player"
)

function shell_start_disp {
    echo -n "$(hostname) - "
    if [[ -f /sbin/ifconfig ]]; then
        ifconfig | awk '/inet / && !/inet6/ && !/127.0.0.1/ {ips = ips " " $2} END {print ips}'
    fi
}


# IF Linux
if [[ $(uname) == "Linux" ]]; then
#    [[ ":$PATH:" != *":/home/errol/Applications:"* ]] && PATH="/home/errol/Applications:${PATH}"
    # The next line updates PATH for the Google Cloud SDK.
#    if [ -f '/home/errol/google-cloud-sdk/path.zsh.inc' ]; then . '/home/errol/google-cloud-sdk/path.zsh.inc'; fi

    # The next line enables shell command completion for gcloud.
#    if [ -f '/home/errol/google-cloud-sdk/completion.zsh.inc' ]; then . '/home/errol/google-cloud-sdk/completion.zsh.inc'; fi
    export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
fi

## IF MacOS
if [[ $(uname) == "Darwin" ]]; then
    # PATH
  if [ -d /opt/homebrew/bin ]; then
    export PATH="/opt/homebrew/bin:$PATH"
  fi
fi

# Google Stuff
if [[ -r ~/.googlerc ]]; then
    . ~/.googlerc
fi

shell_start_disp

# Alias
if [[ -r ~/.aliasrc ]]; then
    . ~/.aliasrc
fi

# History
HISTSIZE=5000000
HISTFILE=~/.bash_history
SAVEHIST=$HISTSIZE


# Fuzzy search
function fh() {
    eval $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed 's/ *[0-9]* *//')
}

# history with search
function h() {
    # check if we passed any parameters
    if [ -z "$*" ]; then
        # if no parameters were passed print entire history
        history 1
    else
        # if words were passed use it as a search
        history 1 | egrep --color=auto "$@"
    fi
}


# Google Stuff
if `env | grep -q google`; then
  . ~/.google_rc
fi


# Check Starship is installed
if which starship &>/dev/null; then
    if [ $SHELL == '/bin/zsh' ]; then
      eval "$(starship init zsh)"
    elif [ $SHELL == '/bin/bash' ]; then
      eval "$(starship init bash)"
    fi
else
    echo "Starship not Installed, Install with: curl -sS https://starship.rs/install.sh | sh"
fi

export PATH="$PATH:/Users/errol/.local/bin"

# COMPLETION

for bcfile in ~/.bash_completion.d/*; do
    [ -f "$bcfile" ] && . $bcfile
    if [ -d "$bcfile" ]; then
        for subbc in $bcfile/*; do
            [[ -f "$subbc" ]] && . $subbc
        done
    fi
done

