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

# 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 alacritty_config_setup {
    if [[ -f $HOME/.config/alacritty/alacritty-base.toml ]]; then
        # echo $(pwd)
        toml_files=("$HOME/.config/alacritty/alacritty-base.toml" "$HOME/.config/alacritty/alacritty-$1.toml")
        combined_file="$HOME/.config/alacritty/alacritty.toml"
        echo -n >"$combined_file"
        for file in "${toml_files[@]}"; do
            # Check if the file exists
            if [ -f "$file" ]; then
                # Append content of the current TOML file to the combined file
                cat "$file" >>"$combined_file"
                # Add a newline to separate TOML sections (optional)
                echo >>"$combined_file"
            else
                echo "File '$file' not found. Skipping."
            fi
        done
    fi
}
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"

    # Alacritty Crap
    alacritty_config_setup linux
fi

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

    # Alacritty Crap
    alacritty_config_setup osx
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
}

# Check Starship is installed
if which starship &>/dev/null; then
    eval "$(starship init zsh)"
else
    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" ]; do
        for subbc in $bcfile/*; do
            [[ -f "$subbc" ]] && . $subbc
        done
    fi
done
