#!/bin/sh # Clara installer. # # curl -fsSL https://registry.clara.kalenmichael.com/v1/install | sh # curl -fsSL https://registry.clara.kalenmichael.com/v1/install | sh -s -- --no-desktop # # Reads the signed installer projection from core/install.json, verifies the # Ed25519 signature, downloads the matching core archive, and installs the # `clara` command under ~/.local/share/clara/versions. set -eu BASE_URL="${CLARA_RELEASE_BASE_URL:-${CLARA_REGISTRY_BASE_URL:-https://registry.clara.kalenmichael.com/v1}}" BASE_URL_EXPLICIT=0 if [ -n "${CLARA_RELEASE_BASE_URL:-}" ] || [ -n "${CLARA_REGISTRY_BASE_URL:-}" ]; then BASE_URL_EXPLICIT=1; fi BASE_URL="${BASE_URL%/}" REQUESTED_VERSION="" BIN_DIR="${CLARA_BIN_DIR:-$HOME/.local/bin}" PUBLIC_KEY='MCowBQYDK2VwAyEAGVaTZBnu1fcTSFP8XZWoIkovYL0T7Ymykd5rjdcLDek=' DESKTOP_MODE="${CLARA_INSTALL_DESKTOP:-auto}" if [ "${CLARA_NO_DESKTOP:-}" = "1" ]; then DESKTOP_MODE='no'; fi say() { printf '%s\n' "$*"; } fail() { printf 'error: %s\n' "$*" >&2 exit 1 } while [ $# -gt 0 ]; do case "$1" in --version) REQUESTED_VERSION="${2:-}"; shift 2 ;; --base-url) BASE_URL="${2:-}"; BASE_URL_EXPLICIT=1; BASE_URL="${BASE_URL%/}"; shift 2 ;; --bin-dir) BIN_DIR="${2:-}"; shift 2 ;; --desktop) DESKTOP_MODE='yes'; shift ;; --no-desktop) DESKTOP_MODE='no'; shift ;; -h|--help) say "Usage: install.sh [--version ] [--base-url ] [--bin-dir ] [--desktop|--no-desktop]" exit 0 ;; *) fail "unknown option: $1" ;; esac done command -v curl >/dev/null 2>&1 || fail "Clara needs curl to download the release." command -v tar >/dev/null 2>&1 || fail "Clara needs tar to extract the release." command -v awk >/dev/null 2>&1 || fail "Clara needs awk to read the installer projection." proto='=https' case "$BASE_URL" in http://127.0.0.1:* | http://localhost:* | http://\[::1\]:*) proto='=https,http' ;; esac download() { curl -fsSL --proto "$proto" --tlsv1.2 --connect-timeout 10 --max-time "${3:-60}" "$1" -o "$2" } os=$(uname -s 2>/dev/null || true) arch=$(uname -m 2>/dev/null || true) case "$os" in Linux) case "$arch" in x86_64 | amd64) platform='linux-x64' ;; aarch64 | arm64) platform='linux-arm64' ;; *) fail "Clara has no Linux build for $arch." ;; esac if [ -f /etc/alpine-release ] || (ldd --version 2>&1 | grep -qi musl); then platform="${platform}-musl" fi ;; Darwin) case "$arch" in x86_64 | amd64) platform='darwin-x64' ;; aarch64 | arm64) platform='darwin-arm64' ;; *) fail "Clara has no macOS build for $arch." ;; esac ;; *) fail "Clara has no shell installer for $os. Download the release archive for your platform." ;; esac if [ "$DESKTOP_MODE" = 'auto' ]; then case "$os" in Darwin) DESKTOP_MODE='yes' ;; Linux) if [ -n "${DISPLAY:-}${WAYLAND_DISPLAY:-}" ]; then DESKTOP_MODE='yes'; else DESKTOP_MODE='no'; fi ;; *) DESKTOP_MODE='no' ;; esac fi case "$DESKTOP_MODE" in yes | always | 1) DESKTOP_MODE='yes' ;; no | never | 0) DESKTOP_MODE='no' ;; *) fail "CLARA_INSTALL_DESKTOP must be auto, yes, or no." ;; esac tmp=$(mktemp -d 2>/dev/null || echo "${TMPDIR:-/tmp}/clara-install-$$") trap 'rm -rf "$tmp"' EXIT HUP INT TERM say "Clara installer for $platform" download "$BASE_URL/core/install.json" "$tmp/install.json" 30 || fail "Could not read $BASE_URL/core/install.json." download "$BASE_URL/core/install.json.sig" "$tmp/install.json.sig" 30 || fail "Could not read the installer signature." verify_signature() { # The compiled key is base64; the template placeholder contains underscores. # Do not compare against the placeholder literal: rendering replaces it too. case "$PUBLIC_KEY" in '' | *_*) if [ "${CLARA_REQUIRE_SIGNATURE:-}" = "1" ]; then fail "This installer was not built with a release public key." fi say "warning: this installer was not built with a release public key; verifying the checksum only." return 0 ;; esac verifier='' for candidate in /opt/homebrew/bin/openssl /usr/local/bin/openssl openssl; do command -v "$candidate" >/dev/null 2>&1 || continue case "$("$candidate" version 2>/dev/null || true)" in *LibreSSL*) continue ;; esac if "$candidate" pkeyutl -help 2>&1 | grep -q 'rawin'; then verifier="$candidate" break fi done if [ -z "$verifier" ]; then if [ "${CLARA_REQUIRE_SIGNATURE:-}" = "1" ]; then fail "Clara needs an Ed25519-capable openssl to verify this release." fi say "warning: no Ed25519-capable openssl found; verifying the checksum only." return 0 fi { printf '%s\n' '-----BEGIN PUBLIC KEY-----' printf '%s\n' "$PUBLIC_KEY" | fold -w 64 printf '%s\n' '-----END PUBLIC KEY-----' } > "$tmp/public.pem" if ! base64 -d "$tmp/install.json.sig" > "$tmp/install.sig" 2>/dev/null; then if ! base64 -D "$tmp/install.json.sig" > "$tmp/install.sig" 2>/dev/null; then "$verifier" base64 -d -A -in "$tmp/install.json.sig" -out "$tmp/install.sig" || fail "Could not decode the installer signature." fi fi "$verifier" pkeyutl -verify -pubin -inkey "$tmp/public.pem" -rawin \ -in "$tmp/install.json" -sigfile "$tmp/install.sig" >/dev/null 2>&1 || fail "The installer signature did not match." say "Verified the installer signature." } verify_signature version=$(sed -n 's/^[[:space:]]*"version":[[:space:]]*"\([^"]*\)".*/\1/p' "$tmp/install.json" | head -n 1) [ -n "$version" ] || fail "The installer projection has no version." if [ -n "$REQUESTED_VERSION" ] && [ "$REQUESTED_VERSION" != "$version" ]; then fail "The official registry only publishes Clara $version, not $REQUESTED_VERSION." fi block=$(awk -v key="\"$platform\":" ' $0 ~ ("^[[:space:]]*" key "[[:space:]]*\\{") { inside = 1 } inside { print } inside && /^[[:space:]]*}[,]?[[:space:]]*$/ { exit } ' "$tmp/install.json") url=$(printf '%s\n' "$block" | sed -n 's/.*"url"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') sha=$(printf '%s\n' "$block" | sed -n 's/.*"sha256"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p') [ -n "$url" ] || fail "The installer has no build for $platform." [ -n "$sha" ] || fail "The installer has no checksum for $platform." say "Downloading Clara $version" download "$url" "$tmp/clara.tar.gz" 300 || fail "Could not download $url." if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$tmp/clara.tar.gz" | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then actual=$(shasum -a 256 "$tmp/clara.tar.gz" | awk '{print $1}') else fail "Clara needs sha256sum or shasum to verify the download." fi [ "$actual" = "$sha" ] || fail "The download checksum did not match." mkdir -p "$tmp/unpack" tar -xzf "$tmp/clara.tar.gz" -C "$tmp/unpack" chmod 755 "$tmp/unpack/clara" version_output=$("$tmp/unpack/clara" --version 2>/dev/null) || fail "The downloaded Clara binary could not run on this machine." VERSION=$(printf '%s\n' "$version_output" | sed -n 's/^\([0-9][0-9A-Za-z.+-]*\).*/\1/p' | head -n 1) [ -n "$VERSION" ] || VERSION="$version" if [ -n "$REQUESTED_VERSION" ] && [ "$REQUESTED_VERSION" != "$VERSION" ]; then fail "The release contains Clara $VERSION, not $REQUESTED_VERSION." fi root="${CLARA_DATA_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/clara}/versions/$VERSION" mkdir -p "$root" cp "$tmp/unpack/clara" "$root/clara" chmod 755 "$root/clara" mkdir -p "$BIN_DIR" ln -sf "$root/clara" "$BIN_DIR/clara" say "Installed Clara $VERSION to $root" say "Linked $BIN_DIR/clara" if [ "$DESKTOP_MODE" = 'yes' ]; then say "Installing the Clara desktop app" if "$BIN_DIR/clara" desktop install --optional; then : else say "warning: the desktop app was not installed. Run \`clara desktop install\` later." fi fi case ":$PATH:" in *":$BIN_DIR:"*) say "Run: clara setup" ;; *) say "Add $BIN_DIR to PATH, then run: clara setup" ;; esac