#!/bin/sh set -eu unset LICENSE_KEY AUTH ENTERED CHECK_URL="https://get.optakt.ai/" BOOTSTRAP_BASE="https://get.optakt.ai/?fingerprint=" KEYGEN_PRODUCT="13764e1a-e242-4dba-9880-bcb0a1a1c0c6" # A trace the acceptance harness asks for (OPTAKT_TUI_TRACE: the CLI writes # each question it shows there, by handle) reaches the CLI through sudo, # which resets the environment; unset, it is passed empty, which traces # nothing. One quoted word, so a path with spaces stays one (Astra H5, # round 4: an unquoted fragment split it into arguments). TRACE_ENV="OPTAKT_TUI_TRACE=${OPTAKT_TUI_TRACE:-}" # On a Mac this script runs macOS's own tools: what a person installed can # come first on their PATH and not run at all (Asiya's Mac, 15/11/2026: an # Intel Homebrew's /usr/local/bin/openssl on Apple silicon, 'Bad CPU type', # so no machine fingerprint and no licence check). The person's PATH is put # back before the CLI takes over, and its every entry is still searched for # other optakt copies. A test that plays a Mac puts its own directory in # place of macSystemPath, so its fakes stay the system's tools and nothing # reaches the host's real sudo (15/11/2026: a test that did not replaced the # host's /usr/local/bin/optakt). The system is read once, before the PATH # changes: every later step asks RUN_OS and RUN_ARCH, so all see one answer. RUN_OS=$(uname -s) RUN_ARCH=$(uname -m) PERSON_PATH=$PATH case "$RUN_OS" in Darwin) PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH"; export PATH ;; esac if [ -t 1 ]; then RUN_OK='\033[32m✓\033[0m'; RUN_NO='\033[31m✗\033[0m'; RUN_RED='\033[31m' RUN_DIM='\033[2m'; RUN_BOLD='\033[1m'; RUN_ACC='\033[34m'; RUN_END='\033[0m' else RUN_OK='✓'; RUN_NO='✗'; RUN_RED=''; RUN_DIM=''; RUN_BOLD=''; RUN_ACC=''; RUN_END='' fi run_stage() { [ "${RUN_STAGE:-}" = "$1" ] && return 0 [ -z "${RUN_STAGE:-}" ] || printf '\n' RUN_STAGE="$1"; export RUN_STAGE printf '%b→ %s%b\n' "$RUN_BOLD" "$1" "$RUN_END" } # A step that takes time shows while it runs, as the CLI's does: "… label" # until its ✓ or ✗ takes the line (on a terminal; in a log, a line of its # own). A wait with nothing on screen read as a hang (alpha.33: the # download before the CLI's first line). run_begin() { RUN_BEGUN=1 if [ -t 1 ]; then printf ' %b… %s%b' "$RUN_DIM" "$1" "$RUN_END"; else printf ' … %s\n' "$1"; fi } run_end_begun() { [ -n "${RUN_BEGUN:-}" ] || return 0 RUN_BEGUN="" if [ -t 1 ]; then printf '\r\033[K'; fi } run_done() { run_end_begun; printf ' %b %s\n' "$RUN_OK" "$1"; } # A stop the person asked for is information, never a failure: the bar of # the notice the CLI leaves standing (Esc in the Go installer). run_info() { printf ' %b│%b %s\n' "$RUN_ACC" "$RUN_END" "$1"; } run_fail() { RUN_FAILED=1 run_end_begun printf ' %b %s\n' "$RUN_NO" "$1" >&2 [ -z "${2:-}" ] || printf ' %b■ %s%b\n' "$RUN_RED" "$2" "$RUN_END" >&2 } run_note() { printf ' %b› %s%b\n' "$RUN_DIM" "$1" "$RUN_END"; } # Every exit says why. The failures this script knows say it on their own # line (run_fail), ending with the status of the command that failed; a # command that fails under set -e ends the run with its status all the # same, and with nothing said yet this is the sentence. run_exit() { RUN_STATUS=$? [ "$RUN_STATUS" -eq 0 ] || [ -n "${RUN_FAILED:-}" ] || run_fail "The install stopped because a command failed (status $RUN_STATUS); nothing more was done." } trap run_exit EXIT # A question takes the rows it is drawn on, counted at the terminal's # width: once answered it gives them back (run_answered), and only its # recap stays, as every question the CLI asks (Max, 01/11/2026 and # 09/11/2026: the license question stayed on screen whole). run_rows() { RUN_COLS=$(stty size < /dev/tty 2>/dev/null | cut -d' ' -f2) [ -n "$RUN_COLS" ] && [ "$RUN_COLS" -gt 0 ] 2>/dev/null || RUN_COLS=80 echo $(( ($1 + RUN_COLS - 1) / RUN_COLS > 0 ? ($1 + RUN_COLS - 1) / RUN_COLS : 1 )) } run_ask() { run_end_begun printf ' %b?%b %s\n\n' "$RUN_ACC" "$RUN_END" "$1" >&2 RUN_ASKED=$(( $(run_rows $((4 + ${#1}))) + 1 )) if [ -n "$2" ]; then printf ' %b│%b %s\n\n' "$RUN_ACC" "$RUN_END" "$2" >&2 RUN_ASKED=$(( RUN_ASKED + $(run_rows $((6 + ${#2}))) + 1 )) fi printf ' %b›%b ' "$RUN_ACC" "$RUN_END" >&2 } # run_answered takes the question back, its answer line included ($1 its # characters as shown), then writes its recap when there is one ($2): the # question's step, ✓ and what was answered, never a key's value. run_answered() { if [ -t 2 ]; then printf '\033[%dA\r\033[J' $(( RUN_ASKED + $(run_rows $((6 + $1))) )) >&2 fi [ -z "${2:-}" ] || run_done "$2" } # run_sudo asks for the password sudo needs, once, as the run asks every # question: on a line of its own, never on a step's open line, and taken # back once answered (Max, 16/11/2026: "[sudo] password for awfm9:" printed # into the Release step's "… optakt" line). sudo keeps the credential for # the run, so every later sudo here asks nothing. When sudo needs no # password, or there is no terminal to ask on, nothing is asked: sudo then # runs, or refuses, as it does. run_sudo() { sudo -n true 2>/dev/null && return 0 { [ -r /dev/tty ] && ( : < /dev/tty ) 2>/dev/null; } || return 0 run_ask "Your password for sudo on this machine" "" if sudo -p '' -v < /dev/tty; then run_answered 0 return 0 fi run_answered 0 run_fail "sudo did not accept the password; nothing was changed." exit 1 } case "$RUN_OS" in Darwin) [ -f "$HOME/Library/Application Support/io.optakt.one/config/one.env" ] && FOUND_DEPLOYMENT=1 ;; *) [ -d /etc/optakt ] && FOUND_DEPLOYMENT=1 ;; esac if [ -z "${FOUND_DEPLOYMENT:-}" ] && [ -r /dev/tty ] && ( : < /dev/tty ) 2>/dev/null; then run_stage Requirements printf ' %b│%b This install needs:\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • sudo on this machine\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • about 6 GB of disk space\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • Telegram bot token\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • LLM API or subscription\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • %s\n' "$RUN_ACC" "$RUN_END" "Optakt licence key" >&2 printf ' %b│%b • Voyage AI API key (optional)\n' "$RUN_ACC" "$RUN_END" >&2 printf ' %b│%b • ElevenLabs API key (optional)\n' "$RUN_ACC" "$RUN_END" >&2 fi if [ -z "${HOME:-}" ]; then run_fail "HOME is not set; the Optakt licence location cannot be established." exit 1 fi LICENSE_DIR="$HOME/.optakt" LICENSE_FILE="$LICENSE_DIR/license" TMP=$(umask 077; mktemp -d) trap 'run_exit; rm -rf "$TMP"' EXIT check_license() { case "$1" in ""|*[!A-Za-z0-9-]*) return 1 ;; esac # The key goes to curl on its stdin (printf is the shell's own, never # a process): an argument is readable by every account on the machine. CURL_STATUS="" CHECK_STATUS=$(printf 'Authorization: License %s\n' "$1" | curl -sS --proto '=https' --proto-redir '=https' -o "$TMP/license-check" -w '%{http_code}' \ -H @- -H "X-Optakt-License-Check: 1" "$CHECK_URL") || { CURL_STATUS=$?; return 2; } case "$CHECK_STATUS" in 204) return 0 ;; 403) return 1 ;; 409) return 3 ;; *) return 2 ;; esac } # A subshell, so the umask stays here: left set, it made the Mac CLI link # that sudo creates later root's with mode 0700. save_license() ( umask 077 mkdir -p "$LICENSE_DIR" chmod 0700 "$LICENSE_DIR" printf '%s' "$1" > "$LICENSE_FILE.tmp.$$" chmod 0600 "$LICENSE_FILE.tmp.$$" mv -f "$LICENSE_FILE.tmp.$$" "$LICENSE_FILE" ) restore_license_tty() { stty "$LICENSE_STTY_STATE" < /dev/tty 2>/dev/null || true } read_masked_license() { ENTERED="" LICENSE_STTY_STATE=$(stty -g < /dev/tty) || return 1 trap 'restore_license_tty; printf "\n" >&2; run_fail "Interrupted; nothing was installed."; exit 1' HUP INT TERM stty -echo -icanon min 1 time 0 < /dev/tty || return 1 while :; do LICENSE_CHAR=$(dd bs=1 count=1 < /dev/tty 2>/dev/null) || { restore_license_tty trap - HUP INT TERM return 1 } case "$LICENSE_CHAR" in "") break ;; "$(printf '\033')") restore_license_tty trap - HUP INT TERM return 2 ;; "$(printf '\010')"|"$(printf '\177')") if [ -n "$ENTERED" ]; then ENTERED=${ENTERED%?} printf '\b \b' >&2 fi ;; *) ENTERED="$ENTERED$LICENSE_CHAR" printf '•' >&2 ;; esac done restore_license_tty trap - HUP INT TERM return 0 } # --- Existing deployment --- # On Linux the deployment's directory is the service's (0750): the person # can see that it exists, never its env file (Dev107, 29/10/2026). same_file() { a=$(readlink -f "$1" 2>/dev/null || printf '%s' "$1") b=$(readlink -f "$2" 2>/dev/null || printf '%s' "$2") [ "$a" = "$b" ] } remove_other_clis() { old_ifs=$IFS IFS=: for dir in $PATH; do IFS=$old_ifs other="$dir/optakt" { [ -e "$other" ] || [ -L "$other" ]; } || continue same_file "$other" "$1" && continue if rm -f "$other" 2>/dev/null || sudo -n rm -f "$other" 2>/dev/null; then run_stage Release run_done "Removed an outdated optakt at $other" else run_fail "Could not remove the outdated optakt at $other: delete it, or run $1." fi done IFS=$old_ifs hash -r 2>/dev/null || true } DEPLOYED="" case "$RUN_OS" in Darwin) if [ -f "$HOME/Library/Application Support/io.optakt.one/config/one.env" ]; then DEPLOYED="$HOME/Library/Application Support/io.optakt.one/bin/optakt" [ -x "$DEPLOYED" ] || DEPLOYED=/usr/local/bin/optakt fi ;; *) if [ -d /etc/optakt ]; then DEPLOYED=/usr/local/bin/optakt; fi ;; esac if [ -n "$DEPLOYED" ] && [ -x "$DEPLOYED" ]; then remove_other_clis "$DEPLOYED" # A CLI that knows --released goes on under a Release stage opened here. RELEASED="" [ -z "${RUN_STAGE:-}" ] || ! "$DEPLOYED" upgrade --released --help >/dev/null 2>&1 || RELEASED="--released" PATH=$PERSON_PATH; export PATH case "$RUN_OS" in Darwin) exec "$DEPLOYED" upgrade $RELEASED ;; *) run_sudo; exec sudo env "$TRACE_ENV" "$DEPLOYED" upgrade $RELEASED ;; esac fi # --- New deployment --- LICENSE_KEY="" NOTICE="" if [ -s "$LICENSE_FILE" ]; then LICENSE_KEY=$(cat "$LICENSE_FILE") CHECK_RESULT=0 check_license "$LICENSE_KEY" || CHECK_RESULT=$? case "$CHECK_RESULT" in 0|3) chmod 0700 "$LICENSE_DIR" chmod 0600 "$LICENSE_FILE" ;; 1) LICENSE_KEY="" NOTICE="The saved licence key was refused or has expired." ;; *) run_fail "Could not validate the saved licence key; nothing was downloaded." exit "${CURL_STATUS:-1}" ;; esac fi if [ -z "$LICENSE_KEY" ]; then if [ -r /dev/tty ] && ( : < /dev/tty ) 2>/dev/null; then while :; do run_stage "Licence" run_ask "Licence key" "${NOTICE:+$NOTICE }Enter your licence key and press Enter to proceed. Esc cancels; nothing is installed yet." # set -e would end the script on the read's own status: Esc (2) is # said and stops, a failed read (1) is the next branch's. LICENSE_READ=0 read_masked_license || LICENSE_READ=$? printf '\n' >&2 run_answered "${#ENTERED}" if [ "$LICENSE_READ" = 2 ]; then # Esc is the person's stop, not a failure (the Go installer's # errInstallStopped is information, and the run succeeds). run_info "Cancelled; nothing was installed." exit 0 fi if [ "$LICENSE_READ" != 0 ]; then run_fail "No choice was read; nothing was downloaded." exit 1 fi LICENSE_KEY="$ENTERED" if [ -z "$LICENSE_KEY" ]; then NOTICE="A licence key is required." continue fi CHECK_RESULT=0 run_begin "Licence key" check_license "$LICENSE_KEY" || CHECK_RESULT=$? run_end_begun case "$CHECK_RESULT" in 0|3) break ;; 1) LICENSE_KEY="" NOTICE="That licence key was refused or has expired." ;; *) run_fail "Could not validate the licence key; nothing was downloaded." exit "${CURL_STATUS:-1}" ;; esac done else run_stage "Licence" run_fail "${NOTICE:-A licence key is required; save it to ~/.optakt/license or run the command in a terminal.}" exit 1 fi save_license "$LICENSE_KEY" run_stage "Licence" # The file is always under HOME (licensePathsScript); it is shown from ~. # POSIX only: the script runs under sh, which is dash on Debian and Ubuntu. run_done "Licence key · your key, saved to ~/${LICENSE_FILE#"$HOME"/}" fi case "$RUN_OS" in Linux) if [ -s /var/lib/dbus/machine-id ]; then MACHINE_ID=$(cat /var/lib/dbus/machine-id) elif [ -s /etc/machine-id ]; then MACHINE_ID=$(cat /etc/machine-id) else run_fail "This machine has no stable machine ID; the Optakt licence was not activated." exit 1 fi ;; Darwin) MACHINE_ID=$(/usr/sbin/ioreg -rd1 -c IOPlatformExpertDevice \ | awk -F'"' '/IOPlatformUUID/ { print $(NF-1); exit }') ;; *) run_fail "Unsupported OS: $RUN_OS"; exit 1 ;; esac if [ -z "$MACHINE_ID" ] || ! command -v openssl >/dev/null 2>&1; then run_fail "Could not establish this machine's licensed identity; nothing was downloaded." exit 1 fi FINGERPRINT=$(printf '%s' "$KEYGEN_PRODUCT" \ | openssl dgst -sha256 -hmac "$MACHINE_ID" \ | awk '{ print $NF }') BOOTSTRAP_URL="$BOOTSTRAP_BASE$FINGERPRINT" STATUS=$(printf 'Authorization: License %s\n' "$LICENSE_KEY" | curl -sS --proto '=https' --proto-redir '=https' -o "$TMP/bootstrap" -w '%{http_code}' \ -H @- "$BOOTSTRAP_URL") || { code=$? run_fail "Could not reach get.optakt.ai; nothing was downloaded." exit "$code" } if [ "$STATUS" != "200" ]; then run_fail "get.optakt.ai answered HTTP $STATUS; nothing was downloaded." exit 1 fi # The installer it runs says why it fails on its own line. sh "$TMP/bootstrap" || { code=$?; RUN_FAILED=1; exit "$code"; }