#!/usr/bin/env sh # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at https://mozilla.org/MPL/2.0/. # # install.sh — One-command installer for uProot + uproot-distro (Termux & Linux). # Downloads prebuilt release binaries when available (zero compiler required), # or compiles cleanly from source when passed --build inside a git checkout. set -eu SCRIPT_DIR="" if [ -n "${0:-}" ] && [ -f "$0" ]; then SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)" fi if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then C_RESET="$(printf '\033[0m')" C_BOLD="$(printf '\033[1m')" C_GREEN="$(printf '\033[32m')" C_CYAN="$(printf '\033[36m')" C_YELLOW="$(printf '\033[33m')" C_RED="$(printf '\033[31m')" else C_RESET="" C_BOLD="" C_GREEN="" C_CYAN="" C_YELLOW="" C_RED="" fi info() { printf '%s==>%s %s\n' "${C_CYAN}${C_BOLD}" "${C_RESET}" "$*"; } ok() { printf '%s[ok]%s %s\n' "${C_GREEN}${C_BOLD}" "${C_RESET}" "$*"; } warn() { printf '%s[warn]%s %s\n' "${C_YELLOW}${C_BOLD}" "${C_RESET}" "$*" >&2; } die() { printf '%s[error]%s %s\n' "${C_RED}${C_BOLD}" "${C_RESET}" "$*" >&2; exit 1; } # Auto-detect sensible install prefix if [ -n "${PREFIX:-}" ] && [ -d "${PREFIX}/bin" ]; then INSTALL_PREFIX="${PREFIX}" elif [ "$(id -u)" -eq 0 ]; then INSTALL_PREFIX="/usr/local" else INSTALL_PREFIX="${HOME}/.local" fi REPO="${UPROOT_REPO:-wahyuzero/uproot}" RELEASE_TAG="${UPROOT_VERSION:-latest}" FORCE_BUILD=0 UNINSTALL=0 usage() { cat < Install prefix (default: ${INSTALL_PREFIX}) --repo GitHub repo for prebuilt releases (default: ${REPO}) --version Release tag to download (default: latest) --build Compile from source instead of downloading prebuilt release --uninstall Remove installed 'uproot' and 'uproot-distro' from /bin -h, --help Show this help text EOF } while [ $# -gt 0 ]; do case "$1" in --prefix) [ $# -ge 2 ] || die "Missing argument for --prefix" INSTALL_PREFIX="$2"; shift 2 ;; --prefix=*) INSTALL_PREFIX="${1#*=}"; shift ;; --repo) [ $# -ge 2 ] || die "Missing argument for --repo" REPO="$2"; shift 2 ;; --repo=*) REPO="${1#*=}"; shift ;; --version|--tag) [ $# -ge 2 ] || die "Missing argument for $1" RELEASE_TAG="$2"; shift 2 ;; --version=*|--tag=*) RELEASE_TAG="${1#*=}"; shift ;; --build) FORCE_BUILD=1; shift ;; --uninstall) UNINSTALL=1; shift ;; -h|--help) usage; exit 0 ;; *) die "Unknown option: $1 (try 'install.sh --help')" ;; esac done BIN_DIR="${INSTALL_PREFIX}/bin" if [ "$UNINSTALL" -eq 1 ]; then info "Uninstalling uproot and uproot-distro from ${BIN_DIR}..." rm -f "${BIN_DIR}/uproot" "${BIN_DIR}/uproot-distro" ok "Uninstalled from ${BIN_DIR}." exit 0 fi detect_arch() { m="$(uname -m)" case "$m" in aarch64|arm64) printf 'aarch64\n' ;; x86_64|amd64) printf 'x86_64\n' ;; armv7l|armv8l|armhf|arm) printf 'armv7l\n' ;; *) die "Unsupported CPU architecture: ${m} (supported: aarch64, x86_64, armv7l)" ;; esac } in_repo_checkout=0 if [ -n "$SCRIPT_DIR" ] && [ -f "${SCRIPT_DIR}/Makefile" ] && [ -d "${SCRIPT_DIR}/src" ] && [ -f "${SCRIPT_DIR}/scripts/uproot-distro" ]; then in_repo_checkout=1 fi fetch_url() { url="$1" out="$2" if command -v curl >/dev/null 2>&1; then curl -fsSL --retry 2 "$url" -o "$out" return $? elif command -v wget >/dev/null 2>&1; then wget -qO "$out" "$url" return $? fi return 127 } verify_sha256() { sums_file="$1" asset_name="$2" target_file="$3" [ -f "$sums_file" ] || return 0 expected="$(awk -v name="$asset_name" '$2 == name || $2 == ("./" name) { print $1; exit }' "$sums_file")" [ -n "$expected" ] || return 0 actual="" if command -v sha256sum >/dev/null 2>&1; then actual="$(sha256sum "$target_file" | awk '{ print $1 }')" elif command -v shasum >/dev/null 2>&1; then actual="$(shasum -a 256 "$target_file" | awk '{ print $1 }')" elif command -v python3 >/dev/null 2>&1; then actual="$(python3 -c 'import hashlib,sys; print(hashlib.sha256(open(sys.argv[1],"rb").read()).hexdigest())' "$target_file")" else warn "No sha256sum tool found; skipping checksum verification for ${asset_name}." return 0 fi if [ "$actual" != "$expected" ]; then die "SHA-256 mismatch for ${asset_name} (expected ${expected}, got ${actual})" fi ok "Verified SHA-256 for ${asset_name}." } build_from_source() { [ "$in_repo_checkout" -eq 1 ] || \ die "Source build requires a repository checkout. Clone https://github.com/${REPO}.git and run ./install.sh --build" info "Checking build toolchain (clang, lld, make)..." missing="" for tool in clang ld.lld make; do if ! command -v "$tool" >/dev/null 2>&1; then missing="${missing} ${tool}" fi done if [ -n "$missing" ]; then if [ -n "${PREFIX:-}" ] && command -v pkg >/dev/null 2>&1; then die "Missing tools:${missing}. Install in Termux with: pkg install clang lld make" else die "Missing tools:${missing}. Install clang, lld, and make first." fi fi info "Building uProot static binary..." if [ "$FORCE_BUILD" -eq 1 ]; then (cd "$SCRIPT_DIR" && make clean && make) else (cd "$SCRIPT_DIR" && make) fi } ARCH="$(detect_arch)" TMP_STAGE="" cleanup() { if [ -n "${TMP_STAGE:-}" ] && [ -d "$TMP_STAGE" ]; then rm -rf "$TMP_STAGE" fi } trap cleanup EXIT INT TERM SRC_UPROOT="" SRC_DISTRO="" if [ "$FORCE_BUILD" -eq 1 ]; then build_from_source SRC_UPROOT="${SCRIPT_DIR}/uproot" SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" elif [ "$in_repo_checkout" -eq 1 ] && [ -x "${SCRIPT_DIR}/uproot" ]; then info "Using existing built binary at ${SCRIPT_DIR}/uproot (pass --build to recompile)." SRC_UPROOT="${SCRIPT_DIR}/uproot" SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" elif [ "$in_repo_checkout" -eq 1 ] && [ -x "${SCRIPT_DIR}/prebuilt/${ARCH}/uproot" ]; then info "Using prebuilt binary at ${SCRIPT_DIR}/prebuilt/${ARCH}/uproot." SRC_UPROOT="${SCRIPT_DIR}/prebuilt/${ARCH}/uproot" SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" else if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then if [ "$in_repo_checkout" -eq 1 ]; then warn "Neither curl nor wget found; falling back to source build." build_from_source SRC_UPROOT="${SCRIPT_DIR}/uproot" SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" else die "curl or wget is required to download prebuilt uProot releases." fi else TMP_STAGE="$(mktemp -d "${TMPDIR:-/tmp}/uproot_install.XXXXXX")" if [ -n "${UPROOT_RELEASE_BASE_URL:-}" ]; then REL_BASE="${UPROOT_RELEASE_BASE_URL%/}" elif [ "$RELEASE_TAG" = "latest" ]; then REL_BASE="https://github.com/${REPO}/releases/latest/download" else REL_BASE="https://github.com/${REPO}/releases/download/${RELEASE_TAG}" fi BIN_ASSET="uproot-linux-${ARCH}" info "Downloading prebuilt ${BIN_ASSET} (${RELEASE_TAG}) from ${REPO}..." if fetch_url "${REL_BASE}/${BIN_ASSET}" "${TMP_STAGE}/uproot"; then (fetch_url "${REL_BASE}/SHA256SUMS" "${TMP_STAGE}/SHA256SUMS" >/dev/null 2>&1 || true) verify_sha256 "${TMP_STAGE}/SHA256SUMS" "$BIN_ASSET" "${TMP_STAGE}/uproot" chmod 0755 "${TMP_STAGE}/uproot" SRC_UPROOT="${TMP_STAGE}/uproot" if [ "$in_repo_checkout" -eq 1 ]; then SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" elif fetch_url "${REL_BASE}/uproot-distro" "${TMP_STAGE}/uproot-distro"; then verify_sha256 "${TMP_STAGE}/SHA256SUMS" "uproot-distro" "${TMP_STAGE}/uproot-distro" chmod 0755 "${TMP_STAGE}/uproot-distro" SRC_DISTRO="${TMP_STAGE}/uproot-distro" else RAW_REF="$RELEASE_TAG" [ "$RAW_REF" = "latest" ] && RAW_REF="main" RAW_URL="https://raw.githubusercontent.com/${REPO}/${RAW_REF}/scripts/uproot-distro" fetch_url "$RAW_URL" "${TMP_STAGE}/uproot-distro" || \ die "Failed to download uproot-distro from ${REL_BASE} or ${RAW_URL}" chmod 0755 "${TMP_STAGE}/uproot-distro" SRC_DISTRO="${TMP_STAGE}/uproot-distro" fi elif [ "$in_repo_checkout" -eq 1 ]; then warn "Prebuilt release not found at ${REL_BASE}; building from source..." build_from_source SRC_UPROOT="${SCRIPT_DIR}/uproot" SRC_DISTRO="${SCRIPT_DIR}/scripts/uproot-distro" else die "Could not download ${REL_BASE}/${BIN_ASSET}. If no release is published yet, clone the repo and run ./install.sh --build" fi fi fi mkdir -p "$BIN_DIR" info "Installing binaries to ${BIN_DIR}..." cp -f "$SRC_UPROOT" "${BIN_DIR}/uproot" cp -f "$SRC_DISTRO" "${BIN_DIR}/uproot-distro" chmod 0755 "${BIN_DIR}/uproot" "${BIN_DIR}/uproot-distro" VER="$("${BIN_DIR}/uproot" --version 2>/dev/null || printf 'uproot')" ok "Installed: ${VER}" ok "Installed: ${BIN_DIR}/uproot-distro" case ":${PATH:-}:" in *":${BIN_DIR}:"*) ;; *) warn "${BIN_DIR} is not in your PATH yet." printf ' Add to your shell profile: export PATH="%s:$PATH"\n' "$BIN_DIR" ;; esac cat <