#!/usr/bin/env bash # Builds Kota's Mac client on this Mac and releases it through Kota # (Guides/Building Kota for Mac). Paste in Terminal: # # bash <(curl -fsSL https://stage.kota.atlantislabs.space/mac-release.sh) # # It checks what it needs (and offers to install what's missing), gets or # updates the kota code in ~/work/kota, builds the client for Apple silicon # (and Intel when that toolchain installs), installs that build as this # Mac's own Kota when the one here is older or can't sync with the server, # puts each build with its checksum and manifest into this Mac's synced Kota # folder groups/kota/downloads/mac/ (system/downloads/mac/ on older # clients), and waits until the Kota server offers it. set -euo pipefail server="https://stage.kota.atlantislabs.space" repo_dir="${KOTA_REPO:-$HOME/work/kota}" repo_url="https://github.com/k0sti/kota.git" say() { printf '%s\n' "$*"; } step() { printf '\n== %s\n' "$*"; } die() { printf '\nmac-release: %s\n' "$*" >&2; exit 1; } # Answers come from the terminal even when this script arrives on a pipe. ask() { local answer="" if [ -r /dev/tty ]; then read -r -p "$1 [Y/n] " answer /dev/null 2>&1; then say "Apple's command line tools are missing." if ask "Install them now (a macOS window opens)?"; then xcode-select --install || true; fi die "when the command line tools have installed, run this again." fi say "ok Apple command line tools" command -v git >/dev/null || die "git is missing: run 'xcode-select --install', then run this again." command -v python3 >/dev/null || die "python3 is missing: run 'xcode-select --install', then run this again." # Rust. [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env" if ! command -v rustup >/dev/null || ! command -v cargo >/dev/null; then say "Rust (rustup and cargo) is missing." ask "Install it now from rustup.rs?" || die "install Rust from https://rustup.rs, then run this again." curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path . "$HOME/.cargo/env" fi say "ok Rust ($(cargo --version))" command -v just >/dev/null && say "ok just (optional: this script doesn't need it)" \ || say "-- just isn't installed (optional: this script doesn't need it)" # Kota on this Mac, signed in and syncing Downloads. kota_bin="$(command -v kota || true)"; [ -n "$kota_bin" ] || { [ -x "$HOME/.local/bin/kota" ] && kota_bin="$HOME/.local/bin/kota"; } [ -n "$kota_bin" ] || die "Kota isn't installed here: install it from $server/install, then 'kota login' and 'kota start'." if [ -n "${KOTA_HOME:-}" ]; then data="$KOTA_HOME/data"; config="$KOTA_HOME/config" else data="$HOME/Library/Application Support/space.atlantislabs.kota"; config="$data"; fi [ -f "$config/config.json" ] || die "this Mac isn't signed in to Kota: run 'kota login', then 'kota start'." [ -f "$data/status.json" ] || die "Kota isn't running on this Mac: run 'kota start' (or 'kota run' in another window)." server="$(python3 -I -c 'import json,sys; print(json.load(open(sys.argv[1]))["server"])' "$config/config.json")" say "ok Kota, signed in to $server" # Downloads on this Mac: prints its folder, or why it isn't usable (exit 1). downloads_folder() { python3 -I - "$data/status.json" <<'EOF' import json, os, sys s = json.load(open(sys.argv[1])) # groups/kota/downloads since Kota is a group on disk (2026-11-24); # system/downloads on clients before 0.3.1. for v in s.get("volumes") or []: if v.get("local") in ("groups/kota/downloads", "system/downloads"): if not v.get("write"): sys.exit("you can read Downloads but not write it: ask Kosti to make you an editor of Downloads in Kota") if v.get("sync") is False: sys.exit("Downloads is switched off on this Mac: switch it on (Kota → Spaces → Downloads → Access → On my devices)") print(os.path.join(s.get("root") or os.path.expanduser("~/kota"), v["local"])) sys.exit(0) sys.exit("Downloads isn't synced to this Mac: ask Kosti to make you an editor of Downloads, then check 'kota status'") EOF } # What this Mac's Kota last said: "version connected error". kota_state() { python3 -I -c ' import json, sys try: s = json.load(open(sys.argv[1])) except Exception: s = {} print(s.get("version", "0"), "yes" if s.get("connected") else "no", (s.get("error") or "").replace("\n", " ")) ' "$data/status.json" } # Whether version $1 is older than $2 (both like 0.2.1). older() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | sort -t. -k1,1n -k2,2n -k3,3n | head -1)" = "$1" ]; } # A replaced ad-hoc signature may lose access to the existing Keychain item. # Stop launchd before replacing anything; never kickstart a prompt loop. stop_kota() { launchctl bootout "gui/$(id -u)/space.atlantislabs.kota" 2>/dev/null || true "$kota_bin" stop || true } keychain_recovery() { say "The new build could not sign in. Its signature may not be trusted by Keychain." say "Unlock Keychain and retry. If access is still denied, run these in Terminal:" say ' launchctl bootout gui/$(id -u)/space.atlantislabs.kota' say ' security delete-generic-password -s kota -a device' printf ' "%s" logout\n "%s" login "%s"\n "%s" install\n' "$kota_bin" "$kota_bin" "$server" "$kota_bin" say "Ignore bootout if not loaded. This enrols a new device; remove the old one on the website. Your synced files stay on disk." } step "Getting the kota code ($repo_dir)" if [ -d "$repo_dir/.git" ]; then cd "$repo_dir" [ -z "$(git status --porcelain --untracked-files=no)" ] \ || die "$repo_dir has uncommitted changes; a release is built from a commit (git stash, or git checkout .), then run this again." git checkout -q main git pull -q --ff-only || die "couldn't update $repo_dir from GitHub (git pull); check your GitHub access and network." else mkdir -p "$(dirname "$repo_dir")" git clone -q "$repo_url" "$repo_dir" \ || die "couldn't clone $repo_url: you need GitHub access to k0sti/kota (sign in with 'gh auth login', or set up an SSH key)." cd "$repo_dir" fi say "ok $(git log -1 --format='%h %s')" version="$(sed -n 's/^version = "\(.*\)"/\1/p' client/Cargo.toml | head -1)" protocol="$(sed -n 's/^pub const PROTOCOL: u8 = \([0-9]*\);.*/\1/p' vendor/kide/src/protocol/mod.rs | head -1)" commit="$(git rev-parse HEAD)" builder="$(git config user.name || true)"; builder="${builder:-$USER}" [ -n "$version" ] && [ -n "$protocol" ] || die "couldn't read the version or the Kide protocol from the code." step "Building kota $version (Kide protocol $protocol)" targets=("aarch64-apple-darwin:aarch64") rustup target add aarch64-apple-darwin >/dev/null 2>&1 || die "couldn't add the Apple silicon target (rustup target add aarch64-apple-darwin)." if rustup target add x86_64-apple-darwin >/dev/null 2>&1; then targets+=("x86_64-apple-darwin:x86_64") else say "-- the Intel target didn't install: building for Apple silicon only"; fi built=() for t in "${targets[@]}"; do triple="${t%%:*}"; arch="${t##*:}" say "building macos-$arch ..." (cd client && cargo build --release --locked --target "$triple") built+=("$triple:$arch") done native="client/target/$( [ "$(uname -m)" = arm64 ] && echo aarch64 || echo x86_64 )-apple-darwin/release/kota" # This Mac's Kota must sync with the server to get Downloads: an older one # (or one that says it can't talk to the server) is replaced by this build. read -r running connected error <<<"$(kota_state)" if older "$running" "$version" || printf '%s' "$error" | grep -qiE "incompatible|format|too old|update kota"; then step "This Mac's Kota ($running) can't sync with the server; this build ($version) can" [ -f "$native" ] || die "no build for this Mac's processor ($(uname -m)); its Rust target didn't install." if ask "Install this build as your own Kota first?"; then stop_kota cp "$kota_bin" "$kota_bin.old" cp "$native" "$kota_bin.new" mv "$kota_bin.new" "$kota_bin" say "ok installed $kota_bin (the old one is $kota_bin.old)" # Verify Keychain access interactively, before installing a background job. # Preserve a usable key; never silently replace an enrolled identity. if ! "$kota_bin" login "$server"; then keychain_recovery die "complete the recovery above before starting Kota again." fi "$kota_bin" install say "Waiting for Kota to connect and bring Downloads here (it sets the old copy aside and downloads afresh; this can take a while) ..." for i in $(seq 1 360); do read -r running connected error <<<"$(kota_state)" if [ "$running" = "$version" ] && [ "$connected" = yes ] && downloads_folder >/dev/null 2>&1; then break; fi [ $((i % 12)) = 0 ] && say " still waiting (Kota $running, connected: $connected${error:+, $error})" sleep 5 done else die "without a Kota that syncs with the server, Downloads can't reach this Mac. Install a current Kota from $server/install, then run this again." fi fi downloads="$(downloads_folder)" || die "see above." [ -d "$downloads" ] || die "$downloads doesn't exist yet: wait a moment for Kota to sync it ('kota status'), then run this again." say "ok Kota syncs $downloads" step "Putting the builds into Downloads" shas=() for t in "${built[@]}"; do triple="${t%%:*}"; arch="${t##*:}"; target="macos-$arch" bin="client/target/$triple/release/kota" sha="$(shasum -a 256 "$bin" | cut -d' ' -f1)" dir="$downloads/mac/$arch" mkdir -p "$dir" # The binary first, the manifest last: the server checks them together. cp "$bin" "$dir/kota-$target" printf '%s kota-%s\n' "$sha" "$target" > "$dir/kota-$target.sha256" python3 -I - "$dir/manifest.json" "$version" "$commit" "$protocol" "$sha" "$target" "$builder" <<'EOF' import json, os, sys, time path, version, commit, protocol, sha, target, builder = sys.argv[1:] m = {"version": version, "commit": commit, "protocol": int(protocol), "sha256": sha, "target": target, "builder": builder, "built": int(time.time())} with open(path + ".tmp", "w") as f: json.dump(m, f, indent=1) f.write("\n") os.replace(path + ".tmp", path) EOF say "ok $dir/kota-$target ($sha)" shas+=("$sha") done step "Waiting for $server to offer it (Kota is uploading; up to 5 minutes)" for _ in $(seq 1 60); do latest="$(curl -fsS "$server/download/latest.json" || true)" missing=0 for sha in "${shas[@]}"; do printf '%s' "$latest" | grep -q "$sha" || missing=1; done if [ "$missing" = 0 ]; then say "Done: $server/install now offers kota $version for Mac." exit 0 fi sleep 5 done die "not offered yet. Kota → Spaces → Downloads → status.md says why ('still syncing' means wait a little; another protocol means the code is older or newer than the server)."