#!/bin/sh set -e RELEASES_URL="https://clireleases.blacksmith.sh/cli" CHANNEL="${BLACKSMITH_CHANNEL:-latest}" # Detect OS OS="$(uname -s | tr '[:upper:]' '[:lower:]')" case "$OS" in linux|darwin) ;; *) echo "Error: unsupported OS: $OS" >&2 echo "Blacksmith CLI supports linux and darwin (macOS)." >&2 exit 1 ;; esac # Detect architecture ARCH="$(uname -m)" case "$ARCH" in x86_64|amd64) ARCH="amd64" ;; arm64|aarch64) ARCH="arm64" ;; *) echo "Error: unsupported architecture: $ARCH" >&2 echo "Blacksmith CLI supports amd64 and arm64." >&2 exit 1 ;; esac # Ensure rsync is available (required for testbox sync) if ! command -v rsync >/dev/null 2>&1; then echo "rsync not found, installing..." if [ "$OS" = "darwin" ]; then if command -v brew >/dev/null 2>&1; then brew install rsync || echo "Warning: failed to install rsync via Homebrew" else echo "Warning: rsync not found and Homebrew is not installed." echo "Install rsync manually: brew install rsync" fi else if command -v apt-get >/dev/null 2>&1; then sudo apt-get update -qq && sudo apt-get install -y -qq rsync || echo "Warning: failed to install rsync via apt-get" elif command -v dnf >/dev/null 2>&1; then sudo dnf install -y rsync || echo "Warning: failed to install rsync via dnf" elif command -v yum >/dev/null 2>&1; then sudo yum install -y rsync || echo "Warning: failed to install rsync via yum" elif command -v apk >/dev/null 2>&1; then sudo apk add --no-cache rsync || echo "Warning: failed to install rsync via apk" elif command -v pacman >/dev/null 2>&1; then sudo pacman -S --noconfirm rsync || echo "Warning: failed to install rsync via pacman" else echo "Warning: rsync not found and no supported package manager detected." echo "Install rsync manually for testbox sync to work." fi fi fi BINARY_URL="${RELEASES_URL}/${CHANNEL}/${OS}/${ARCH}/blacksmith" CHECKSUM_URL="${BINARY_URL}.sha256" INSTALL_TMPDIR="$(mktemp -d)" trap 'rm -rf "$INSTALL_TMPDIR"' EXIT echo "Installing Blacksmith CLI (${OS}/${ARCH}) from ${CHANNEL} channel..." # Download binary curl -fsSL "$BINARY_URL" -o "$INSTALL_TMPDIR/blacksmith" # Download and verify checksum curl -fsSL "$CHECKSUM_URL" -o "$INSTALL_TMPDIR/blacksmith.sha256" EXPECTED="$(awk '{print $1}' "$INSTALL_TMPDIR/blacksmith.sha256")" if command -v sha256sum >/dev/null 2>&1; then ACTUAL="$(sha256sum "$INSTALL_TMPDIR/blacksmith" | awk '{print $1}')" elif command -v shasum >/dev/null 2>&1; then ACTUAL="$(shasum -a 256 "$INSTALL_TMPDIR/blacksmith" | awk '{print $1}')" else echo "Error: no SHA-256 tool found (need sha256sum or shasum)" >&2 exit 1 fi if [ "$EXPECTED" != "$ACTUAL" ]; then echo "Error: checksum verification failed" >&2 echo " expected: $EXPECTED" >&2 echo " got: $ACTUAL" >&2 exit 1 fi chmod +x "$INSTALL_TMPDIR/blacksmith" # Determine install directory: # 1. BLACKSMITH_INSTALL_DIR if explicitly set # 2. ~/.local/bin if it exists (user-owned, auto-update friendly) # 3. /usr/local/bin as fallback (may require sudo, no auto-update) CUSTOM_DIR="" if [ -n "${BLACKSMITH_INSTALL_DIR}" ]; then INSTALL_DIR="${BLACKSMITH_INSTALL_DIR}" CUSTOM_DIR="1" elif [ -d "$HOME/.local/bin" ]; then INSTALL_DIR="$HOME/.local/bin" else INSTALL_DIR="/usr/local/bin" fi # Create install directory if it doesn't exist if [ ! -d "$INSTALL_DIR" ]; then mkdir -p "$INSTALL_DIR" 2>/dev/null || sudo mkdir -p "$INSTALL_DIR" fi if [ -w "$INSTALL_DIR" ]; then mv "$INSTALL_TMPDIR/blacksmith" "$INSTALL_DIR/blacksmith" else echo "Installing to $INSTALL_DIR (requires sudo)..." sudo mv "$INSTALL_TMPDIR/blacksmith" "$INSTALL_DIR/blacksmith" fi # Add ~/.local/bin to PATH in shell rc file if needed if [ "$INSTALL_DIR" = "$HOME/.local/bin" ] && [ -z "$CUSTOM_DIR" ]; then case ":$PATH:" in *":$HOME/.local/bin:"*) ;; *) case "$(basename "$SHELL")" in zsh) RC_FILE="$HOME/.zshrc" ;; bash) if [ "$(uname -s)" = "Darwin" ]; then RC_FILE="$HOME/.bash_profile" else RC_FILE="$HOME/.bashrc" fi ;; *) RC_FILE="" ;; esac if [ -n "$RC_FILE" ] && ! grep -q '.local/bin' "$RC_FILE" 2>/dev/null; then echo '' >> "$RC_FILE" echo '# Blacksmith CLI' >> "$RC_FILE" echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$RC_FILE" echo "" echo "Added $HOME/.local/bin to PATH in $RC_FILE" echo "Run 'source $RC_FILE' or open a new terminal to use blacksmith." fi ;; esac fi echo "" echo "Blacksmith CLI installed successfully to ${INSTALL_DIR}/blacksmith" "$INSTALL_DIR/blacksmith" --version 2>/dev/null || true echo "" echo "Get started:" echo " blacksmith auth login" echo " blacksmith testbox --help" echo "" if ! command -v gh >/dev/null 2>&1; then echo "Tip: Install the GitHub CLI (gh) for enhanced testbox status reporting." if [ "$OS" = "darwin" ]; then echo " brew install gh" elif command -v apt-get >/dev/null 2>&1; then echo " sudo apt update && sudo apt install gh -y" elif command -v dnf >/dev/null 2>&1; then echo " sudo dnf install gh" elif command -v pacman >/dev/null 2>&1; then echo " sudo pacman -S github-cli" elif command -v apk >/dev/null 2>&1; then echo " sudo apk add github-cli" else echo " https://cli.github.com" fi fi