#!/bin/bash
# Install the 5-Minute Timer: stage the self-contained web app + a `timer` launcher.
set -e

APP_DIR="$HOME/.local/share/adom-timer"
BIN_DIR="$HOME/.local/bin"
MOD_DIR="$(cd "$(dirname "$0")" && pwd)"

mkdir -p "$APP_DIR" "$BIN_DIR"

# The timer is a single self-contained HTML file — no build, no server deps.
cp "$MOD_DIR/index.html" "$APP_DIR/index.html"

# Launcher: serve the app on a free port and print the URL.
cat > "$BIN_DIR/timer" <<'EOF'
#!/bin/bash
# Serve the 5-Minute Timer locally and print its URL.
APP_DIR="$HOME/.local/share/adom-timer"
PORT="${TIMER_PORT:-8741}"
echo "Serving 5-Minute Timer at http://localhost:$PORT/"
echo "(Ctrl-C to stop. Override the port with TIMER_PORT=NNNN timer.)"
exec python3 -m http.server "$PORT" --directory "$APP_DIR"
EOF
chmod +x "$BIN_DIR/timer"

# Install the skill so Claude Code knows about the app.
SKILL_DIR="$HOME/.claude/skills/timer"
mkdir -p "$SKILL_DIR"
cp "$MOD_DIR/SKILL.md" "$SKILL_DIR/SKILL.md"

echo "OK: 5-Minute Timer installed."
echo "  - Run:  timer            (serves it locally, opens on a port)"
echo "  - File: $APP_DIR/index.html  (open directly in any browser)"
case ":$PATH:" in *":$BIN_DIR:"*) ;; *) echo "  note: add $BIN_DIR to PATH to use the 'timer' command." ;; esac
