#!/bin/sh set -eu version=0.5.2 base=${AGENTSOCKET_RELEASE_URL:-https://agentsocket.xyz/releases/$version} case "$(uname -s)" in Darwin) os=darwin;; Linux) os=linux;; *) echo 'Use the PowerShell installer on Windows.' >&2; exit 1;; esac case "$(uname -m)" in arm64|aarch64) arch=arm64;; x86_64|amd64) arch=x64;; *) echo 'Unsupported CPU architecture.' >&2; exit 1;; esac name=agentsocket-$os-$arch tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT HUP INT TERM curl -fsSL "$base/$name" -o "$tmp/$name" curl -fsSL "$base/SHA256SUMS" -o "$tmp/SHA256SUMS" expected=$(awk -v name="$name" '$2==name {print $1}' "$tmp/SHA256SUMS") [ ${#expected} -eq 64 ] || { echo 'Release checksum missing.' >&2; exit 1; } if command -v sha256sum >/dev/null 2>&1; then actual=$(sha256sum "$tmp/$name" | awk '{print $1}'); else actual=$(shasum -a 256 "$tmp/$name" | awk '{print $1}'); fi [ "$actual" = "$expected" ] || { echo 'Release checksum verification failed.' >&2; exit 1; } bin=${AGENTSOCKET_INSTALL_DIR:-} if [ -z "$bin" ]; then case ":$PATH:" in *":$HOME/.local/bin:"*) bin=$HOME/.local/bin;; esac if [ -z "$bin" ]; then previous_ifs=$IFS; IFS=: for candidate in $PATH; do case "$candidate" in "$HOME"/*|/usr/local/bin) if [ -d "$candidate" ] && [ -w "$candidate" ]; then if [ "$os" = darwin ]; then owner_uid=$(stat -f '%u' "$candidate"); mode=$(stat -f '%Lp' "$candidate"); else owner_uid=$(stat -c '%u' "$candidate"); mode=$(stat -c '%a' "$candidate"); fi case "$mode" in *[2367][0-7]|*[2367]) continue;; esac if [ "$owner_uid" = "$(id -u)" ]; then bin=$candidate; break; fi fi;; esac done IFS=$previous_ifs fi bin=${bin:-$HOME/.local/bin} fi # Reject symlink components and directories writable by another principal. check=$bin while [ "$check" != / ] && [ "$check" != . ]; do [ ! -L "$check" ] || { echo 'Unsafe symlink installation directory.' >&2; exit 1; } [ "$check" != "$HOME" ] || break parent=$(dirname "$check") [ "$parent" != "$check" ] || break check=$parent done mkdir -p "$bin" if [ "$os" = darwin ]; then owner_uid=$(stat -f '%u' "$bin"); mode=$(stat -f '%Lp' "$bin"); else owner_uid=$(stat -c '%u' "$bin"); mode=$(stat -c '%a' "$bin"); fi [ "$owner_uid" = "$(id -u)" ] || { echo 'Installation directory must belong to the current user.' >&2; exit 1; } case "$mode" in *[2367][0-7]|*[2367]) echo 'Installation directory must not be group or other writable.' >&2; exit 1;; esac # Every replaceable ancestor is part of the executable trust boundary. A # root/current-user-owned sticky directory (for example /tmp) is safe because # another principal cannot rename this user's child entry. check=$(cd "$bin" && pwd -P) while [ "$check" != / ] && [ "$check" != . ]; do [ ! -L "$check" ] || { echo 'Unsafe symlink installation directory.' >&2; exit 1; } if [ "$os" = darwin ]; then ancestor_uid=$(stat -f '%u' "$check"); ancestor_mode=$(stat -f '%Lp' "$check"); else ancestor_uid=$(stat -c '%u' "$check"); ancestor_mode=$(stat -c '%a' "$check"); fi case "$ancestor_uid" in 0|"$(id -u)") ;; *) echo 'Installation directory ancestors must belong to root or the current user.' >&2; exit 1;; esac case "$ancestor_mode" in 1???) ;; *[2367][0-7]|*[2367]) echo 'Installation directory has a replaceable ancestor.' >&2; exit 1;; esac parent=$(dirname "$check") [ "$parent" != "$check" ] || break check=$parent done [ ! -L "$bin/agentsocket" ] || { echo 'Refusing symlink installation target.' >&2; exit 1; } chmod 755 "$tmp/$name"; mv "$tmp/$name" "$bin/agentsocket" # This installer is explicitly requested to make the command available in future shells. case ":$PATH:" in *":$bin:"*) ;; *) escaped=$(printf '%s' "$bin" | sed "s/'/'\\\\''/g") line="export PATH='$escaped':\$PATH" for profile in "$HOME/.profile" "$HOME/.bashrc" "$HOME/.zshrc"; do if ! [ -f "$profile" ] || ! grep -F "$line" "$profile" >/dev/null 2>&1; then printf '\n# AgentSocket CLI\n%s\n' "$line" >> "$profile"; fi done ;; esac "$bin/agentsocket" --help >/dev/null printf 'AgentSocket %s installed. Open a new terminal, then run:\n agentsocket login\n agentsocket connect\nCurrent terminal: %s/agentsocket login\n' "$version" "$bin"