#!/usr/bin/env bash set -euo pipefail REPO="${HRUN_REPO:-ice5kysl/hrun}" INSTALL_DIR="${HRUN_INSTALL_DIR:-$HOME/.local/bin}" VERSION="${HRUN_VERSION:-latest}" info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } warn() { printf '\033[1;33mwarn:\033[0m %s\n' "$*" >&2; } fail() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; } detect_platform() { local os arch os="$(uname -s | tr '[:upper:]' '[:lower:]')" arch="$(uname -m)" case "$os" in darwin|linux) ;; *) fail "Unsupported OS: $os (supported: macOS, Linux)" ;; esac case "$arch" in x86_64|amd64) arch="amd64" ;; arm64|aarch64) arch="arm64" ;; *) fail "Unsupported architecture: $arch (supported: arm64, amd64)" ;; esac printf '%s-%s' "$os" "$arch" } mkdir -p "$INSTALL_DIR" # 1. If executed inside a local checkout of HRun, build directly from source. if [[ -f "./go.mod" && -f "./cmd/hrun/main.go" ]] && command -v go >/dev/null 2>&1; then info "Building hrun from local source..." go build -o "$INSTALL_DIR/hrun" ./cmd/hrun chmod 755 "$INSTALL_DIR/hrun" info "Installed: $INSTALL_DIR/hrun" "$INSTALL_DIR/hrun" --version || true exit 0 fi PLATFORM="$(detect_platform)" ASSET="hrun-${PLATFORM}" if [[ "$VERSION" == "latest" ]]; then DOWNLOAD_URL="https://github.com/${REPO}/releases/latest/download/${ASSET}" else DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${ASSET}" fi # 2. Try downloading prebuilt release binary first. TMP_BIN="$(mktemp "${TMPDIR:-/tmp}/hrun.XXXXXX")" trap 'rm -f "$TMP_BIN"' EXIT info "Downloading ${ASSET} from GitHub Releases (${VERSION})..." if curl -fsSL "$DOWNLOAD_URL" -o "$TMP_BIN" 2>/dev/null; then chmod 755 "$TMP_BIN" mv "$TMP_BIN" "$INSTALL_DIR/hrun" info "Installed: $INSTALL_DIR/hrun" else # 3. Fallback to building from source with Go if installed. warn "Prebuilt release binary unavailable; falling back to 'go install'..." if ! command -v go >/dev/null 2>&1; then fail "Could not download ${DOWNLOAD_URL} and Go is not installed. Please install Go 1.22+ or check https://github.com/${REPO}/releases" fi GOBIN="$INSTALL_DIR" go install "github.com/${REPO}/cmd/hrun@latest" info "Installed via go install: $INSTALL_DIR/hrun" fi case ":$PATH:" in *":$INSTALL_DIR:"*) ;; *) warn "$INSTALL_DIR is not in your PATH. Add this line to your ~/.zshrc or ~/.bashrc:" printf ' export PATH="%s:$PATH"\n' "$INSTALL_DIR" ;; esac info "Done! Run 'hrun web' to launch the local Web workspace, or 'hrun --help' for CLI usage."