[[ -f "$ZSH_CONFIG/zsh-init.sh" ]] && source "$ZSH_CONFIG/zsh-init.sh"

source $OHMYZSH/oh-my-zsh.sh

# eza hyperlink workaround: oh-my-zsh's bare --hyperlink eats the next arg on newer eza.
_hl_flag="--hyperlink"
if command -v eza >/dev/null && eza --help 2>&1 | grep -q -- '--hyperlink.*<WHEN>'; then
  _hl_flag="--hyperlink=always"
fi
for _ea in la ldot lD lDD ll ls lsd lsdl lS lT; do
  (( $+aliases[$_ea] )) && alias "$_ea"="${aliases[$_ea]} $_hl_flag"
done
unset _ea _hl_flag

# zoxide: frecency-based directory jumping.
command -v zoxide >/dev/null && eval "$(zoxide init zsh)" 2>/dev/null

# bat: colored `cat` and colored man pages.
if command -v bat >/dev/null; then
  alias cat='bat'
  export BAT_THEME="${BAT_THEME:-Monokai Extended}"
  export MANPAGER="sh -c 'col -bx | bat -l man -p'"
  export MANROFFOPT='-c'
fi

# delta: prettier git diffs as git's pager.
command -v delta >/dev/null && export GIT_PAGER='delta'

alias c='clear'
alias q="exit"
alias usedev='use_dev_branch'
alias usemaster='use_master_branch'
alias nbstat=$'netbird status --json | jq -r \'.peers.details[]? | [(.hostname // .fqdn), .netbirdIp, .status] | @tsv\' | column -t -s $\'\\t\''   
alias open-ports="ss -tulpn | grep LISTEN"
alias yeet='yay -Rcs'

# Auto-update zshrc, zsh-functions, and starship config on interactive shell start. Disable with ZSHRC_UPDATE_DISABLED=true.
if [[ -o interactive && "$ZSHRC_UPDATE_DISABLED" != "true" ]]; then
  tmp=/tmp/.zshrc.$$
  if curl -fsSL --connect-timeout 3 --max-time 8 "$ZSHRC_URL" -o "$tmp" 2>/dev/null \
      && [[ -f "$tmp" ]] && zsh -n "$tmp" 2>/dev/null \
      && ! cmp -s "$tmp" "$HOME/.zshrc" 2>/dev/null; then
    echo "Updating zshrc..."
    mv "$tmp" "$HOME/.zshrc" && exec zsh
  fi
  rm -f "$tmp"

  # zsh-init
  itmp=/tmp/.zsh-init.$$
  if curl -fsSL --connect-timeout 3 --max-time 8 "$ZSHRC_GIT/zsh-init.sh" -o "$itmp" 2>/dev/null \
      && [[ -f "$itmp" ]] && ! cmp -s "$itmp" "$ZSH_CONFIG/zsh-init.sh" 2>/dev/null; then
    echo "Updating zsh-init..."
    mv "$itmp" "$ZSH_CONFIG/zsh-init.sh"
  else
    rm -f "$itmp"
  fi

  # zsh-functions
  ftmp=/tmp/.zsh-functions.$$
  if curl -fsSL --connect-timeout 3 --max-time 8 "$ZSHRC_GIT/zsh-functions.zsh" -o "$ftmp" 2>/dev/null \
      && [[ -f "$ftmp" ]] && ! cmp -s "$ftmp" "$ZSH_CONFIG/zsh-functions.zsh" 2>/dev/null; then
    echo "Updating zsh-functions..."
    mv "$ftmp" "$ZSH_CONFIG/zsh-functions.zsh"
  else
    rm -f "$ftmp"
  fi

  # starship config
  if command -v starship >/dev/null; then
    stmp=$(mktemp)
    if curl -fsSL --connect-timeout 3 --max-time 8 "$ZSHRC_GIT/starship.toml" -o "$stmp" 2>/dev/null \
        && [[ -f "$stmp" ]] && ! cmp -s "$stmp" "$HOME/.config/starship.toml" 2>/dev/null; then
      mkdir -p "$HOME/.config"
      mv "$stmp" "$HOME/.config/starship.toml"
    else
      rm -f "$stmp"
    fi
  fi
fi

[[ -f $HOME/.zshrc.local ]] || touch $HOME/.zshrc.local
source $HOME/.zshrc.local

if [[ $FFENABLED != "false" && ( -o interactive || $(tty) == *"pts"* ) ]] && command -v fastfetch >/dev/null; then
  fastfetch --config examples/13
fi
