#!/bin/sh set -eu usage() { cat <<'EOF' Install the anycloud CLI. Usage: curl -fsSL https://get.anycloud.sh | sh sh install.sh [--version ] [--install-dir ] Options: --version Install a specific release, for example 0.1.30 or v0.1.30 --install-dir Install directory; defaults to ANYCLOUD_INSTALL_DIR or /usr/local/bin -h, --help Show this help EOF } INSTALL_DIR="${ANYCLOUD_INSTALL_DIR:-/usr/local/bin}" VERSION="" while [ "$#" -gt 0 ]; do case "$1" in --version) if [ "$#" -lt 2 ] || [ -z "$2" ]; then echo "Error: --version requires a value." >&2 exit 1 fi VERSION="${2#v}" shift 2 ;; --install-dir) if [ "$#" -lt 2 ] || [ -z "$2" ]; then echo "Error: --install-dir requires a value." >&2 exit 1 fi INSTALL_DIR="$2" shift 2 ;; -h|--help) usage exit 0 ;; *) echo "Error: unknown argument: $1" >&2 usage >&2 exit 1 ;; esac done # Detect OS and architecture OS=$(uname -s | tr '[:upper:]' '[:lower:]') ARCH=$(uname -m) case $ARCH in x86_64) ARCH="x64" ;; aarch64|arm64) ARCH="arm64" ;; *) echo "Unsupported architecture: $ARCH" exit 1 ;; esac PLATFORM="${OS}-${ARCH}" ASSET_NAME="anycloud-${PLATFORM}.tar.gz" RELEASES_BASE="https://github.com/anycloud-sh/releases/releases" if [ -n "$VERSION" ]; then RELEASE_URL="${RELEASES_BASE}/download/v${VERSION}" VERSION_LABEL="v${VERSION}" else RELEASE_URL="${RELEASES_BASE}/latest/download" VERSION_LABEL="latest" fi TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/anycloud-install.XXXXXX") cleanup() { rm -rf "$TMP_DIR" } trap cleanup EXIT verify_checksum() { if ! line=$(awk -v asset="$ASSET_NAME" '$2 == asset { print; found = 1 } END { exit found ? 0 : 1 }' "$TMP_DIR/checksums.txt"); then echo "Error: checksum for ${ASSET_NAME} not found in checksums.txt" >&2 exit 1 fi if command -v sha256sum >/dev/null 2>&1; then printf '%s\n' "$line" | (cd "$TMP_DIR" && sha256sum -c -) elif command -v shasum >/dev/null 2>&1; then printf '%s\n' "$line" | (cd "$TMP_DIR" && shasum -a 256 -c -) else echo "Error: sha256sum or shasum is required to verify the download." >&2 exit 1 fi } echo "Downloading anycloud ${VERSION_LABEL} for ${PLATFORM}..." if ! curl -fsSLo "$TMP_DIR/$ASSET_NAME" "$RELEASE_URL/$ASSET_NAME"; then echo "Error: Failed to download ${ASSET_NAME} from ${RELEASE_URL}" >&2 exit 1 fi if ! curl -fsSLo "$TMP_DIR/checksums.txt" "$RELEASE_URL/checksums.txt"; then echo "Error: Failed to download checksums.txt from ${RELEASE_URL}" >&2 exit 1 fi verify_checksum if ! tar -xzf "$TMP_DIR/$ASSET_NAME" -C "$TMP_DIR" anycloud; then echo "Error: Failed to extract anycloud from ${ASSET_NAME}" >&2 exit 1 fi chmod +x "$TMP_DIR/anycloud" # Install to INSTALL_DIR (may need sudo) if [ ! -d "$INSTALL_DIR" ]; then mkdir -p "$INSTALL_DIR" 2>/dev/null || sudo mkdir -p "$INSTALL_DIR" fi if [ -w "$INSTALL_DIR" ]; then mv "$TMP_DIR/anycloud" "$INSTALL_DIR/anycloud" else echo "Installing to $INSTALL_DIR (requires sudo)..." sudo mv "$TMP_DIR/anycloud" "$INSTALL_DIR/anycloud" fi echo "" echo "anycloud installed successfully to $INSTALL_DIR/anycloud" echo "" "$INSTALL_DIR/anycloud" --version