#!/usr/bin/env bash # setup-caveman.sh — idempotent auto-installer for Caveman + RTK token optimization. # Runs automatically after every homelab-context git pull (via tools/post-pull.sh). # # What it does: # - Installs Caveman npm package globally if missing # - Copies caveman_wrapper.sh → ~/bin/ # - Copies caveman.js wrapper → ~/bin/caveman (CLI entry point) # - Copies templates → ~/templates/ # - Creates ~/bin/ and ~/templates/ dirs if missing # - All operations are idempotent (safe to re-run) # # Works on: macOS (Homebrew node) and Linux (system node) set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" CAVEMAN_DIR="$SCRIPT_DIR/caveman" BIN_DIR="$HOME/bin" TEMPLATES_DIR="$HOME/templates" # Colors for output (only when connected to a terminal) if [ -t 1 ]; then GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m' else GREEN=''; YELLOW=''; BLUE=''; NC='' fi log() { echo -e "${BLUE}[caveman]${NC} $1"; } ok() { echo -e "${GREEN}[caveman]${NC} $1"; } skip() { echo -e "${YELLOW}[caveman]${NC} $1"; } # --- 1. Check / install node + npm --- if ! command -v node &>/dev/null; then echo "[caveman] node.js not found — skipping caveman install" exit 0 fi # --- 2. Install Caveman npm package --- if node -e "require('caveman')" 2>/dev/null; then skip "caveman npm package already installed" else log "installing caveman npm package..." npm install -g caveman 2>&1 | tail -1 ok "caveman npm package installed" fi # --- 3. Create target directories --- mkdir -p "$BIN_DIR" "$TEMPLATES_DIR" # --- 4. Install wrapper script --- install -m 755 "$CAVEMAN_DIR/caveman_wrapper.sh" "$BIN_DIR/caveman_wrapper.sh" ok "caveman_wrapper.sh → $BIN_DIR/caveman_wrapper.sh" # --- 5. Install caveman CLI wrapper --- install -m 755 "$CAVEMAN_DIR/caveman.js" "$BIN_DIR/caveman" ok "caveman.js → $BIN_DIR/caveman" # --- 6. Install templates --- for tmpl in "$CAVEMAN_DIR/templates/"*.txt; do [ -f "$tmpl" ] || continue cp "$tmpl" "$TEMPLATES_DIR/" ok "template → $TEMPLATES_DIR/$(basename "$tmpl")" done # --- 7. Verify --- if [ -x "$BIN_DIR/caveman_wrapper.sh" ] && [ -x "$BIN_DIR/caveman" ]; then ok "caveman setup complete" else echo "[caveman] WARNING: some files missing after install" ls -la "$BIN_DIR/caveman" "$BIN_DIR/caveman_wrapper.sh" 2>&1 fi