#!/bin/sh # Copyright (c) 2026 Unidatum Integrated Products LLC and Unidatum Integrated Products Limited, Ireland. # Licensed under the Unidatum Speech Free Use Licence v1.0: see the LICENSE file. # Unidatum Speech installer: speech.unidatum.com/install.sh # # curl -fsSL https://speech.unidatum.com/install.sh | sh -s -- --code XXXX-XXXX # # An AI coding agent installing on a person's behalf (Claude Code and others) # uses the supported automation route instead of a code from the download page: # # curl -fsSL https://speech.unidatum.com/install.sh | sh -s -- --agent # # See "Agent mode" below and https://speech.unidatum.com/agents/ # # What this script does, in order, and nothing else: # # 0. (--agent only, instead of --code) asks the site for a proof-of-work # challenge, solves it on this machine (about a second of hashing, with # perl, python3 or node) and exchanges the answer for a single-use install # code. That is all the human check an agent can give, and the site counts # it as an agent ("agent" client label, no more than 40 an hour for the # site and 10 an hour per country). See "Agent mode". # 1. Looks at the machine: Linux x86_64, Linux arm64 or macOS on Apple silicon, and the # tools it needs (curl, tar, a sha256 program; bzip2 and uv on Linux). # 2. Asks speech.unidatum.com which release is current and, with your # install code from the download page, fetches the matching package. # The code is single-use and lasts fifteen minutes; it only unlocks the # package download. Nothing about you is sent. # 3. Verifies the package's SHA-256 against the checksum file published # beside it AND against the release's SHA256SUMS before anything is # unpacked. A mismatch stops the install and deletes the download. # (There is no signature yet: the checksums come from the same site as # the package, so they prove integrity in transit, not authorship.) # 4. Unpacks the package under your home folder (no sudo) and runs the # package's own installer, which makes a Python environment, downloads # the speech models (about 1 GB, each checked against a recorded hash) # and writes a config for this machine's cores. The package's LICENSE # and THIRD-PARTY-NOTICES.md are copied next to the install. # 5. Generates an API key into a file only you can read, and prints the # file's path, never the key. # 6. Installs a systemd user unit (Linux) or a launchd agent (macOS) and # starts it, unless you say --no-start. # # Options: # --code CODE the install code from https://speech.unidatum.com/download # (or UNIDATUM_SPEECH_INSTALL_CODE in the environment) # --agent no code: do the supported agent route (see "Agent mode"); # also UNIDATUM_SPEECH_AGENT=1. Never asks a question, prints # no progress bars, waits for /health at the end. # --prefix DIR where to install (default: ~/.local/share/unidatum-speech # on Linux, ~/unidatum-speech on macOS); the config and # key file go to DIR/config when --prefix is given # --port N the port to listen on (default 18790), loopback only # --cpus N how many cpus the service may use (default: half the # machine, at most 8; a dedicated server can take more, # up to three quarters, e.g. --cpus 24 on a 32 vCPU host) # --release vX.Y.Z a release other than the current one # --no-start install everything but do not enable or start the service # --dry-run show what would be done; download and write nothing # --uninstall stop the service and remove what this script installed # (the config folder with your key is kept unless --purge) # --purge with --uninstall: remove the config folder and key too # --yes do not ask before removing (uninstall) # --version print this installer's version and the current release # -h, --help this text # # Agent mode (--agent): legitimate automation gets a low-friction route, by # Unidatum's decision. The script does what the download page's "Continue # anyway" button does for a person whose browser cannot run the check: a # proof of work instead of a Turnstile click, so it proves effort, not a # person, and says so (it identifies itself as unidatum-speech-agent/N, which # the site counts under the client label "agent"). Everything else is the same: # the code is single use, the checksum is verified, nothing needs sudo. When the # hourly ceiling is reached it exits 75 (EX_TEMPFAIL) and prints Retry-After # and the honest alternative: ask the person to open the download page, pass the # check, and give you the command with its code. Exit codes: 0 done, 1 failed, # 2 bad usage, 75 try again later. # # It never uses sudo, never touches anything outside the prefix, the config # folder, the user unit folder (~/.config/systemd/user) or ~/Library/LaunchAgents, # and never deletes anything except on --uninstall, which asks first. # Re-running it updates the install in place; the config and key are kept. # # People read these scripts: every step below says what it does and why. # shellcheck shell=sh set -eu INSTALLER_VERSION="1.2 (2026-12-05)" SITE="${UNIDATUM_SPEECH_SITE:-https://speech.unidatum.com}" # overridable for testing against a local copy PRODUCT="unidatum-speech" # ---------------------------------------------------------------- arguments code="${UNIDATUM_SPEECH_INSTALL_CODE:-}" agent=0; [ -n "${UNIDATUM_SPEECH_AGENT:-}" ] && agent=1 prefix="" port="18790" cpus_wanted="" release="" start=1 dry=0 uninstall=0 purge=0 yes=0 usage() { sed -n '2,49p' "$0" 2>/dev/null | sed 's/^# \{0,1\}//'; } while [ $# -gt 0 ]; do case "$1" in --code) code="$2"; shift ;; --code=*) code="${1#--code=}" ;; --agent) agent=1 ;; --prefix) prefix="$2"; shift ;; --prefix=*) prefix="${1#--prefix=}" ;; --port) port="$2"; shift ;; --port=*) port="${1#--port=}" ;; --cpus) cpus_wanted="$2"; shift ;; --cpus=*) cpus_wanted="${1#--cpus=}" ;; --release) release="$2"; shift ;; --release=*) release="${1#--release=}" ;; --no-start) start=0 ;; --dry-run) dry=1 ;; --uninstall) uninstall=1 ;; --purge) purge=1 ;; --yes|-y) yes=1 ;; --version|-V) echo "unidatum-speech installer $INSTALLER_VERSION" cur=$(curl -fsS "$SITE/dl/latest.json" 2>/dev/null | sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' | head -n 1 || true) [ -n "$cur" ] && echo "current release at $SITE: $cur" exit 0 ;; -h|--help) # When piped through sh there is no file to read the header from; print a short form. if [ -f "$0" ]; then usage; else echo "usage: install.sh [--code CODE | --agent] [--prefix DIR] [--port N] [--cpus N] [--release vX.Y.Z] [--no-start] [--dry-run] [--uninstall [--purge] [--yes]] [--version]" echo "full text: curl -fsSL $SITE/install.sh | less" fi exit 0 ;; *) echo "install.sh: unknown option $1 (try --help)" >&2; exit 2 ;; esac shift done case "$port" in ''|*[!0-9]*) echo "install.sh: --port needs a number" >&2; exit 2 ;; esac case "$cpus_wanted" in *[!0-9]*|0) echo "install.sh: --cpus needs a number" >&2; exit 2 ;; esac # ------------------------------------------------------------------ helpers say() { printf '%s\n' "$*"; } step() { printf '\n==> %s\n' "$*"; } die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; } have() { command -v "$1" >/dev/null 2>&1; } # In agent mode every request says who is asking, so the site can count the # agent route (a label for the figures, not a credential). The version is the # installer's major.minor. if [ "$agent" = 1 ]; then curl() { command curl -A "unidatum-speech-agent/${INSTALLER_VERSION%% *}" "$@"; } fi # run prints the command in a dry run and runs it otherwise. run() { if [ "$dry" = 1 ]; then printf ' (dry run) %s\n' "$*"; else "$@"; fi; } sha256_of() { if have sha256sum; then sha256sum "$1" | cut -d' ' -f1 elif have shasum; then shasum -a 256 "$1" | cut -d' ' -f1 else die "need sha256sum or shasum to verify the download"; fi } # ------------------------------------------------------------- agent mode # solve_pow CHALLENGE BITS prints a nonce n such that SHA-256 of "
: is the challenge without its signature (the
# part after the last dot). 2^19 hashes on average: about a second with perl,
# python3 or node, whichever is here. A shell loop that starts one process per
# hash would take hours, so there is no such fallback: without one of the three
# the agent route cannot work and the script says what to do instead.
solve_pow() {
sp_p=${1%.*}
if have perl && perl -MDigest::SHA -e1 >/dev/null 2>&1; then
perl -MDigest::SHA=sha256 -e '
my ($p, $bits) = @ARGV; my ($full, $rem) = (int($bits / 8), $bits % 8); my $zero = "\0" x $full;
for (my $n = 0;; $n++) {
my $h = sha256("$p:$n");
next unless substr($h, 0, $full) eq $zero;
next if $rem && (ord(substr($h, $full, 1)) >> (8 - $rem));
print "$n\n"; exit 0;
}' "$sp_p" "$2"
elif have python3; then
python3 -I -c '
import hashlib, sys
p, bits = sys.argv[1], int(sys.argv[2]); full, rem = divmod(bits, 8); zero = bytes(full); n = 0
while True:
h = hashlib.sha256(("%s:%d" % (p, n)).encode()).digest()
if h[:full] == zero and (not rem or h[full] >> (8 - rem) == 0): print(n); break
n += 1' "$sp_p" "$2"
elif have node; then
node -e '
const c = require("crypto"); const [p, bits] = [process.argv[1], Number(process.argv[2])];
const full = Math.floor(bits / 8), rem = bits % 8;
for (let n = 0;; n++) { const h = c.createHash("sha256").update(p + ":" + n).digest();
let ok = true; for (let i = 0; i < full; i++) if (h[i] !== 0) { ok = false; break; }
if (ok && (!rem || h[full] >> (8 - rem) === 0)) { console.log(n); break; } }' "$sp_p" "$2"
else
return 1
fi
}
# The honest alternative, printed whenever the agent route cannot be used.
agent_alternative() {
say " What to do instead: ask the person to open $SITE/download in a browser, pass the"
say " check there, and paste you the install command shown with its code (it looks like"
say " curl -fsSL $SITE/install.sh | sh -s -- --code XXXX-XXXX). Run that command as given."
}
# agent_code sets $code: a challenge, the work, and a single-use install code.
# It exits 75 when the site's hourly ceiling says wait.
agent_code() {
ac=$(curl -sS -X POST -H 'content-type: application/json' -d '{}' -w '\n%{http_code}' "$SITE/dl/agent/challenge") || die "cannot reach $SITE"
ac_http=$(printf '%s\n' "$ac" | tail -n 1); ac_body=$(printf '%s\n' "$ac" | sed '$d')
case "$ac_http" in
200) ;;
503) say " the site has no human check configured right now; the download is plain"; code=""; return 0 ;;
429)
ac_wait=$(printf '%s' "$ac_body" | sed -n 's/.*"retry_after": *\([0-9]*\).*/\1/p')
ac_scope=$(printf '%s' "$ac_body" | sed -n 's/.*"scope": *"\([a-z]*\)".*/\1/p')
say " The agent route is at its hourly ceiling (scope: ${ac_scope:-unknown}). Nothing was installed."
say " Retry-After: ${ac_wait:-60} seconds (the ceilings reset on the clock hour)."
agent_alternative
exit 75 ;;
*) die "the agent challenge answered $ac_http: $ac_body" ;;
esac
ac_c=$(printf '%s' "$ac_body" | sed -n 's/.*"c": *"\([^"]*\)".*/\1/p')
ac_bits=$(printf '%s' "$ac_body" | sed -n 's/.*"bits": *\([0-9]*\).*/\1/p')
if [ -z "$ac_c" ] || [ -z "$ac_bits" ]; then die "the agent challenge was not in the expected shape: $ac_body"; fi
say " challenge: $ac_bits bits of proof of work (2^$ac_bits hashes on average)"
ac_n=$(solve_pow "$ac_c" "$ac_bits") || {
say " no perl (with Digest::SHA), python3 or node here to do the proof of work."
say " Install one of them (a user-level python3 or node is enough) and run again."
agent_alternative
exit 1; }
ac=$(curl -sS -X POST -H 'content-type: application/json' -d "{\"c\":\"$ac_c\",\"n\":\"$ac_n\"}" -w '\n%{http_code}' "$SITE/dl/agent/ticket") || die "cannot reach $SITE"
ac_http=$(printf '%s\n' "$ac" | tail -n 1); ac_body=$(printf '%s\n' "$ac" | sed '$d')
case "$ac_http" in
200) ;;
429)
ac_wait=$(printf '%s' "$ac_body" | sed -n 's/.*"retry_after": *\([0-9]*\).*/\1/p')
say " The agent route reached its hourly ceiling while the work was being done. Nothing was installed."
say " Retry-After: ${ac_wait:-60} seconds (the ceilings reset on the clock hour)."
agent_alternative
exit 75 ;;
*) die "the agent ticket answered $ac_http: $ac_body" ;;
esac
code=$(printf '%s' "$ac_body" | sed -n 's/.*"code": *"\([^"]*\)".*/\1/p')
[ -n "$code" ] || die "the agent ticket had no code in it: $ac_body"
say " solved; got a single-use install code (redeemed next, never printed)"
}
# ------------------------------------------------------- the profile's sums
# Kept as functions of numbers only, so that the tests (site/install/test-plan.sh)
# can check them without a machine to match.
#
# split_cpus REST: how many of the REST cpus (the service's, less the text to
# speech one) go to the streaming (fast) stage and how many to the final stage.
# Measured on a c7a and a Graviton4 (docs/COSTS.md sections 1a and 1b): the
# fast stage is the limit, about 0.6 of a core per live stream against about
# 0.14 for the final decode, so it gets about three times the cores. A final
# worker takes two threads, so the final stage is a whole number of those, at
# least one. Sets plan_fast and plan_final.
split_cpus() {
plan_workers=$(( ($1 + 4) / 8 ))
[ "$plan_workers" -lt 1 ] && plan_workers=1
plan_final=$((plan_workers * 2))
plan_fast=$(($1 - plan_final))
[ "$plan_fast" -lt 1 ] && { plan_fast=1; plan_final=$(($1 - 1)); plan_workers=$((plan_final / 2)); plan_final=$((plan_workers * 2)); }
return 0
}
# stream_cap CPUS KIND: max_sessions for CPUS cpus of fast and final stage, from
# what was measured, at 85 percent of the highest clean count (the knee):
# graviton4 28 English streams on 24 vCPUs -> 24 on 24
# epyc 20 English streams on 24 vCPUs -> 17 on 24 (AMD EPYC 9R14, c7a)
# Anything else was not measured: prints nothing, and the service keeps its
# own default. The number is the cap above which new sessions get 503.
stream_cap() {
case "$2" in
graviton4) num=28; den=24 ;;
epyc) num=20; den=24 ;;
*) return 0 ;;
esac
echo $(( ($1 * num * 85 + den * 50) / (den * 100) ))
}
# need_mb FAST FINAL_WORKERS: resident memory the workers need, in MB. It
# scales with the processes, not the streams: about 0.8 GB per fast worker and
# 1.8 GB per final worker (two threads), plus 1 GB for the service and a text to
# speech voice. Measured: 20 GB for 18 fast + 3 final on Graviton4, 15 GB on a c7a.
need_mb() { echo $(( $1 * 800 + $2 * 1800 + 1000 )); }
# Tests source this file with the variable below set and stop here.
# shellcheck disable=SC2317
if [ -n "${UNIDATUM_SPEECH_INSTALL_FUNCTIONS_ONLY:-}" ]; then return 0 2>/dev/null || exit 0; fi
# ------------------------------------------------------------- the machine
os=$(uname -s); arch=$(uname -m)
case "$os-$arch" in
Linux-x86_64) plat="linux-amd64"; osname="Linux x86_64" ;;
Linux-aarch64|Linux-arm64) plat="linux-arm64"; osname="Linux arm64 (Graviton and other aarch64)" ;;
Darwin-arm64) plat="macos-arm64"; osname="macOS, Apple silicon" ;;
Darwin-x86_64) die "there is no macOS Intel package; the service needs Apple silicon on a Mac" ;;
*) die "unsupported platform $os $arch (packages: Linux x86_64, Linux arm64, macOS Apple silicon)" ;;
esac
case "$plat" in linux-*) family=linux ;; *) family=macos ;; esac
if [ -z "$prefix" ]; then
if [ "$family" = linux ]; then prefix="$HOME/.local/share/unidatum-speech"; config_dir="$HOME/.config/unidatum-speech"
else prefix="$HOME/unidatum-speech"; config_dir="$prefix/config"; fi
else
config_dir="$prefix/config"
fi
unit_dir="$HOME/.config/systemd/user"
label="com.unidatum.speech"
# ---------------------------------------------------------------- uninstall
if [ "$uninstall" = 1 ]; then
step "Uninstall from $prefix"
if [ "$family" = linux ]; then
say " would stop and disable the systemd user unit unidatum-speech (and a service started by hand from $prefix), remove $unit_dir/unidatum-speech.service, and remove $prefix"
else
say " would unload the launchd agent $label, remove ~/Library/LaunchAgents/$label.plist, and remove $prefix"
fi
if [ "$purge" = 1 ]; then say " and remove the config folder $config_dir, including the key file"
else say " the config folder $config_dir (with your key) is kept; add --purge to remove it"; fi
[ -d "$prefix" ] || say " ($prefix does not exist; nothing installed there)"
if [ "$dry" = 1 ]; then say " dry run: nothing removed"; exit 0; fi
if [ "$yes" != 1 ]; then
if [ -t 0 ]; then
printf ' Continue? [y/N] '; read -r answer
case "$answer" in y|Y|yes) ;; *) say " nothing removed"; exit 0 ;; esac
else
die "refusing to remove without confirmation; run with --yes (and read the plan above first)"
fi
fi
if [ "$family" = linux ]; then
if have systemctl; then systemctl --user disable --now unidatum-speech >/dev/null 2>&1 || true; fi
rm -f "$unit_dir/unidatum-speech.service" # the unit file goes whether or not systemd is running here
if have systemctl; then systemctl --user daemon-reload >/dev/null 2>&1 || true; fi
else
launchctl bootout "gui/$(id -u)/$label" >/dev/null 2>&1 || true
rm -f "$HOME/Library/LaunchAgents/$label.plist"
fi
# A service started by hand (no systemd user session, so the installer only printed the command) is not
# the unit's: stop the one running from this prefix, if any, before its files go.
if [ "$family" = linux ] && have pkill && pgrep -u "$(id -u)" -f "$prefix/bin/unidatum-speech" >/dev/null 2>&1; then
pkill -u "$(id -u)" -f "$prefix/bin/unidatum-speech" || true
say " stopped the service that was running from $prefix (started by hand, not by systemd)"
fi
rm -rf "$prefix"
[ "$purge" = 1 ] && rm -rf "$config_dir"
say " removed. Thank you for trying Unidatum Speech."
exit 0
fi
# ------------------------------------------------------------ prerequisites
step "Unidatum Speech installer $INSTALLER_VERSION on $osname"
missing=""
for t in curl tar; do have "$t" || missing="$missing $t"; done
have sha256sum || have shasum || missing="$missing sha256sum"
# The package's installer unpacks the speech models, which are .tar.bz2 (macOS has bzip2). A bare
# Linux box may lack the bzip2 program but have python3, whose bz2 module does the same job, so
# in that case a decompress-only stand-in is used for this install (see "the package's own
# installer" below); nothing is installed on the system.
bz2_shim=0
if [ "$family" = linux ] && ! have bzip2; then
if have python3 && python3 -I -c 'import bz2' >/dev/null 2>&1; then bz2_shim=1; else missing="$missing bzip2"; fi
fi
[ -z "$missing" ] || die "missing:$missing (install them with your package manager and run again; they are system packages and this installer never installs system packages. An agent should ask the person to install them)"
if [ "$family" = linux ]; then
if have uv; then uvbin=$(command -v uv); elif [ -x "$HOME/.local/bin/uv" ]; then uvbin="$HOME/.local/bin/uv"; else uvbin=""; fi
if [ -z "$uvbin" ]; then
say " uv (the Python package manager) is needed to make the workers' Python environment and is not installed."
say " Install it first, then run this again: https://docs.astral.sh/uv/getting-started/installation/"
say " (this installer does not run other people's install scripts for you)"
[ "$agent" = 1 ] && say " An agent may install uv for the person, in their home folder, no root needed, with uv's own installer or: python3 -m pip install --user uv"
exit 1
fi
say " uv: $uvbin"
if have systemctl && systemctl --user show-environment >/dev/null 2>&1; then systemd_ok=1; else systemd_ok=0; say " no systemd user session here: the service will be installed but not enabled; a start command is printed at the end"; fi
else
systemd_ok=0
have openssl || die "openssl is needed to generate the API key (it ships with macOS)"
fi
[ "$bz2_shim" = 1 ] && say " bzip2 is not installed: this install unpacks the models with a stand-in made from python3's bz2 module (nothing is installed on the system)"
say " prefix: $prefix"
say " config: $config_dir"
say " port: $port (loopback)"
say " licence: Unidatum Speech Free Use Licence v1.0, free for any use including commercial; installing means you accept it: $SITE/licence/ (embedding it in a product you distribute needs a commercial licence: https://www.unidatum.ie/en/contact)"
[ "$dry" = 1 ] && say " DRY RUN: nothing will be downloaded or written"
# --------------------------------------------------------------- the release
step "Release"
latest=$(curl -fsS "$SITE/dl/latest.json") || die "cannot read $SITE/dl/latest.json (is the network up?)"
current=$(printf '%s' "$latest" | sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' | head -n 1)
[ -n "$current" ] || die "the release list at $SITE/dl/latest.json has no version in it"
tag="${release:-$current}"
case "$tag" in v[0-9]*) ;; *) die "a release looks like v0.1.0, not '$tag'" ;; esac
v="${tag#v}"
pkg="$PRODUCT-$v-$plat.tar.gz"
if [ "$plat" = linux-arm64 ] && [ "$(printf '%s\n%s\n' "$v" 0.1.1 | sort -V | head -n 1)" != "0.1.1" ]; then
die "release $v has no Linux arm64 package (it starts at v0.1.1); the site will offer it once that release is published"
fi
say " current release: $current"; [ "$tag" != "$current" ] && say " requested: $tag"
say " package: $pkg"
# ---------------------------------------------------------- the install code
# The download page checks that a person is asking and hands out a short
# code; the site exchanges it exactly once for a fifteen-minute download
# ticket. If the site has no check configured it says so by returning an
# empty ticket, and the download is plain.
step "Install code"
if [ "$dry" = 1 ]; then
if [ -n "$code" ]; then say " would redeem code ${code%%-*}-.... for a download ticket (not done in a dry run; the code stays unused)"
elif [ "$agent" = 1 ]; then say " --agent: would ask $SITE/dl/agent/challenge for a proof of work, solve it here (about a second; perl, python3 or node: $(for t in perl python3 node; do have $t && printf '%s ' $t; done)) and exchange it for a single-use code; nothing is asked of the site in a dry run"
else say " no --code given: the download would fail unless the site's check is off (an AI agent can add --agent)"; fi
ticket=""
else
if [ -z "$code" ] && [ "$agent" = 1 ]; then agent_code; fi
code_clean=$(printf '%s' "$code" | tr -d '-' | tr '[:lower:]' '[:upper:]')
http=$(curl -sS -o /tmp/.us-redeem.$$ -w '%{http_code}' "$SITE/dl/redeem?code=$code_clean") || die "cannot reach $SITE"
ticket=$(cat /tmp/.us-redeem.$$ 2>/dev/null || true); rm -f /tmp/.us-redeem.$$
case "$http" in
200) if [ -n "$ticket" ]; then say " code accepted (it is now used up)"; else say " the site has no download check configured; plain download"; fi ;;
400) die "that is not an install code. A person gets one at $SITE/download and passes it as --code XXXX-XXXX; an AI agent installing on a person's behalf adds --agent instead of a code" ;;
404) die "unknown or already used install code. Codes work once; get a fresh one at $SITE/download" ;;
410) die "that install code has expired (they last fifteen minutes). Get a fresh one at $SITE/download" ;;
429) die "too many attempts from this address; wait a minute" ;;
*) die "the site answered $http to the install code: $ticket" ;;
esac
fi
# ---------------------------------------------------------------- download
step "Download and verify"
work=$(mktemp -d "${TMPDIR:-/tmp}/unidatum-speech-install.XXXXXX")
trap 'rm -rf "$work"' EXIT INT TERM
url="$SITE/dl/$tag/$pkg"
if [ "$dry" = 1 ]; then
say " would download $url"
say " would verify it against $url.sha256 and $SITE/dl/SHA256SUMS"
else
if [ -n "$ticket" ]; then dl="$url?t=$ticket"; else dl="$url"; fi
if [ "$agent" = 1 ]; then progress="-sS"; say " downloading $pkg (a few hundred MB; no progress bar in agent mode)"; else progress="--progress-bar"; fi
# shellcheck disable=SC2086
curl -fL $progress -o "$work/$pkg" "$dl" || die "download failed (a 403 means the ticket was refused: get a fresh code at $SITE/download)"
curl -fsS -o "$work/$pkg.sha256" "$url.sha256" || die "cannot fetch the checksum file $url.sha256"
curl -fsS -o "$work/SHA256SUMS" "$SITE/dl/SHA256SUMS" || die "cannot fetch $SITE/dl/SHA256SUMS"
got=$(sha256_of "$work/$pkg")
want=$(cut -d' ' -f1 "$work/$pkg.sha256")
listed=$(grep " $pkg\$" "$work/SHA256SUMS" | cut -d' ' -f1 || true)
if [ ${#want} -ne 64 ] || [ -n "$(printf '%s' "$want" | tr -d '0-9a-f')" ]; then die "the checksum file is malformed; not installing"; fi
if [ "$got" != "$want" ]; then
rm -f "$work/$pkg"
die "CHECKSUM MISMATCH for $pkg: downloaded $got, published $want. The download was deleted and nothing was installed. Try again; if it repeats, tell Unidatum."
fi
if [ -n "$listed" ] && [ "$listed" != "$got" ]; then
rm -f "$work/$pkg"
die "CHECKSUM MISMATCH: $pkg is $got but SHA256SUMS lists $listed. The download was deleted and nothing was installed."
fi
[ -z "$listed" ] && [ "$tag" = "$current" ] && die "SHA256SUMS does not list $pkg; not installing"
say " sha256 $got"
say " matches $pkg.sha256$([ -n "$listed" ] && printf ' and SHA256SUMS')"
fi
# --------------------------------------------------------- unpack and install
step "Install under $prefix"
name="$PRODUCT-$v-$plat"
run mkdir -p "$prefix/releases" "$config_dir"
if [ "$dry" = 1 ]; then
say " would unpack $pkg to $prefix/releases/$name"
else
rm -rf "$prefix/releases/$name"
tar -xzf "$work/$pkg" -C "$prefix/releases" || die "cannot unpack $pkg"
[ -x "$prefix/releases/$name/packaging/unidatum-speech" ] || die "the package has no binary at packaging/unidatum-speech"
fi
svc="$prefix/releases/$name"
# A profile for this machine. The package ships profiles for Unidatum's own
# machines, whose cpu lists would not match yours, so one is written from
# what this machine has: at most half the cpus, at most 8, one hardware thread
# per core, efficiency cores left out when the machine has both kinds and
# enough performance cores remain. The service checks the plan before it
# starts and refuses one over the budget.
step "A profile for this machine"
if [ "$family" = linux ]; then
ncpu=$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN)
atoms=""; [ -r /sys/devices/cpu_atom/cpus ] && atoms=$(cat /sys/devices/cpu_atom/cpus)
in_list() { # in_list N "a-b,c" -> 0 if N is in the list
il_found=1; il_old=$IFS; IFS=,
for part in $2; do case "$part" in
*-*) lo=${part%-*}; hi=${part#*-}; [ "$1" -ge "$lo" ] && [ "$1" -le "$hi" ] && il_found=0 ;;
*) [ "$1" = "$part" ] && il_found=0 ;;
esac; done
IFS=$il_old; return $il_found; }
allowed=$(taskset -pc $$ 2>/dev/null | sed 's/.*: //' || true) # a container or cpuset may restrict us
cands=""; ecands=""
# cpu numbers in numeric order (a glob would give cpu10 before cpu2)
for n in $(for d in /sys/devices/system/cpu/cpu[0-9]*; do echo "${d##*cpu}"; done | sort -n); do
sysdir=/sys/devices/system/cpu/cpu$n
[ -n "$allowed" ] && ! in_list "$n" "$allowed" && continue
first=$(sed 's/[-,].*//' "$sysdir/topology/thread_siblings_list" 2>/dev/null || echo "$n")
[ "$first" = "$n" ] || continue # one hardware thread per core
if [ -n "$atoms" ] && in_list "$n" "$atoms"; then ecands="$ecands $n"; else cands="$cands $n"; fi
done
exclude_e=false
if [ -n "$atoms" ]; then
if [ "$(echo "$cands" | wc -w)" -ge 3 ]; then exclude_e=true; say " hybrid CPU: efficiency cores ($atoms) left out"
else cands="$cands $ecands"; say " hybrid CPU, but fewer than three performance cores are available here: efficiency cores are used too"; fi
fi
budget=$((ncpu / 2)); [ "$budget" -gt 8 ] && budget=8
if [ -n "$cpus_wanted" ]; then
[ "$cpus_wanted" -le $((ncpu * 3 / 4)) ] || die "--cpus $cpus_wanted is more than three quarters of this machine's $ncpu cpus; the system needs the rest"
budget=$cpus_wanted
fi
else
ncpu=$(sysctl -n hw.ncpu); pcores=$(sysctl -n hw.perflevel0.physicalcpu 2>/dev/null || echo "$ncpu")
budget=$((ncpu / 2)); [ "$budget" -gt "$pcores" ] && budget=$pcores; [ "$budget" -gt 8 ] && budget=8
[ -z "$cpus_wanted" ] || say " --cpus is for Linux servers; ignored on macOS"
cands=""; i=0; while [ "$i" -lt "$pcores" ]; do cands="$cands $i"; i=$((i + 1)); done
exclude_e=true
say " Apple silicon: $pcores performance cores; the cpu numbers below are thread slots, not pins"
fi
[ "$budget" -ge 3 ] || die "this machine has $ncpu cpus; the service wants at least 3 for itself (half the machine), so it cannot run here"
# the first $budget candidates are the service's cpus
chosen=""; total=0
for c in $cands; do
[ "$total" -lt "$budget" ] || break
chosen="$chosen $c"; total=$((total + 1))
done
[ "$total" -ge 3 ] || die "fewer than three usable cpus found ($cands)"
# shellcheck disable=SC2086
set -- $chosen
if [ "$total" -ge 4 ]; then tts_n=1; else tts_n=0; fi
rest=$((total - tts_n))
split_cpus "$rest"
fast_n=$plan_fast; final_n=$plan_final
fast_cpus=""; final_cpus=""; tts_cpus=""; k=0
for c in "$@"; do
if [ "$k" -lt "$fast_n" ]; then fast_cpus="${fast_cpus:+$fast_cpus,}$c"
elif [ "$k" -lt $((fast_n + final_n)) ]; then final_cpus="${final_cpus:+$final_cpus,}$c"
elif [ "$tts_n" = 1 ] && [ -z "$tts_cpus" ]; then tts_cpus="$c"; fi
k=$((k + 1))
done
final_workers=$((final_n / 2)); [ "$final_workers" -lt 1 ] && final_workers=1
say " cpus: fast $fast_cpus | final $final_cpus ($final_workers worker(s)) | tts ${tts_cpus:-none} | budget $budget of $ncpu"
[ -z "$tts_cpus" ] && say " (fewer than four cpus for the service: text to speech is left out; edit the config to add it)"
say " the streaming stage gets about three times the cores of the final stage: it is the stage that limits live streams"
# How many live streams to admit. Only where it was measured (see stream_cap);
# elsewhere the service keeps its default.
cpu_kind=""
if [ "$family" = linux ]; then
if [ "$arch" = aarch64 ] || [ "$arch" = arm64 ]; then
# 0xd4f is Neoverse V2, the core of AWS Graviton4
grep -qi 'CPU part.*0xd4f' /proc/cpuinfo 2>/dev/null && cpu_kind=graviton4
elif grep -qi 'model name.*AMD EPYC' /proc/cpuinfo 2>/dev/null; then cpu_kind=epyc; fi
fi
sessions=$(stream_cap $((fast_n + final_n)) "$cpu_kind")
if [ -n "$sessions" ]; then
say " max_sessions $sessions: 85 percent of the live English streams measured clean on this kind of cpu (docs/COSTS.md)"
else
say " max_sessions: not measured on this kind of cpu, so the service's own limit applies (4 streams per fast cpu)"
fi
# Memory scales with the worker processes, not with the streams.
need=$(need_mb "$fast_n" "$final_workers")
if [ "$family" = linux ]; then have_mb=$(awk '/^MemTotal:/ { print int($2 / 1024) }' /proc/meminfo 2>/dev/null || echo 0)
else have_mb=$(( $(sysctl -n hw.memsize 2>/dev/null || echo 0) / 1048576 )); fi
say " memory: the workers need about $(( (need + 512) / 1024 )) GB resident (this machine has $(( (have_mb + 512) / 1024 )) GB)"
if [ "$have_mb" -gt 0 ] && [ "$have_mb" -lt $((need + 2048)) ]; then
say " WARNING: that leaves under 2 GB for the system. Expect swapping or the kernel stopping a worker."
say " Use fewer cpus (--cpus N, or a machine with more memory); each fast cpu costs about 0.8 GB."
fi
if [ -z "$cpus_wanted" ] && [ "$ncpu" -ge 16 ]; then
say " this machine has $ncpu cpus and the service uses $total of them; on a dedicated server, --cpus N (up to $((ncpu * 3 / 4))) uses more"
fi
max_fraction=0.5; [ "$budget" -gt $((ncpu / 2)) ] && max_fraction=0.8
sessions_line="# max_sessions: not set; the service allows 4 streams per fast cpu"
[ -n "$sessions" ] && sessions_line="max_sessions = $sessions # 85 percent of the clean count measured on this kind of cpu; new sessions beyond it get 503"
profile="$svc/config/this-machine.toml"
if [ "$family" = linux ]; then
models="$prefix/models"; python="$prefix/venv/bin/python"; workerdir="$prefix/worker"; data="$prefix"; socket_line=""
else
models="$prefix/models"; python="$prefix/venv/bin/python"; workerdir="$prefix/worker"; data="$prefix/data"; socket_line="socket_dir = \"$prefix/run\""
fi
tts_block="[tts]
cpus = \"$tts_cpus\"
workers = $tts_n
kokoro_dir = \"$models/kokoro-multi-lang-v1_0\"
voice = \"af_heart\"
max_queue = 8
max_chars = 5000"
[ -z "$tts_cpus" ] && tts_block="[tts]
cpus = \"\"
workers = 0
kokoro_dir = \"$models/kokoro-multi-lang-v1_0\""
if [ "$dry" = 1 ]; then
say " would write $profile"
else
cat > "$profile" <