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

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

    for package in "${packages[@]}"; do
        # Check if the package exists in Cellar (for formulae)
        if [ -d "$cellar_dir/$package" ]; then
            # echo "$package (formula) is already installed."
        # Check if the package exists in Caskroom (for casks)
        elif [ -d "$caskroom_dir/$package" ]; then
            # echo "$package (cask) is already installed."
        else
            echo "Installing $package..."
            # Install or upgrade package based on whether it's a cask
            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
}


## 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

# 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

  # Alacritty Crap
  alacritty_config_setup osx
fi

shell_start_disp

# Keybindings
bindkey -e
bindkey '^p' history-search-backward
bindkey '^n' history-search-forward
bindkey '^[w' kill-region

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

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

setopt EXTENDED_HISTORY
setopt APPEND_HISTORY
setopt SHARE_HISTORY
setopt HIST_IGNORE_SPACE
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_SAVE_NO_DUPS
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS

# 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
}
# Auto CD
setopt AUTOCD

# Completion styling
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
zstyle ':completion:*' menu no
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'ls --color $realpath'
zstyle ':fzf-tab:complete:__zoxide_z:*' fzf-preview 'ls --color $realpath'
zstyle ':completion:*' range 1000:100 # Try 100 history words at a time; max 1000 words.


# Check Starship is installed
if which starship &> /dev/null
then
  eval "$(starship init zsh)"
else
  curl -sS https://starship.rs/install.sh | sh
fi
# Created by `pipx` on 2024-07-16 03:28:59
export PATH="$PATH:/Users/errol/.local/bin"

if [[ ! -d ~/.tmux/plugins/tpm ]]
then
  git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm
fi
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/errol/Downloads/google-cloud-sdk/path.zsh.inc' ]; then . '/Users/errol/Downloads/google-cloud-sdk/path.zsh.inc'; fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/errol/Downloads/google-cloud-sdk/completion.zsh.inc' ]; then . '/Users/errol/Downloads/google-cloud-sdk/completion.zsh.inc'; fi

export GOOGLE_APPLICATION_CREDENTIALS=/Users/errol/.config/gcloud/application_default_credentials.json

alias gam="/home/errol/bin/gamadv-xtd3/gam"
if which kubectl &> /dev/null
then
  source <(kubectl completion zsh)
fi
