#!/bin/bash # ============================================================================== # z-install.sh # # Installs rupa/z ("jump around") for the current user. # # Usage: # curl -fsSL https://curl-pipe-bash.watzinger.biz/z-install.sh | bash # # What it does: # - Downloads z.sh from a pinned rupa/z commit and verifies its SHA-256 # (z has no checksummed releases; bump Z_COMMIT and Z_SHA256 together) # - Installs to ~/.local/share/z/z.sh (skipped if already identical) # - Appends a source line to ~/.bashrc (once) # - Warns if zoxide is still initialised in ~/.bashrc # # Requirements: curl, sha256sum # ============================================================================== set -euo pipefail # ── Configuration ────────────────────────────────────────────────────────────── Z_COMMIT="d37a763a6a30e1b32766fecc3b8ffd6127f8a0fd" Z_SHA256="a74bc5976b7dd1adc9efdda3b2b79eaff2bef7d227cf3139959f8df2b89beb72" Z_URL="https://raw.githubusercontent.com/rupa/z/${Z_COMMIT}/z.sh" INSTALL_PATH="${HOME}/.local/share/z/z.sh" BASHRC="${HOME}/.bashrc" TMP_DIR="$(mktemp -d)" # ── Cleanup on exit ──────────────────────────────────────────────────────────── cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT # ── Helpers ──────────────────────────────────────────────────────────────────── err() { echo "[ERROR] $*" >&2; } info() { echo "[INFO] $*"; } warn() { echo "[WARN] $*" >&2; } # ── Dependency check ─────────────────────────────────────────────────────────── check_dependencies() { local missing=() for cmd in curl sha256sum; do command -v "$cmd" &>/dev/null || missing+=("$cmd") done if [ ${#missing[@]} -gt 0 ]; then err "Missing required tools: ${missing[*]}" err "Install them and re-run this script." exit 1 fi } # ── Install z.sh ─────────────────────────────────────────────────────────────── install_z() { if [[ -f "${INSTALL_PATH}" ]] && echo "${Z_SHA256} ${INSTALL_PATH}" | sha256sum --check --status; then info "z.sh (${Z_COMMIT:0:7}) already installed at ${INSTALL_PATH} - skipping download." return fi info "Downloading z.sh (${Z_COMMIT:0:7})..." curl -fsSL "${Z_URL}" -o "${TMP_DIR}/z.sh" info "Verifying checksum..." if ! echo "${Z_SHA256} ${TMP_DIR}/z.sh" | sha256sum --check --status; then err "Checksum mismatch for ${Z_URL}." exit 1 fi info "Checksum verified OK." install -D -m 0644 "${TMP_DIR}/z.sh" "${INSTALL_PATH}" info "Installed: ${INSTALL_PATH}" } # ── Source z.sh from ~/.bashrc ───────────────────────────────────────────────── register_in_bashrc() { local marker="# z-install.sh" if grep -q "${marker}" "${BASHRC}" 2>/dev/null; then info "${BASHRC} already sources ${INSTALL_PATH}." else info "Adding source line to ${BASHRC}..." cat >> "${BASHRC}" </dev/null; then warn "${BASHRC} still runs 'zoxide init'; remove it so 'z' is rupa/z." fi info "Apply in the current shell with:" info " source ${INSTALL_PATH}" } # ── Main ─────────────────────────────────────────────────────────────────────── main() { info "=== z (jump around) installer ===" check_dependencies install_z register_in_bashrc info "Done." } main "$@"