#!/bin/sh # Official Z8Harness installer for macOS and Linux. set -eu fail() { printf 'Z8Harness: %s\n' "$*" >&2; exit 1; } fetch_file() { status=$(curl --silent --show-error --fail --retry 2 --connect-timeout 15 \ --max-time 900 --max-filesize "$3" --output "$2" --write-out '%{http_code}' "$1") || fail "Download failed: $1" [ "$status" = 200 ] || fail "Unexpected HTTP $status from $1" } setup_path() { case ":$PATH:" in *":$HOME/.local/bin:"*) return ;; esac line='export PATH="$HOME/.local/bin:$PATH"' case "${SHELL##*/}" in zsh) profile=${ZDOTDIR:-$HOME}/.zshrc ;; bash) profile=$HOME/.bashrc if [ "$system" = Darwin ]; then for profile in "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.profile"; do [ -f "$profile" ] && break done fi ;; fish) profile=${XDG_CONFIG_HOME:-$HOME/.config}/fish/config.fish line='fish_add_path "$HOME/.local/bin"' ;; *) profile=$HOME/.profile ;; esac if mkdir -p "$(dirname "$profile")" && { grep -Fqx "$line" "$profile" 2>/dev/null || printf '\n%s\n' "$line" >> "$profile"; }; then printf 'PATH updated in %s. Open a new terminal, or run:\n' "$profile" else printf 'Add the following line to your shell profile:\n' fi printf ' %s\n' "$line" } main() { [ "$#" -le 1 ] || fail 'Usage: sh install.sh [stable|latest|version]' target=${1:-stable} case "$target" in -h|--help) printf 'Usage: sh install.sh [stable|latest|version]\n'; return ;; esac base=${AIAGENT_NATIVE_RELEASE_BASE_URL:-https://www.z8.com.cn/updates/cli} base=${base%/} case "$base" in *'@'*|*'?'*|*'#'*|*[[:space:]]*) fail 'Invalid release URL.' ;; esac case "$base/" in https://*|http://127.0.0.1:*|http://127.0.0.1/*|http://localhost:*|http://localhost/*) ;; *) fail 'Use an HTTPS release URL.' ;; esac command -v curl >/dev/null || fail 'Install curl and retry.' system=$(uname -s) case "$(uname -m)" in x86_64|amd64) cpu=x64 ;; aarch64|arm64) cpu=arm64 ;; *) fail "Unsupported CPU: $(uname -m)" ;; esac case "$system" in Darwin) # Detect Apple silicon if the user's shell is running under Rosetta. if [ "$cpu" = x64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || :)" = 1 ]; then cpu=arm64; fi platform=darwin-$cpu ;; Linux) platform=linux-$cpu if ! getconf GNU_LIBC_VERSION >/dev/null 2>&1; then if ldd --version 2>&1 | grep -qi musl; then platform=$platform-musl else fail 'Could not determine the Linux libc.'; fi fi ;; *) fail "Unsupported operating system: $system" ;; esac umask 077 stage=$(mktemp -d "${TMPDIR:-/tmp}/z8harness.XXXXXXXX") trap 'rm -rf "$stage"' 0 trap 'exit 1' HUP INT TERM case "$target" in stable|latest) fetch_file "$base/$target" "$stage/channel" 4096; version=$(cat "$stage/channel") ;; *) version=$target ;; esac version=${version#v} case "$version" in ''|*[!0-9A-Za-z.+-]*) fail 'Invalid release version.' ;; esac [ "${#version}" -le 96 ] && printf '%s\n' "$version" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$' || fail 'Invalid release version.' fetch_file "$base/$version/SHA256SUMS" "$stage/sums" 65536 asset=$platform/Z8Harness expected=$(awk -v path="$asset" '$2 == path { print $1; n++ } END { if (n != 1) exit 1 }' "$stage/sums") || { available=$(awk 'NF == 2 { split($2, a, "/"); print a[1] }' "$stage/sums" | tr '\n' ' ') fail "No package for $platform in $version. Available: $available" } printf '%s\n' "$expected" | grep -Eq '^[a-f0-9]{64}$' || fail 'Invalid release checksum.' printf 'Downloading Z8Harness %s (%s)…\n' "$version" "$platform" fetch_file "$base/$version/$asset" "$stage/Z8Harness" 536870912 if command -v sha256sum >/dev/null; then checksum=$(sha256sum < "$stage/Z8Harness") elif command -v shasum >/dev/null; then checksum=$(shasum -a 256 < "$stage/Z8Harness") else fail 'A SHA-256 utility is required.'; fi [ "${checksum%% *}" = "$expected" ] || fail 'SHA-256 mismatch. Download discarded.' chmod 700 "$stage/Z8Harness" AIAGENT_NATIVE_RELEASE_BASE_URL=$base "$stage/Z8Harness" install "$target"