#!/bin/zsh

# -------------------------
# Resolve this config's real directory
# -------------------------

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
  symlink_dir="$(cd "$(dirname "$(readlink -f "${(%):-%N}")")" && pwd)"
else
  # macOS does not ship GNU readlink -f by default.
  symlink_dir="$(cd "$(dirname "$(readlink "${(%):-%N}")")" && pwd)"
fi

# -------------------------
# Private, machine-local configuration
# -------------------------
# These files are intentionally not part of this repository.
# Use them for secrets, tokens, private exports, work paths, and host-specific setup.

[[ -f "$HOME/.bash_private" ]] && source "$HOME/.bash_private"
[[ -f "$HOME/.private" ]] && source "$HOME/.private"
[[ -f "$HOME/.zsh_private" ]] && source "$HOME/.zsh_private"

# Do not source ~/.inputrc here.
# It is a readline config file, not shell code.

# -------------------------
# OS-specific configuration
# -------------------------

if [[ "$OSTYPE" == "darwin"* ]]; then
  [[ -f "$HOME/.macrc" ]] && source "$HOME/.macrc"
fi

# -------------------------
# Local directories
# -------------------------

if [[ ! -d "$HOME/projects" ]]; then
  echo "Making $HOME/projects"
  mkdir -p "$HOME/projects"
fi

if command -v go >/dev/null 2>&1; then
  mkdir -p "$HOME/.go"
fi

# -------------------------
# Git completion
# -------------------------
# Auto-installs Git completion files from a pinned Git commit.
# This avoids fetching from a moving branch like master.
#
# To fill in the placeholders:
#
#   cd /tmp
#   git clone https://github.com/git/git.git git-src
#   cd git-src
#   git checkout v2.45.2
#   git rev-parse HEAD
#   shasum -a 256 contrib/completion/git-completion.bash
#   shasum -a 256 contrib/completion/git-completion.zsh
#
# Then paste the commit and hashes below.


ZSH_CACHE_DIR="$HOME/.zsh"
mkdir -p "$ZSH_CACHE_DIR"

GIT_COMPLETION_COMMIT="bea9ecd24b0c3bf06cab4a851694fe09e7e51408"

GIT_COMPLETION_BASH_URL="https://raw.githubusercontent.com/git/git/${GIT_COMPLETION_COMMIT}/contrib/completion/git-completion.bash"
GIT_COMPLETION_ZSH_URL="https://raw.githubusercontent.com/git/git/${GIT_COMPLETION_COMMIT}/contrib/completion/git-completion.zsh"

GIT_COMPLETION_BASH_FILE="$ZSH_CACHE_DIR/git-completion.bash"
GIT_COMPLETION_ZSH_FILE="$ZSH_CACHE_DIR/_git"

GIT_COMPLETION_BASH_SHA256="e667d8fdc0f0071833c94ccb2565a503bf413da0ae28d74efe2e70e2beb1c4c6"
GIT_COMPLETION_ZSH_SHA256="f6075d283250785edcf09281992bde6b4bcc43eb2dca74488d880a52f7659c09"

_zshv2_sha256() {
  if command -v sha256sum >/dev/null 2>&1; then
    sha256sum "$1" | awk '{print $1}'
  elif command -v shasum >/dev/null 2>&1; then
    shasum -a 256 "$1" | awk '{print $1}'
  else
    echo "No SHA-256 tool found. Need sha256sum or shasum." >&2
    return 1
  fi
}

_zshv2_file_has_sha256() {
  local file="$1"
  local expected_sha="$2"
  local actual_sha

  [[ -f "$file" ]] || return 1

  actual_sha="$(_zshv2_sha256 "$file")" || return 1
  [[ "$actual_sha" == "$expected_sha" ]]
}

_zshv2_install_verified_file() {
  local name="$1"
  local url="$2"
  local dest="$3"
  local expected_sha="$4"
  local tmp_file
  local actual_sha

  if [[ -z "$url" || -z "$dest" || -z "$expected_sha" ]]; then
    echo "Git completion install misconfigured for $name." >&2
    return 1
  fi

  if _zshv2_file_has_sha256 "$dest" "$expected_sha"; then
    return 0
  fi

  if ! command -v curl >/dev/null 2>&1; then
    echo "curl is required to install $name." >&2
    return 1
  fi

  tmp_file="$(mktemp "${dest}.tmp.XXXXXX")" || return 1

  if ! curl --fail --location --silent --show-error --output "$tmp_file" "$url"; then
    echo "Failed to download $name from $url" >&2
    rm -f "$tmp_file"
    return 1
  fi

  actual_sha="$(_zshv2_sha256 "$tmp_file")" || {
    rm -f "$tmp_file"
    return 1
  }

  if [[ "$actual_sha" != "$expected_sha" ]]; then
    echo "SHA-256 mismatch for $name." >&2
    echo "Expected: $expected_sha" >&2
    echo "Actual:   $actual_sha" >&2
    rm -f "$tmp_file"
    return 1
  fi

  mv "$tmp_file" "$dest"
}

_zshv2_install_git_completion() {
  _zshv2_install_verified_file \
    "git-completion.bash" \
    "$GIT_COMPLETION_BASH_URL" \
    "$GIT_COMPLETION_BASH_FILE" \
    "$GIT_COMPLETION_BASH_SHA256"

  _zshv2_install_verified_file \
    "git-completion.zsh" \
    "$GIT_COMPLETION_ZSH_URL" \
    "$GIT_COMPLETION_ZSH_FILE" \
    "$GIT_COMPLETION_ZSH_SHA256"
}

_zshv2_install_git_completion

if [[ -f "$GIT_COMPLETION_BASH_FILE" ]]; then
  zstyle ':completion:*:*:git:*' script "$GIT_COMPLETION_BASH_FILE"
fi

fpath=("$ZSH_CACHE_DIR" $fpath)

autoload -Uz compinit
compinit

# -------------------------
# Pull in modular config
# -------------------------

source "$symlink_dir/aliases.zsh"
source "$symlink_dir/functions.zsh"
source "$symlink_dir/exports.zsh"
source "$symlink_dir/initializations.zsh"
source "$symlink_dir/ps1.zsh"
source "$symlink_dir/keybinds.zsh"

# -------------------------
# Language/tool initialization
# -------------------------

# Rust
[[ -f "$HOME/.cargo/env" ]] && source "$HOME/.cargo/env"

# Haskell / GHCup
[[ -f "$HOME/.ghcup/env" ]] && source "$HOME/.ghcup/env"

# OCaml / opam
[[ -r "$HOME/.opam/opam-init/init.sh" ]] && . "$HOME/.opam/opam-init/init.sh" >/dev/null 2>/dev/null || true

# -------------------------
# ZSH syntax highlighting
# -------------------------
# This still supports an existing local clone under:
#
#   $HOME/.zsh/zsh-syntax-highlighting
#
# But it does not automatically clone during shell startup.
# Install with:
#
#   brew install zsh-syntax-highlighting
#
# or:
#
#   sudo apt install zsh-syntax-highlighting
#
# It must be sourced last.

if [[ -f "$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
  source "$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
  source "/opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
  source "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
  source "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
fi

# -------------------------
# Deduplicate PATH
# -------------------------

typeset -U path PATH
