#!/usr/bin/env sh # Synthia Terminal installer for macOS and Linux · by AI-SNS.IO # # curl -fsSL /install.sh | sh # # Detects the OS and CPU, downloads # synthia-terminal---.tar.gz # and SHA256SUMS from the download base URL, refuses anything whose SHA-256 # does not match, installs the app files to ~/.local/lib/synthia-terminal # (kept apart from the data folder, which `synthia data` / `uninstall` own) # and puts `synthia` and `synthia-terminal` in ~/.local/bin. # # Environment: # SYNTHIA_TERMINAL_BASE_URL where the archives and SHA256SUMS live # (https://, or http:// on localhost for tests) # SYNTHIA_TERMINAL_VERSION version to install (default below) # SYNTHIA_TERMINAL_BIN_DIR where the commands go (default ~/.local/bin) # SYNTHIA_TERMINAL_APP_DIR where the app files go (default ~/.local/lib/synthia-terminal) # SYNTHIA_TERMINAL_NO_PATH=1 do not touch shell rc files # SYNTHIA_TERMINAL_TEST_LOCAL_HTTP=1 # tests only: allow http:// on 127.0.0.1 / # localhost (everything else is https-only) # # Integrity, not authenticity: SHA256SUMS comes from the same host as the # archives, so the check catches a damaged or swapped download on the way, # not a compromised host. TODO(signing): verify a signature over SHA256SUMS # (and Apple / Authenticode signatures) once the AI-SNS.IO certificates exist. set -eu # Served by the Synthia Platform (/admin/downloads). Override with SYNTHIA_TERMINAL_BASE_URL. DEFAULT_BASE_URL="https://platform.ai-sns.io/dl/terminal" DEFAULT_VERSION="0.1.0" BASE_URL="${SYNTHIA_TERMINAL_BASE_URL:-$DEFAULT_BASE_URL}" VERSION="${SYNTHIA_TERMINAL_VERSION:-$DEFAULT_VERSION}" while [ "${BASE_URL%/}" != "$BASE_URL" ]; do BASE_URL="${BASE_URL%/}"; done say() { printf '%s\n' "$*"; } fail() { printf 'エラー / error: %s\n' "$*" >&2; exit 1; } have() { command -v "$1" >/dev/null 2>&1; } PROTO="=https" case "$BASE_URL" in https://*) ;; http://127.0.0.1:* | http://localhost:* | http://127.0.0.1/* | http://localhost/*) [ "${SYNTHIA_TERMINAL_TEST_LOCAL_HTTP:-0}" = 1 ] || fail "SYNTHIA_TERMINAL_BASE_URL must be https://: $BASE_URL" PROTO="=http" ;; *) fail "SYNTHIA_TERMINAL_BASE_URL must be https://: $BASE_URL" ;; esac case "$VERSION" in *[!0-9A-Za-z.-]* | "") fail "invalid SYNTHIA_TERMINAL_VERSION: $VERSION" ;; esac case "$(uname -s)" in Darwin) OS=macos ;; Linux) OS=linux ;; *) fail "this installer supports macOS and Linux; on Windows use install.ps1" ;; esac case "$(uname -m)" in arm64 | aarch64) ARCH=arm64 ;; x86_64 | amd64) ARCH=x64 ;; *) fail "unsupported CPU: $(uname -m)" ;; esac # Rosetta: an arm64 Mac running this shell under x86_64 still wants arm64. if [ "$OS" = macos ] && [ "$ARCH" = x64 ] && [ "$(sysctl -in sysctl.proc_translated 2>/dev/null || echo 0)" = 1 ]; then ARCH=arm64 fi ASSET="synthia-terminal-${VERSION}-${OS}-${ARCH}.tar.gz" APP_DIR="${SYNTHIA_TERMINAL_APP_DIR:-$HOME/.local/lib/synthia-terminal}" BIN_DIR="${SYNTHIA_TERMINAL_BIN_DIR:-$HOME/.local/bin}" fetch() { if have curl; then if [ "$PROTO" = "=https" ]; then curl --proto '=https' --tlsv1.2 -fsSL --retry 3 -o "$2" "$1" else curl --proto '=http' -fsSL --retry 3 -o "$2" "$1" fi elif have wget; then if [ "$PROTO" = "=https" ]; then wget --https-only --secure-protocol=TLSv1_2 -q -O "$2" "$1" else wget -q -O "$2" "$1" fi else fail "curl or wget is required" fi } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{ print tolower($1) }' elif have shasum; then shasum -a 256 "$1" | awk '{ print tolower($1) }' elif have openssl; then openssl dgst -sha256 -r "$1" | awk '{ print tolower($1) }' else fail "cannot verify the download: install sha256sum, shasum or openssl" fi } WORK="$(mktemp -d "${TMPDIR:-/tmp}/synthia-terminal-install.XXXXXX")" cleanup() { rm -rf "$WORK"; } trap cleanup EXIT trap 'cleanup; exit 130' INT trap 'cleanup; exit 143' TERM say "Synthia Terminal ${VERSION} (${OS}-${ARCH}) をダウンロードしています…" fetch "${BASE_URL}/SHA256SUMS" "$WORK/SHA256SUMS" || fail "could not download ${BASE_URL}/SHA256SUMS" fetch "${BASE_URL}/${ASSET}" "$WORK/${ASSET}" || fail "could not download ${BASE_URL}/${ASSET}" # SHA256SUMS lines: "<64 hex> " (sha256sum format; "*name" too). EXPECTED="$(awk -v f="$ASSET" '{ n = $2; sub(/^\*/, "", n); if (n == f) print tolower($1) }' "$WORK/SHA256SUMS")" case "$EXPECTED" in "") fail "${ASSET} is not listed in SHA256SUMS" ;; *[!0-9a-f]*) fail "SHA256SUMS has a malformed entry for ${ASSET}" ;; esac [ "${#EXPECTED}" -eq 64 ] || fail "SHA256SUMS has a malformed entry for ${ASSET}" ACTUAL="$(sha256_of "$WORK/${ASSET}")" if [ "$EXPECTED" != "$ACTUAL" ]; then printf 'SHA-256 mismatch for %s\n expected: %s\n actual: %s\n' "$ASSET" "$EXPECTED" "$ACTUAL" >&2 fail "the download did not match SHA256SUMS; nothing was installed" fi say "SHA-256 OK (${ASSET})" mkdir -p "$WORK/x" tar -xzf "$WORK/${ASSET}" -C "$WORK/x" STAGE="$(find "$WORK/x" -mindepth 1 -maxdepth 1 -type d | head -n 1)" [ -n "$STAGE" ] && [ -f "$STAGE/synthia" ] || fail "unexpected archive layout (no synthia binary)" # Only ever replace a folder this installer made (marker file), so a # mistyped SYNTHIA_TERMINAL_APP_DIR cannot delete someone's data. MARKER=".synthia-terminal-install" if [ -e "$APP_DIR" ] && [ ! -f "$APP_DIR/$MARKER" ]; then if [ -d "$APP_DIR" ] && [ -z "$(ls -A "$APP_DIR" 2>/dev/null)" ]; then rmdir "$APP_DIR" else fail "$APP_DIR exists and is not a Synthia Terminal install (no $MARKER); refusing to replace it" fi fi # Replace the app folder in one rename; a running copy keeps its old inode. mkdir -p "$(dirname "$APP_DIR")" NEW="${APP_DIR}.new.$$" OLD="${APP_DIR}.old.$$" rm -rf "$NEW" cp -R "$STAGE" "$NEW" chmod 755 "$NEW/synthia" printf 'Synthia Terminal %s\n' "$VERSION" > "$NEW/$MARKER" if [ -e "$APP_DIR" ]; then mv "$APP_DIR" "$OLD"; fi mv "$NEW" "$APP_DIR" rm -rf "$OLD" mkdir -p "$BIN_DIR" # Symlinks: the binary resolves grammars/, node_modules/ and vendor/ next to # its real path, which a symlink keeps. An existing `synthia` that is not # ours is left alone (warned); `synthia-terminal` is always ours to write. ours() { [ -L "$1" ] && case "$(readlink "$1")" in */synthia-terminal/synthia | "$APP_DIR/synthia") true ;; *) false ;; esac } SYNTHIA_NAME="synthia" if [ -e "$BIN_DIR/synthia" ] || [ -L "$BIN_DIR/synthia" ]; then if ! ours "$BIN_DIR/synthia"; then say "注意 / note: $BIN_DIR/synthia already exists and is not Synthia Terminal; left as is. Use \`synthia-terminal\`." SYNTHIA_NAME="synthia-terminal" fi fi [ "$SYNTHIA_NAME" = "synthia" ] && ln -sfn "$APP_DIR/synthia" "$BIN_DIR/synthia" ln -sfn "$APP_DIR/synthia" "$BIN_DIR/synthia-terminal" if [ "$OS" = macos ]; then # Downloads by curl carry no quarantine flag; clear it anyway in case the # archive came through a browser. xattr -dr com.apple.quarantine "$APP_DIR" 2>/dev/null || true fi PATH_NOTE="" case ":${PATH:-}:" in *":$BIN_DIR:"*) ;; *) if [ "${SYNTHIA_TERMINAL_NO_PATH:-0}" = 1 ]; then PATH_NOTE="PATH に追加してください / add to PATH: export PATH=\"$BIN_DIR:\$PATH\"" else case "$(basename "${SHELL:-sh}")" in zsh) RC="$HOME/.zshrc"; LINE="export PATH=\"$BIN_DIR:\$PATH\"" ;; bash) if [ "$OS" = macos ]; then RC="$HOME/.bash_profile"; else RC="$HOME/.bashrc"; fi; LINE="export PATH=\"$BIN_DIR:\$PATH\"" ;; fish) RC="$HOME/.config/fish/config.fish"; LINE="set -gx PATH $BIN_DIR \$PATH" ;; *) RC="$HOME/.profile"; LINE="export PATH=\"$BIN_DIR:\$PATH\"" ;; esac MARK="# added by synthia-terminal installer" mkdir -p "$(dirname "$RC")" touch "$RC" if ! grep -qsF "$MARK" "$RC"; then printf '\n%s\n%s\n' "$MARK" "$LINE" >> "$RC"; fi PATH_NOTE="新しいターミナルを開くと synthia が使えます (PATH: $RC) / open a new terminal to use synthia" fi ;; esac say "" if [ "$SYNTHIA_NAME" = "synthia" ]; then say "Synthia Terminal をインストールしました。\`synthia login\` で有効化してください。" say " ${BIN_DIR}/synthia (alias: synthia-terminal)" else say "Synthia Terminal をインストールしました。\`synthia-terminal login\` で有効化してください。" say " ${BIN_DIR}/synthia-terminal" fi [ -n "$PATH_NOTE" ] && say " $PATH_NOTE" if [ "$OS" = macos ]; then say "" say "macOS: このビルドは未署名です。初回起動がブロックされた場合は" say " システム設定 → プライバシーとセキュリティ →「このまま開く」を選んでください。" say " (Unsigned build: if macOS blocks the first launch, open System Settings →" say " Privacy & Security and choose \"Open Anyway\".)" fi