#!/usr/bin/env bash # Copyright 2025 Octelium Labs, LLC. All rights reserved. MIT license. # # Usage: # curl -sSL https://... | bash # VERSION=v1.2.3 bash install.sh # install a specific version # INSTALL_DIR=~/.local/bin bash install.sh # custom install directory # USE_SUDO=false bash install.sh # skip sudo set -euo pipefail : "${USE_SUDO:="true"}" : "${DEBUG:="false"}" : "${INSTALL_DIR:="/usr/local/bin"}" : "${VERSION:=""}" DOWNLOAD_REPO="https://github.com/octelium/cordium/releases" LATEST_RELEASE_URL="https://api.github.com/repos/octelium/cordium/releases/latest" GITHUB_API_VERSION="2026-03-10" BINARIES=("cordium") HAS_CURL="$(type "curl" &>/dev/null && echo true || echo false)" HAS_WGET="$(type "wget" &>/dev/null && echo true || echo false)" HAS_TAR="$(type "tar" &>/dev/null && echo true || echo false)" HAS_SHA256SUM="$(type "sha256sum" &>/dev/null && echo true || echo false)" HAS_SHASUM="$(type "shasum" &>/dev/null && echo true || echo false)" TMP_ROOT="$(mktemp -d 2>/dev/null || mktemp -d -t 'cordium-install-XXXXXX')" cleanup() { [[ -d "${TMP_ROOT:-}" ]] && rm -rf "$TMP_ROOT" } trap cleanup EXIT INT TERM log() { echo "$*"; } warn() { echo " WARNING: $*" >&2; } die() { printf ' ERROR: %b\n' "$*" >&2; exit 1; } runAsRoot() { if [[ "$EUID" -ne 0 && "$USE_SUDO" == "true" ]]; then sudo "${@}" else "${@}" fi } initArch() { ARCH=$(uname -m) case "$ARCH" in armv5*) ARCH="armv5" ;; armv6*) ARCH="armv6" ;; armv7*) ARCH="arm" ;; aarch64|arm64) ARCH="arm64" ;; x86_64) ARCH="amd64" ;; x86|i686|i386) ARCH="386" ;; *) die "Unsupported architecture: $ARCH" ;; esac } initOS() { OS=$(uname | tr '[:upper:]' '[:lower:]') case "$OS" in mingw*) OS="windows" ;; esac } verifySupported() { case "$OS" in darwin|linux) ;; *) die "No prebuilt binary for ${OS}-${ARCH}. Build from source: https://github.com/octelium/cordium" ;; esac case "$ARCH" in amd64|arm64) ;; *) die "No prebuilt binary for ${OS}-${ARCH}. Build from source: https://github.com/octelium/cordium" ;; esac [[ "$HAS_CURL" == "true" || "$HAS_WGET" == "true" ]] \ || die "curl or wget is required." [[ "$HAS_TAR" == "true" ]] \ || die "tar is required to extract the binaries." [[ "$HAS_SHA256SUM" == "true" || "$HAS_SHASUM" == "true" ]] \ || warn "sha256sum / shasum not found — skipping checksum verification." } download() { local url="$1" dest="$2" if [[ "$HAS_CURL" == "true" ]]; then curl -fsSL "$url" -o "$dest" else wget -qO "$dest" "$url" fi } fetchLatestTag() { local response if [[ "$HAS_CURL" == "true" ]]; then response="$( curl -fsSL \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: $GITHUB_API_VERSION" \ "$LATEST_RELEASE_URL" )" || return 1 else response="$( wget -qO- \ --header="Accept: application/vnd.github+json" \ --header="X-GitHub-Api-Version: $GITHUB_API_VERSION" \ "$LATEST_RELEASE_URL" )" || return 1 fi printf '%s\n' "$response" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 } resolveTag() { if [[ -n "$VERSION" ]]; then TAG="$VERSION" log "Using requested version: $TAG" else TAG="$(fetchLatestTag || true)" [[ -n "$TAG" ]] || die "Failed to fetch the latest release tag." fi [[ "$TAG" == v* ]] || die "Invalid release tag '$TAG': expected a tag beginning with 'v'." DOWNLOAD_URL_PREFIX="$DOWNLOAD_REPO/download/$TAG" } verifyChecksum() { local filename="$1" local file="$TMP_ROOT/$filename" local checksums_file="$TMP_ROOT/SHA256SUMS" if [[ ! -f "$checksums_file" ]]; then local checksums_url="$DOWNLOAD_URL_PREFIX/SHA256SUMS" if ! download "$checksums_url" "$checksums_file"; then warn "Could not download checksums file — skipping verification for $filename." return 0 fi fi local expected expected=$(grep -E "(^|[[:space:]])${filename}($|[[:space:]])" "$checksums_file" | awk '{print $1}' || true) if [[ -z "$expected" ]]; then warn "No checksum entry found for $filename — skipping verification." return 0 fi local actual if [[ "$HAS_SHA256SUM" == "true" ]]; then actual=$(sha256sum "$file" | awk '{print $1}') elif [[ "$HAS_SHASUM" == "true" ]]; then actual=$(shasum -a 256 "$file" | awk '{print $1}') else warn "No checksum tool available — skipping verification for $filename." return 0 fi if [[ "$actual" != "$expected" ]]; then die "Checksum mismatch for $filename!\n expected: $expected\n got: $actual" fi } downloadAndInstall() { local bin_name="$1" local filename="${bin_name}-${OS}-${ARCH}.tar.gz" local file_url="$DOWNLOAD_URL_PREFIX/$filename" local file="$TMP_ROOT/$filename" log "Downloading $file_url" if ! download "$file_url" "$file"; then die "Failed to download $file_url" fi [[ -s "$file" ]] || die "Downloaded file is empty: $file" verifyChecksum "$filename" tar xf "$file" -C "$TMP_ROOT" --no-same-owner [[ -f "$TMP_ROOT/$bin_name" ]] || die "Binary '$bin_name' not found in archive." runAsRoot cp "$TMP_ROOT/$bin_name" "$INSTALL_DIR/$bin_name" runAsRoot chmod +x "$INSTALL_DIR/$bin_name" } installCompletions() { local bin="$1" local bin_path="$INSTALL_DIR/$bin" if ! "$bin_path" --help &>/dev/null; then warn " Binary failed to execute — skipping completions for $bin." return 0 fi if type "bash" &>/dev/null; then local bash_comp_dir="" if [[ "$OS" == "darwin" ]] && type "brew" &>/dev/null; then bash_comp_dir="$(brew --prefix)/etc/bash_completion.d" elif [[ "$OS" == "linux" ]]; then bash_comp_dir="/usr/share/bash-completion/completions" fi if [[ -n "$bash_comp_dir" && -d "$bash_comp_dir" ]]; then "$bin_path" completion bash > "$TMP_ROOT/${bin}.bash" runAsRoot cp "$TMP_ROOT/${bin}.bash" "$bash_comp_dir/$bin" fi fi if type "zsh" &>/dev/null; then local zsh_comp_dir="/usr/local/share/zsh/site-functions" if [[ "$OS" == "darwin" ]] && type "brew" &>/dev/null; then zsh_comp_dir="$(brew --prefix)/share/zsh/site-functions" fi if [[ -d "$zsh_comp_dir" ]]; then "$bin_path" completion zsh > "$TMP_ROOT/_${bin}" runAsRoot cp "$TMP_ROOT/_${bin}" "$zsh_comp_dir/_$bin" fi fi if type "fish" &>/dev/null; then local fish_comp_dir="/usr/share/fish/vendor_completions.d" if [[ "$OS" == "darwin" ]] && type "brew" &>/dev/null; then fish_comp_dir="$(brew --prefix)/share/fish/vendor_completions.d" fi if [[ -d "$fish_comp_dir" ]]; then "$bin_path" completion fish > "$TMP_ROOT/${bin}.fish" runAsRoot cp "$TMP_ROOT/${bin}.fish" "$fish_comp_dir/${bin}.fish" fi fi } [[ "$DEBUG" == "true" ]] && set -x initArch initOS verifySupported resolveTag if [[ ! -d "$INSTALL_DIR" ]]; then runAsRoot mkdir -p "$INSTALL_DIR" fi for cli in "${BINARIES[@]}"; do downloadAndInstall "$cli" installCompletions "$cli" done echo "" log "Cordium CLI has been successfully installed" log " Docs: https://octelium.com/docs/cordium" log " Repo: https://github.com/octelium/cordium"