#!/bin/bash # # Purpose : Find the command-line tools some items need (restic and rclone # for backups; Node.js and Anthropic's sandbox runtime, srt, for the # agent sandbox), and offer to download them. Source this file after # lib/json.sh; it uses confirm_yes from configure. # # A tool already installed, e.g. with Homebrew, is used as it is. Otherwise # it's downloaded from its maker's releases into TOOLS, checked against the # SHA-256 sums the release publishes, and kept to itself there: nothing in # /usr/local or your shell changes. These are command-line programs, not # apps, so they aren't notarized; downloaded with curl, Gatekeeper doesn't # quarantine them. TOOLS="$HOME/.local/share/federated-connector" TOOLS_BIN="$TOOLS/bin" # Where to look for a tool, in order. launchd jobs start with a short PATH, # so Homebrew's directories are named too. TOOLS_PATH="$TOOLS_BIN:$TOOLS/node/bin:$TOOLS/srt/bin:/opt/homebrew/bin:/usr/local/bin" # The full path of a tool, if it's installed tool_path() { PATH="$TOOLS_PATH:$PATH" command -v "$1" } # restic and rclone name the architectures differently from Node machine_arch() { case "$(uname -m)" in arm64) echo arm64 ;; *) echo amd64 ;; esac } # download_checked download_checked() { local url="$1" sums_url="$2" name="$3" dest="$4" expected actual echo "Downloading $url" curl -fL --progress-bar --max-time 1800 -o "$dest" "$url" || return 1 expected="$(curl -fsL --max-time 30 "$sums_url" | awk -v f="$name" '$2 == f || $2 == "*" f { print $1; exit }')" actual="$(shasum -a 256 "$dest" | cut -d' ' -f1)" if [ -z "$expected" ] || [ "$expected" != "$actual" ]; then echo "${RED}The download doesn't match the checksum its release publishes. Not installing it.$RESET" rm -f "$dest" return 1 fi } install_restic() { local tmp version name status=1 version="$(curl -fsL --max-time 30 https://api.github.com/repos/restic/restic/releases/latest \ | json_get 'd.tag_name.replace(/^v/, "")')" || { echo "Couldn't find restic's latest release."; return 1; } name="restic_${version}_darwin_$(machine_arch).bz2" tmp="$(mktemp -d)" || return 1 if download_checked "https://github.com/restic/restic/releases/download/v$version/$name" \ "https://github.com/restic/restic/releases/download/v$version/SHA256SUMS" "$name" "$tmp/$name" \ && bunzip2 "$tmp/$name" && mkdir -p "$TOOLS_BIN" \ && install -m 755 "$tmp/${name%.bz2}" "$TOOLS_BIN/restic"; then status=0 fi rm -rf "$tmp" return $status } install_rclone() { local tmp version name status=1 version="$(curl -fsL --max-time 30 https://downloads.rclone.org/version.txt | sed -n 's/^rclone v//p')" [ -n "$version" ] || { echo "Couldn't find rclone's latest release."; return 1; } name="rclone-v$version-osx-$(machine_arch).zip" tmp="$(mktemp -d)" || return 1 if download_checked "https://downloads.rclone.org/v$version/$name" \ "https://downloads.rclone.org/v$version/SHA256SUMS" "$name" "$tmp/$name" \ && unzip -q "$tmp/$name" -d "$tmp" && mkdir -p "$TOOLS_BIN" \ && install -m 755 "$tmp/${name%.zip}/rclone" "$TOOLS_BIN/rclone"; then status=0 fi rm -rf "$tmp" return $status } # srt needs Node.js 22.12 or later node_ok() { local node version node="$(tool_path node)" || return 1 version="$("$node" -p 'process.versions.node' 2> /dev/null)" || return 1 [ "$(printf '%s\n' 22.12.0 "$version" | sort -t. -k1,1n -k2,2n -k3,3n | head -n 1)" = 22.12.0 ] } # The current long-term support release, kept in TOOLS/node install_node() { local tmp version arch name status=1 version="$(curl -fsL --max-time 30 https://nodejs.org/dist/index.json \ | json_get 'd.filter(function (x) { return x.lts; })[0].version')" \ || { echo "Couldn't find Node.js' current release."; return 1; } case "$(uname -m)" in arm64) arch=arm64 ;; *) arch=x64 ;; esac name="node-$version-darwin-$arch.tar.gz" tmp="$(mktemp -d)" || return 1 if download_checked "https://nodejs.org/dist/$version/$name" \ "https://nodejs.org/dist/$version/SHASUMS256.txt" "$name" "$tmp/$name" \ && tar -xzf "$tmp/$name" -C "$tmp" && mkdir -p "$TOOLS"; then rm -rf "$TOOLS/node" && mv "$tmp/${name%.tar.gz}" "$TOOLS/node" && status=0 fi rm -rf "$tmp" return $status } # srt from npm, into TOOLS/srt, with the Node.js found or installed. It has no # install scripts of its own; the ones npm would warn it blocked belong to a # Windows-only package it optionally depends on. install_srt() { local npm node_ok || install_node || return 1 npm="$(dirname "$(tool_path node)")/npm" PATH="$(dirname "$npm"):$PATH" "$npm" install --global --prefix "$TOOLS/srt" \ --no-fund --no-audit --ignore-scripts @anthropic-ai/sandbox-runtime > /dev/null } tool_title() { case "$1" in restic) echo "restic (backups)" ;; rclone) echo "rclone (reaches your Nextcloud files for restic)" ;; node) echo "Node.js (runs the sandbox)" ;; srt) echo "the sandbox runtime (srt, from Anthropic)" ;; esac } # tool_ensure ...: offer to install the tools that are missing. Fails if # any is still missing. tool_ensure() { local name missing=() for name in "$@"; do tool_path "$name" > /dev/null || missing+=("$name") done [ ${#missing[@]} -eq 0 ] && return 0 echo "This needs:" for name in "${missing[@]}"; do echo " - $(tool_title "$name")" done confirm_yes "Download and install them (into $TOOLS)?" || return 1 for name in "${missing[@]}"; do "install_$name" || { echo "$RED$(tool_title "$name") was not installed.$RESET"; return 1; } echo "Installed $(tool_title "$name")." done }