#!/bin/sh # Install `sophia` (and `sophia-mcp` beside it) from this repository's GitHub # release, for this OS and CPU. # # curl -fsSL https://raw.githubusercontent.com/sophia-labs/sophia-mcp/main/scripts/install.sh | sh # # Reads releases/latest/download/release.json, downloads the archive it names # for this target and SHA256SUMS, checks the archive's SHA-256 (sha256sum or # shasum) BEFORE unpacking, and refuses on a mismatch. Installs into # ~/.local/bin, or $SOPHIA_INSTALL_DIR. No sudo. Running it again when that # version is already installed changes nothing. Prints the PATH line to add # when the directory is not on PATH. # # SOPHIA_INSTALL_DIR where to install (default ~/.local/bin) # SOPHIA_MCP_UPDATE_URL release base (default https://github.com/sophia-labs/sophia-mcp) set -eu base="${SOPHIA_MCP_UPDATE_URL:-https://github.com/sophia-labs/sophia-mcp}" base="${base%/}" dir="${SOPHIA_INSTALL_DIR:-${HOME:?HOME is not set}/.local/bin}" say() { printf '%s\n' "$*" >&2; } die() { say "install.sh: $*"; exit 1; } case "$(uname -s)" in Linux) os=unknown-linux-gnu ;; Darwin) os=apple-darwin ;; *) die "no sophia release is built for $(uname -s); build from source with cargo" ;; esac case "$(uname -m)" in x86_64 | amd64) cpu=x86_64 ;; aarch64 | arm64) cpu=aarch64 ;; *) die "no sophia release is built for $(uname -m); build from source with cargo" ;; esac target="$cpu-$os" if command -v curl >/dev/null 2>&1; then get() { curl -fsSL "$1" -o "$2"; } elif command -v wget >/dev/null 2>&1; then get() { wget -q "$1" -O "$2"; } else die "needs curl or wget" fi if command -v sha256sum >/dev/null 2>&1; then sha256() { sha256sum "$1" | cut -d' ' -f1; } elif command -v shasum >/dev/null 2>&1; then sha256() { shasum -a 256 "$1" | cut -d' ' -f1; } else die "needs sha256sum or shasum to check the download" fi tmp="$(mktemp -d "${TMPDIR:-/tmp}/sophia-install.XXXXXX")" trap 'rm -rf "$tmp"' EXIT INT TERM get "$base/releases/latest/download/release.json" "$tmp/release.json" \ || die "cannot read $base/releases/latest/download/release.json" # release.json is small and flat: {"name", "version", "assets": {target: file}}. flat="$(tr -d '\n\r\t ' <"$tmp/release.json")" version="$(printf '%s' "$flat" | sed -n 's/.*"version":"\([^"]*\)".*/\1/p')" asset="$(printf '%s' "$flat" | sed -n "s/.*\"$target\":\"\([^\"]*\)\".*/\1/p")" [ -n "$version" ] || die "release.json names no version" [ -n "$asset" ] || die "release $version has no build for $target" case "$asset" in */* | *..*) die "refusing the asset name $asset" ;; esac if [ -x "$dir/sophia" ] && [ "$("$dir/sophia" --version 2>/dev/null || true)" = "sophia $version" ]; then say "sophia $version is already installed in $dir." else rel="$base/releases/download/v$version" get "$rel/SHA256SUMS" "$tmp/SHA256SUMS" || die "cannot read $rel/SHA256SUMS" want="$(awk -v f="$asset" '{n=$2; sub(/^\*/, "", n); if (n == f) print tolower($1)}' "$tmp/SHA256SUMS")" [ -n "$want" ] || die "SHA256SUMS lists no checksum for $asset" get "$rel/$asset" "$tmp/$asset" || die "cannot download $rel/$asset" have="$(sha256 "$tmp/$asset")" [ "$have" = "$want" ] || die "checksum mismatch for $asset: expected $want, got $have. Nothing was installed." mkdir -p "$tmp/x" tar -xzf "$tmp/$asset" -C "$tmp/x" src="$(find "$tmp/x" -type f -name sophia | head -n 1)" [ -n "$src" ] || die "$asset holds no sophia binary" mkdir -p "$dir" for name in sophia sophia-mcp; do file="$(dirname "$src")/$name" [ -f "$file" ] || continue cp "$file" "$dir/.$name.new.$$" chmod 755 "$dir/.$name.new.$$" mv -f "$dir/.$name.new.$$" "$dir/$name" done say "Installed sophia $version into $dir." fi case ":${PATH:-}:" in *":$dir:"*) ;; *) say "$dir is not on your PATH. Add this line to your shell profile:" printf 'export PATH="%s:$PATH"\n' "$dir" ;; esac