# ~/.zshrc
# managed-by: install.sh  — re-run the installer to update this file.
# Machine-local additions belong in ~/.zshrc.local (never overwritten).

# ---------------------------------------------------------------------------
# Oh My Zsh
# ---------------------------------------------------------------------------
export ZSH="${ZSH:-$HOME/.oh-my-zsh}"
export ZSH_CUSTOM="${ZSH_CUSTOM:-$ZSH/custom}"
ZSH_THEME="${ZSH_THEME:-robbyrussell}"

# ---------------------------------------------------------------------------
# History
# ---------------------------------------------------------------------------
HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000
setopt EXTENDED_HISTORY        # timestamps in the history file
setopt HIST_IGNORE_ALL_DUPS    # no duplicate lines
setopt HIST_IGNORE_SPACE       # commands starting with a space stay out
setopt HIST_REDUCE_BLANKS
setopt SHARE_HISTORY           # several shells share one history
setopt APPEND_HISTORY

# ---------------------------------------------------------------------------
# Options & completion
# ---------------------------------------------------------------------------
setopt AUTO_CD                 # `..`, `~/x` without cd
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT
setopt INTERACTIVE_COMMENTS
setopt NO_BEEP
setopt PROMPT_SUBST

zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
[[ -n "${LS_COLORS:-}" ]] && zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"

# ---------------------------------------------------------------------------
# Plugins (all bundled with Oh My Zsh unless installed by the script)
# ---------------------------------------------------------------------------
plugins=(git z extract sudo command-not-found)

[[ -d "$ZSH_CUSTOM/plugins/zsh-completions" ]]      && plugins+=(zsh-completions)
[[ -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]]  && plugins+=(zsh-autosuggestions)

if [[ -f "$ZSH/oh-my-zsh.sh" ]]; then
  source "$ZSH/oh-my-zsh.sh"
else
  # Oh My Zsh missing: stay usable instead of exploding.
  PROMPT='%F{cyan}%n@%m%f %F{blue}%~%f %# '
  autoload -Uz compinit && compinit -i
fi

# Syntax highlighting must be sourced last of the highlighters.
[[ -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]] &&
  source "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"

# ---------------------------------------------------------------------------
# Key bindings (emacs style + history substring search on arrows)
# ---------------------------------------------------------------------------
if [[ -o interactive ]]; then
  bindkey -e
  bindkey '^[[A' history-search-backward
  bindkey '^[[B' history-search-forward
  bindkey '^[[1;5C' forward-word        # ctrl-right
  bindkey '^[[1;5D' backward-word       # ctrl-left
  bindkey '^[[H' beginning-of-line      # home
  bindkey '^[[F' end-of-line            # end
fi

# ---------------------------------------------------------------------------
# Environment
# ---------------------------------------------------------------------------
export EDITOR="${EDITOR:-vim}"
export VISUAL="${VISUAL:-$EDITOR}"
export PAGER="${PAGER:-less}"
export LESS='-R -F -X -i -M'

typeset -U path
path=("$HOME/.local/bin" "$HOME/bin" $path)
export PATH

# ---------------------------------------------------------------------------
# Aliases
# ---------------------------------------------------------------------------
alias ll='ls -alFh'
alias la='ls -Ah'
alias l='ls -CFh'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias reload='source ~/.zshrc'
alias zshconfig='$EDITOR ~/.zshrc'
alias szsh='$EDITOR ~/.zshrc.local'

alias g='git'
alias gs='git status -sb'
alias ga='git add'
alias gc='git commit -m'
alias gd='git diff'
alias gco='git checkout'
alias gb='git branch'
alias gl='git log --oneline --graph --decorate -20'
alias gp='git pull --rebase --autostash'
alias gP='git push'

alias df='df -h'
alias du='du -sh'
alias ports='ss -tulpn 2>/dev/null || netstat -tulpn'
alias myip='curl -fsS https://ifconfig.me; echo'
alias week='date +%V'

# ---------------------------------------------------------------------------
# Functions
# ---------------------------------------------------------------------------
# mkcd <dir> — create and enter a directory.
mkcd() {
  [[ -n "${1:-}" ]] || { print -u2 'usage: mkcd <dir>'; return 1; }
  mkdir -p -- "$1" && cd -- "$1"
}

# serve [port] — quick static HTTP server in the current directory.
serve() {
  local port="${1:-8000}"
  if command -v python3 >/dev/null 2>&1; then
    print -r -- "http://0.0.0.0:$port  (ctrl-c to stop)"
    python3 -m http.server "$port"
  elif command -v python >/dev/null 2>&1; then
    print -r -- "http://0.0.0.0:$port  (ctrl-c to stop)"
    python -m SimpleHTTPServer "$port"
  else
    print -u2 'serve: python not found'
    return 1
  fi
}

# port <n> — what is listening on that port.
port() {
  [[ -n "${1:-}" ]] || { print -u2 'usage: port <number>'; return 1; }
  lsof -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null || ss -tulpn 2>/dev/null | grep -F ":$1 "
}

# ---------------------------------------------------------------------------
# Machine-local overrides (the installer never touches this file)
# ---------------------------------------------------------------------------
[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local"
