#!/bin/bash # # Berserk CLI installer # Usage: curl -fsSL https://go.bzrk.dev | bash # curl -fsSL https://go.bzrk.dev | bash -s v1.0.15 set -e REPO="berserkdb/cli" BINARY_NAME="bzrk" INSTALL_DIR="${BZRK_INSTALL_DIR:-$HOME/.local/bin}" # Parse command line arguments VERSION="$1" # Optional: specific version (e.g., v1.0.15) if [[ -n "$VERSION" ]] && [[ ! "$VERSION" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[^[:space:]]+)?$ ]]; then echo "Usage: $0 [VERSION]" >&2 echo " Examples: $0 v1.0.15" >&2 echo " $0 (installs latest)" >&2 exit 1 fi # Detect platform case "$(uname -s)" in Darwin) echo "macOS is not currently supported. The Berserk CLI is only available for Linux." >&2 echo "See https://github.com/${REPO}/releases for available platforms." >&2 exit 1 ;; Linux) os="linux" ;; MINGW*|MSYS*|CYGWIN*) echo "Windows is not supported by this script." >&2 exit 1 ;; *) echo "Unsupported operating system: $(uname -s)" >&2 exit 1 ;; esac case "$(uname -m)" in x86_64|amd64) arch="x86_64" ;; arm64|aarch64) arch="aarch64" ;; *) echo "Unsupported architecture: $(uname -m)" >&2; exit 1 ;; esac # Check for required dependencies DOWNLOADER="" if command -v curl >/dev/null 2>&1; then DOWNLOADER="curl" elif command -v wget >/dev/null 2>&1; then DOWNLOADER="wget" else echo "Either curl or wget is required but neither is installed" >&2 exit 1 fi HAS_JQ=false if command -v jq >/dev/null 2>&1; then HAS_JQ=true fi download_file() { local url="$1" local output="$2" if [ "$DOWNLOADER" = "curl" ]; then if [ -n "$output" ]; then curl -fsSL -o "$output" "$url" else curl -fsSL "$url" fi elif [ "$DOWNLOADER" = "wget" ]; then if [ -n "$output" ]; then wget -q -O "$output" "$url" else wget -q -O - "$url" fi else return 1 fi } asset_name="${BINARY_NAME}-${os}-${arch}" # Resolve version if [ -z "$VERSION" ]; then echo "Fetching latest release..." release_url="https://api.github.com/repos/${REPO}/releases/latest" release_json=$(download_file "$release_url") if [ "$HAS_JQ" = true ]; then VERSION=$(echo "$release_json" | jq -r '.tag_name') download_url=$(echo "$release_json" | jq -r ".assets[] | select(.name == \"${asset_name}\") | .browser_download_url") else VERSION=$(echo "$release_json" | grep -o '"tag_name"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/') download_url="https://github.com/${REPO}/releases/download/${VERSION}/${asset_name}" fi else [[ "$VERSION" =~ ^v ]] || VERSION="v${VERSION}" download_url="https://github.com/${REPO}/releases/download/${VERSION}/${asset_name}" fi if [ -z "$VERSION" ] || [ "$VERSION" = "null" ]; then echo "Failed to determine latest version" >&2 exit 1 fi echo "Installing ${BINARY_NAME} ${VERSION} (${os}/${arch})..." # Download binary to temp dir tmp_dir=$(mktemp -d) trap 'rm -rf "$tmp_dir"' EXIT binary_path="${tmp_dir}/${BINARY_NAME}" if ! download_file "$download_url" "$binary_path"; then echo "Download failed. Asset '${asset_name}' may not exist for this platform." >&2 echo "Check available assets at: https://github.com/${REPO}/releases/tag/${VERSION}" >&2 exit 1 fi chmod +x "$binary_path" # Verify the binary runs if ! "$binary_path" --version >/dev/null 2>&1; then echo "Downloaded binary failed to execute. Your platform may not be supported yet." >&2 exit 1 fi # Install mkdir -p "$INSTALL_DIR" mv "$binary_path" "${INSTALL_DIR}/${BINARY_NAME}" echo "" echo "Installed ${BINARY_NAME} ${VERSION} to ${INSTALL_DIR}/${BINARY_NAME}" # Check if install dir is in PATH if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then echo "" echo "Add ${INSTALL_DIR} to your PATH:" shell_name=$(basename "${SHELL:-/bin/bash}") case "$shell_name" in zsh) echo " echo 'export PATH=\"${INSTALL_DIR}:\$PATH\"' >> ~/.zshrc && source ~/.zshrc" ;; bash) echo " echo 'export PATH=\"${INSTALL_DIR}:\$PATH\"' >> ~/.bashrc && source ~/.bashrc" ;; fish) echo " fish_add_path ${INSTALL_DIR}" ;; *) echo " echo 'export PATH=\"${INSTALL_DIR}:\$PATH\"' >> ~/.profile" ;; esac fi echo "" echo "Run '${BINARY_NAME} --help' to get started." echo ""