#!/bin/sh # PocketClaw Watch installer # Source: https://pocketclaw.dev/watch/install.sh (docs: https://pocketclaw.dev/watch) # # curl -fsSL https://pocketclaw.dev/watch/install.sh | sh -s -- # installs the agent for the current user (no root), stores the token, # schedules a run every 6 hours (user crontab, or a systemd user timer # when there is no crontab) and sends a first report # curl -fsSL https://pocketclaw.dev/watch/install.sh | sh -s -- --dry-run # downloads the agent to a temporary folder and prints what it would # send; installs nothing, sends nothing # curl -fsSL https://pocketclaw.dev/watch/install.sh | sh -s -- --uninstall # removes the schedule, the token and the agent # # Files it writes (and nothing else): # ~/.local/share/pocketclaw-watch/pocketclaw-watch.sh the agent # ~/.config/pocketclaw-watch/token the machine token, mode 600 # one line in your crontab, or ~/.config/systemd/user/pocketclaw-watch.{service,timer} set -eu LC_ALL=C export LC_ALL BASE="${POCKETCLAW_WATCH_BASE:-https://pocketclaw.dev}" DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/pocketclaw-watch" CONF_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/pocketclaw-watch" UNIT_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user" AGENT="$DATA_DIR/pocketclaw-watch.sh" have() { command -v "$1" >/dev/null 2>&1; } die() { echo "pocketclaw-watch: $*" >&2; exit 1; } MODE=install TOKEN="" for arg in "$@"; do case "$arg" in --dry-run) MODE=dry ;; --uninstall) MODE=uninstall ;; pcw_*) TOKEN="$arg" ;; *) die "unknown argument. Usage: sh -s -- | --dry-run | --uninstall" ;; esac done have curl || die "curl is required." fetch_agent() { curl -fsSL "$BASE/watch/pocketclaw-watch.sh" -o "$1" || die "could not download the agent from $BASE." head -n 1 "$1" | grep -q '^#!/bin/sh' || die "the download is not the agent script." } if [ "$MODE" = dry ]; then tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT INT TERM fetch_agent "$tmp/pocketclaw-watch.sh" sh "$tmp/pocketclaw-watch.sh" --dry-run exit 0 fi if [ "$MODE" = uninstall ]; then if [ -f "$AGENT" ]; then sh "$AGENT" uninstall else echo "Nothing installed for this user ($AGENT not found)." fi exit 0 fi [ -n "$TOKEN" ] || die "give the machine token: sh -s -- pcw_... (from https://pocketclaw.dev/dashboard/watch)" printf '%s' "$TOKEN" | grep -Eq '^pcw_[A-Za-z0-9_-]{32}$' || die "that does not look like a machine token." if [ "$(id -u)" = "0" ]; then echo "Note: running as root. Watch needs no root; install it as the user that runs the agent if you can." >&2 fi umask 077 mkdir -p "$DATA_DIR" "$CONF_DIR" fetch_agent "$AGENT" chmod 700 "$AGENT" printf '%s\n' "$TOKEN" > "$CONF_DIR/token" chmod 600 "$CONF_DIR/token" # A random minute and hour offset, so machines do not all report at once. rnd=$(od -An -N2 -tu2 /dev/urandom 2>/dev/null | tr -d ' ' || echo $$) min=$((rnd % 60)) h=$(((rnd / 60) % 6)) hours="$h,$((h + 6)),$((h + 12)),$((h + 18))" scheduled=no if have crontab; then current=$(crontab -l 2>/dev/null || true) if { printf '%s\n' "$current" | grep -v 'pocketclaw-watch' | sed '/^$/d' || true echo "$min $hours * * * sh \"$AGENT\" run >/dev/null 2>&1 # pocketclaw-watch" } | crontab - 2>/dev/null; then scheduled=cron echo "Scheduled: every 6 hours from your crontab ($min $hours * * *)." else echo "Your crontab could not be written; trying a systemd user timer." >&2 fi fi if [ "$scheduled" = no ] && have systemctl && systemctl --user show-environment >/dev/null 2>&1; then mkdir -p "$UNIT_DIR" cat > "$UNIT_DIR/pocketclaw-watch.service" < "$UNIT_DIR/pocketclaw-watch.timer" </dev/null 2>&1; then scheduled=systemd echo "Scheduled: systemd user timer pocketclaw-watch.timer, every 6 hours." echo "A user timer only runs while you are logged in, unless lingering is on: loginctl enable-linger \"$(id -un)\"" fi fi if [ "$scheduled" = no ]; then echo "No schedule installed (no usable crontab or systemd user session): run \"sh $AGENT run\" from your own scheduler every 6 hours." >&2 fi echo "Sending a first report..." if sh "$AGENT" run; then echo "Done. See it on $BASE/dashboard/watch" else echo "The first report did not go through (see above). The schedule stays in place." >&2 fi echo "What is sent, and never sent: $BASE/watch Uninstall: sh \"$AGENT\" uninstall"