#!/usr/bin/env bash set -euo pipefail REPO="yunyingk/hose-use" echo "==> Detecting platform and architecture..." OS="$(uname -s | tr '[:upper:]' '[:lower:]')" ARCH="$(uname -m)" case "$OS" in darwin) PLATFORM="darwin" ;; linux) PLATFORM="linux" ;; *) echo "Unsupported operating system: $OS" >&2 exit 1 ;; esac case "$ARCH" in x86_64|amd64) ARCH_NAME="x64" ;; arm64|aarch64) ARCH_NAME="arm64" ;; *) echo "Unsupported architecture: $ARCH" >&2 exit 1 ;; esac # Resolve best writable bin directory without requiring sudo TARGET_DIR="${HOSE_USE_INSTALL_DIR:-}" if [ -n "$TARGET_DIR" ]; then mkdir -p "$TARGET_DIR"; fi for candidate in "/opt/homebrew/bin" "/usr/local/bin" "$HOME/.local/bin" "$HOME/bin"; do if [ -z "$TARGET_DIR" ] && [ -d "$candidate" ] && [ -w "$candidate" ]; then TARGET_DIR="$candidate" break fi done if [ -z "$TARGET_DIR" ]; then mkdir -p "$HOME/.local/bin" TARGET_DIR="$HOME/.local/bin" fi ASSET_NAME="hose-use-${PLATFORM}-${ARCH_NAME}" TARGET_PATH="${TARGET_DIR}/hose-use" if [ -d "$TARGET_PATH" ] || { [ -d "${TARGET_DIR}/hose-aipro" ] && [ ! -L "${TARGET_DIR}/hose-aipro" ]; }; then echo "Error: an install target is a directory; refusing to replace it." >&2 exit 1 fi get_file_size() { local file="$1" if [ ! -f "$file" ]; then echo "0" return fi local sz sz=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null || wc -c < "$file" | tr -d ' ') echo "$sz" } format_bytes() { local bytes="$1" if [ -z "$bytes" ] || [ "$bytes" -le 0 ]; then echo "0 B" return fi if [ "$bytes" -ge 1048576 ]; then awk "BEGIN {printf \"%.2f MB (%'d bytes)\", $bytes / 1048576, $bytes}" 2>/dev/null || awk "BEGIN {printf \"%.2f MB (%d bytes)\", $bytes / 1048576, $bytes}" elif [ "$bytes" -ge 1024 ]; then awk "BEGIN {printf \"%.2f KB (%d bytes)\", $bytes / 1024, $bytes}" else echo "${bytes} bytes" fi } extract_host() { local url="$1" echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||' } download_candidate() { local url="$1" local dest="$2" local label="$3" local host host="$(extract_host "$url")" echo "==> Downloading from ${label}" echo " Source Host: ${host}" echo " Source URL: ${url}" # Test connectivity & headers first if ! curl -fIs --connect-timeout 4 "$url" >/dev/null 2>&1; then echo " ↳ Source unreachable or asset not ready (404), falling back to next mirror..." return 1 fi # Download with progress bar if curl -fL --progress-bar "$url" -o "$dest"; then local size_bytes size_bytes="$(get_file_size "$dest")" if [ "$size_bytes" -gt 0 ]; then echo "✓ Download complete! Total size: $(format_bytes "$size_bytes")" echo "✓ Successfully fetched from host: ${host}" return 0 fi fi rm -f "$dest" return 1 } echo "==> Target install location: ${TARGET_PATH}" TMP_DIR="$(mktemp -d)" TMP_FILE="${TMP_DIR}/download" CHECKSUM_FILE="${TMP_DIR}/checksums.txt" INSTALL_STAGE="" trap 'rm -rf "$TMP_DIR"; if [ -n "$INSTALL_STAGE" ]; then rm -f "$INSTALL_STAGE"; fi' EXIT DOWNLOAD_SUCCESS=false DOWNLOADED_FORMAT="archive" if ! command -v curl >/dev/null 2>&1; then echo "Error: curl is required." >&2 exit 1 fi if command -v sha256sum >/dev/null 2>&1; then hash_file() { sha256sum "$1" | awk '{ print tolower($1) }'; } elif command -v shasum >/dev/null 2>&1; then hash_file() { shasum -a 256 "$1" | awk '{ print tolower($1) }'; } else echo "Error: SHA-256 verification requires sha256sum or shasum." >&2 exit 1 fi MIRROR="${HOSE_USE_MIRROR:-auto}" case "$MIRROR" in auto|github|gitea) ;; *) echo "Invalid HOSE_USE_MIRROR: $MIRROR" >&2; exit 1 ;; esac # Resolve latest Gitea tag if available. Resolve once, then pin every mirror and # checksum request to that exact version instead of guessing an old release. GITEA_API_URL="https://git.ying-qing.cn/api/v1/repos/gitea/hose-use/releases/latest" GITHUB_API_URL="https://api.github.com/repos/${REPO}/releases/latest" LATEST_TAG="${HOSE_USE_VERSION:-}" if [ -n "$LATEST_TAG" ]; then LATEST_TAG="v${LATEST_TAG#v}" else api_urls=() if [ "$MIRROR" != "github" ]; then api_urls+=("$GITEA_API_URL"); fi if [ "$MIRROR" != "gitea" ]; then api_urls+=("$GITHUB_API_URL"); fi for api_url in "${api_urls[@]}"; do if release_json=$(curl -fsSL --connect-timeout 4 --max-time 15 "$api_url"); then LATEST_TAG=$(printf '%s' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^" ]*\)".*/\1/p' | head -n 1) if [ -n "$LATEST_TAG" ]; then break; fi fi done fi if ! [[ "$LATEST_TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.-]+)?$ ]]; then echo "Error: could not resolve a release version; set HOSE_USE_VERSION explicitly." >&2 exit 1 fi release_bases=() if [ "$MIRROR" != "github" ]; then release_bases+=("https://git.ying-qing.cn/gitea/hose-use/releases/download/${LATEST_TAG}"); fi if [ "$MIRROR" != "gitea" ]; then release_bases+=("https://github.com/${REPO}/releases/download/${LATEST_TAG}") release_bases+=("https://ghfast.top/https://github.com/${REPO}/releases/download/${LATEST_TAG}") fi verified_download() { local base="$1" asset="$2" expected actual if ! download_candidate "${base}/checksums.txt" "$CHECKSUM_FILE" "Release checksums"; then echo " ↳ Missing checksums.txt for ${LATEST_TAG}; refusing an unverified install." >&2 return 1 fi expected=$(awk -v name="$asset" '{ sub(/\r$/, "") } $2 == name || $2 == "*" name { count++; if (length($1) == 64 && $1 ~ /^[0-9a-fA-F]+$/) value=tolower($1); else invalid=1 } END { if (count == 1 && !invalid) print value; else exit 1 }' "$CHECKSUM_FILE") || { echo " ↳ Missing or ambiguous checksum for ${asset}." >&2 return 1 } if ! download_candidate "${base}/${asset}" "$TMP_FILE" "Release ${LATEST_TAG}"; then return 1; fi actual=$(hash_file "$TMP_FILE") if [ "$actual" != "$expected" ]; then echo " ↳ SHA-256 mismatch for ${asset}; refusing this download." >&2 return 1 fi echo "✓ SHA-256 verified: ${asset}" } # 1. First priority: Compressed Archives (.tar.gz) - fast download & includes aliases # 2. Second priority: Raw Executable (Fallback), with the same checksum requirement. for format in archive raw; do asset="$ASSET_NAME" if [ "$format" = "archive" ]; then asset="${asset}.tar.gz"; fi for base in "${release_bases[@]}"; do if verified_download "$base" "$asset"; then DOWNLOAD_SUCCESS=true DOWNLOADED_FORMAT="$format" break fi done if [ "$DOWNLOAD_SUCCESS" = "true" ]; then break; fi done if [ "$DOWNLOAD_SUCCESS" = "false" ]; then echo "Error: Failed to obtain a verified ${ASSET_NAME} for ${LATEST_TAG} from all candidate mirrors." >&2 exit 1 fi if [ "$DOWNLOADED_FORMAT" = "archive" ]; then echo "==> Extracting selected binary in temporary staging..." member="" listing=$(tar -tzf "$TMP_FILE") for candidate in "$ASSET_NAME" "./$ASSET_NAME" hose-use ./hose-use; do if printf '%s\n' "$listing" | awk -v name="$candidate" '$0 == name { count++ } END { exit count != 1 }'; then member="$candidate" break fi done if [ -z "$member" ]; then echo "Error: release archive has no expected platform binary." >&2; exit 1; fi # Stream only the exact member; archive paths and aliases never get written to bin. tar -xOzf "$TMP_FILE" "$member" > "${TMP_DIR}/hose-use" if [ ! -s "${TMP_DIR}/hose-use" ]; then echo "Error: release binary is empty." >&2; exit 1; fi else mv "$TMP_FILE" "${TMP_DIR}/hose-use" fi # Stage on the target filesystem so the final rename is atomic. INSTALL_STAGE=$(mktemp "${TARGET_DIR}/.hose-use-install.XXXXXX") cp "${TMP_DIR}/hose-use" "$INSTALL_STAGE" chmod 755 "$INSTALL_STAGE" # Ad-hoc sign on macOS if [ "$PLATFORM" = "darwin" ] && command -v codesign >/dev/null 2>&1; then echo "==> Applying macOS code signature..." codesign --force --sign - "$INSTALL_STAGE" 2>/dev/null || true fi mv -f "$INSTALL_STAGE" "$TARGET_PATH" INSTALL_STAGE="" # Create hose-aipro multicall alias pointing at the single binary. ln -sfn hose-use "${TARGET_DIR}/hose-aipro" echo "==> Initializing agent skill to ~/.agents/skills/hose-use..." if "$TARGET_PATH" skill install --yes; then echo "✓ Agent skill installed to ~/.agents/skills/hose-use" else echo "Error: binary installed to ${TARGET_PATH}, but Agent Skill installation failed. Retry: hose-use skill install --yes" >&2 exit 1 fi echo "" echo "✨ hose-use successfully installed to ${TARGET_PATH}!" "$TARGET_PATH" version echo "" echo "Quick start:" echo " 1. Login: hose-use auth login --phone=177... --password=..." echo " 2. List corps: hose-use corps list" echo " 3. List workflows: hose-use workflow list" echo " 4. Switch mode: hose-use config mode stateless" echo " 5. Self-update: hose-use update" echo " AI PRO alias: hose-aipro (默认未激活)" echo " AI PRO 申请凭证: 把版本号与期望到期日发给服务方,拿到凭证后直接粘贴给你的 AI 助手" echo " AI PRO 激活: 由 AI 助手执行,无需手动敲命令" echo ""