#!/usr/bin/env bash # install.sh — junkpile-side installer for chat-saiden. # Idempotent. Robust to brew warnings. # # Prereq: tunnel credentials already copied to /etc/cloudflared/chat-saiden/chat-saiden.json # (one-shot scp from fuji: see cloudflare/provision-tunnel.sh) set -uo pipefail REPO_DIR="$(cd "$(dirname "$0")" && pwd)" echo "Installing chat-saiden from: $REPO_DIR" echo # --- 1. ttyd binary --- if ! command -v ttyd >/dev/null 2>&1; then echo "[1/4] Installing ttyd via brew" if brew install ttyd; then echo " ttyd install succeeded" else rc=$? if command -v ttyd >/dev/null 2>&1; then echo " brew exited $rc but ttyd is present, continuing" else echo "ERROR: ttyd not installed (brew rc=$rc)" exit 1 fi fi else echo "[1/4] ttyd already present: $(ttyd --version 2>&1 | head -1)" fi # --- 2. wrapper script --- echo "[2/4] Installing wrapper" mkdir -p "$HOME/.local/bin" install -m 0755 "$REPO_DIR/junkpile/ttyd-wrapper.sh" "$HOME/.local/bin/ttyd-wrapper.sh" echo " → $HOME/.local/bin/ttyd-wrapper.sh" # --- 3. tunnel config --- echo "[3/4] Installing tunnel config" sudo mkdir -p /etc/cloudflared/chat-saiden sudo install -m 0644 -o root -g chi "$REPO_DIR/junkpile/chat-saiden.yml" /etc/cloudflared/chat-saiden/chat-saiden.yml echo " → /etc/cloudflared/chat-saiden/chat-saiden.yml" if [[ ! -f /etc/cloudflared/chat-saiden/chat-saiden.json ]]; then echo " WARNING: credentials JSON missing at /etc/cloudflared/chat-saiden/chat-saiden.json" echo " Provision it via cloudflare/provision-tunnel.sh from fuji." fi # --- 4. systemd units --- echo "[4/4] Installing systemd units" sudo install -m 0644 "$REPO_DIR/junkpile/ttyd-chat.service" /etc/systemd/system/ttyd-chat.service sudo install -m 0644 "$REPO_DIR/junkpile/cloudflared-chat-saiden.service" /etc/systemd/system/cloudflared-chat-saiden.service sudo systemctl daemon-reload echo " → /etc/systemd/system/ttyd-chat.service" echo " → /etc/systemd/system/cloudflared-chat-saiden.service" echo echo "============================================================" echo " INSTALL DONE — verify:" echo "============================================================" systemctl status ttyd-chat.service --no-pager 2>&1 | head -3 || true systemctl status cloudflared-chat-saiden.service --no-pager 2>&1 | head -3 || true echo echo "============================================================" echo " NEXT STEPS" echo "============================================================" echo " A. Verify credentials present:" echo " sudo ls -la /etc/cloudflared/chat-saiden/" echo " # should show chat-saiden.json (0640 root:chi) + chat-saiden.yml" echo echo " B. Configure CF Access (browser, one-time):" echo " see $REPO_DIR/cloudflare/dashboard-setup.md (Part B + WebSocket only;" echo " tunnel half is already done via cloudflare/provision-tunnel.sh)" echo echo " C. Start the services:" echo " sudo systemctl enable --now ttyd-chat" echo " sudo systemctl enable --now cloudflared-chat-saiden" echo echo " D. Smoke-test:" echo " curl -sI https://chat.saiden.dev # expect CF Access redirect" echo "============================================================"