#!/usr/bin/env bash # Install easyzsh shell stubs into a home directory. # Overwrites: ~/.zshenv ~/.zshrc ~/.zprofile # Never overwrites: ~/.config/zsh/local.zsh ~/.config/zshrc/** ~/.local/bin/** # # Usage: # ./deploy.sh # ./deploy.sh --home /path/to/home # ./deploy.sh --migrate-zprofile # fold pre-easyzsh ~/.zprofile into local.zsh set -euo pipefail ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) TARGET_HOME="${HOME}" MIGRATE_ZPROFILE=0 while [[ $# -gt 0 ]]; do case "$1" in --home) TARGET_HOME="$2" shift 2 ;; --migrate-zprofile) MIGRATE_ZPROFILE=1 shift ;; -h|--help) sed -n '2,12p' "$0" exit 0 ;; *) echo "Unknown argument: $1" >&2 exit 2 ;; esac done if [[ ! -d "$TARGET_HOME" ]]; then echo "Home does not exist: $TARGET_HOME" >&2 exit 1 fi ZSH_CFG="${TARGET_HOME}/.config/zsh" LOCAL_ZSH="${ZSH_CFG}/local.zsh" ts=$(date +%Y%m%d-%H%M%S) backup_file() { local f="$1" if [[ -e "$f" || -L "$f" ]]; then cp -Pp "$f" "${f}.backup.${ts}" echo "Backed up $f -> ${f}.backup.${ts}" fi } file_has_content() { [[ -f "$1" ]] && grep -q '[^[:space:]]' "$1" } echo "Deploying easyzsh shell stubs into $TARGET_HOME" mkdir -p \ "${ZSH_CFG}" \ "${TARGET_HOME}/.config/zshrc" \ "${TARGET_HOME}/.cache/zsh" \ "${TARGET_HOME}/.local/bin" # Migrate legacy fragments into local.zsh once. if [[ ! -f "$LOCAL_ZSH" ]]; then tmp=$(mktemp) : >"$tmp" if file_has_content "${TARGET_HOME}/.config/zshenv.local"; then cat "${TARGET_HOME}/.config/zshenv.local" >>"$tmp" printf '\n' >>"$tmp" fi if [[ -d "${ZSH_CFG}/local" ]]; then for part in env.local.zsh login.local.zsh interactive.local.zsh; do if file_has_content "${ZSH_CFG}/local/${part}"; then cat "${ZSH_CFG}/local/${part}" >>"$tmp" printf '\n' >>"$tmp" fi done fi if file_has_content "$tmp"; then cp "$tmp" "$LOCAL_ZSH" echo "Migrated machine-local fragments -> ~/.config/zsh/local.zsh" fi rm -f "$tmp" fi # Fold a pre-easyzsh ~/.zprofile into local.zsh. Skip easyzsh stub / empty files. if [[ "$MIGRATE_ZPROFILE" -eq 1 && -f "${TARGET_HOME}/.zprofile" ]]; then if grep -Eq 'easyzsh|PATH lives in|Machine-local env' "${TARGET_HOME}/.zprofile" 2>/dev/null \ || grep -Fq 'Leave this file empty' "${TARGET_HOME}/.zprofile" 2>/dev/null; then echo "Skipping zprofile migration: already easyzsh-managed" elif [[ ! -f "$LOCAL_ZSH" ]]; then zprofile_extract=$(mktemp) awk ' /Created by `pipx`/ { skip_pipx=1; next } skip_pipx && /local\/bin/ { skip_pipx=0; next } skip_pipx { next } { print } ' "${TARGET_HOME}/.zprofile" >"$zprofile_extract" if file_has_content "$zprofile_extract"; then cp "$zprofile_extract" "$LOCAL_ZSH" echo "Migrated ~/.zprofile body -> ~/.config/zsh/local.zsh" fi rm -f "$zprofile_extract" fi fi backup_file "${TARGET_HOME}/.zshenv" backup_file "${TARGET_HOME}/.zshrc" backup_file "${TARGET_HOME}/.zprofile" install -m 644 "${ROOT}/.zshenv" "${TARGET_HOME}/.zshenv" install -m 644 "${ROOT}/.zshrc" "${TARGET_HOME}/.zshrc" install -m 644 "${ROOT}/.zprofile" "${TARGET_HOME}/.zprofile" # Remove obsolete managed-tree layout and examples from earlier deploys. /bin/rm -rf \ "${ZSH_CFG}/env" \ "${ZSH_CFG}/interactive" \ "${ZSH_CFG}/login" \ "${ZSH_CFG}/local" /bin/rm -f \ "${ZSH_CFG}/local.zsh.example" \ "${TARGET_HOME}/.config/zshenv.local" # Ensure intentional grok shim when the binary tree exists. if [[ -e "${TARGET_HOME}/.grok/bin/grok" ]]; then ln -sfn "${TARGET_HOME}/.grok/bin/grok" "${TARGET_HOME}/.local/bin/grok" echo "Ensured ~/.local/bin/grok -> ~/.grok/bin/grok" fi echo "Deploy complete." echo "Verify: env -i HOME=\"$TARGET_HOME\" PATH=/usr/bin:/bin zsh -c '. ~/.zshenv; command -v grok'"