#!/bin/sh set -eu DEFAULT_VERSION="0.2.0" DEFAULT_BASE_URL="https://github.com/edudar/agburn/releases/download" GITHUB_LATEST_API="https://api.github.com/repos/edudar/agburn/releases/latest" usage() { cat <<'EOF' Install agburn and its bundled ccusage helper. Usage: install.sh [--version ] [--dir ] Options: --version Release version to install (or "latest"). Overrides AGBURN_VERSION (default: 0.2.0). --dir Target bin directory. Overrides AGBURN_INSTALL_DIR (default: $HOME/.local/bin). -h, --help Show this help message. Environment: AGBURN_VERSION Version to install (default: 0.2.0; "latest" queries GitHub). AGBURN_INSTALL_DIR Destination directory (default: $HOME/.local/bin). AGBURN_BASE_URL Release base URL (default: GitHub releases download URL). EOF } fail() { echo "Error: $*" >&2 exit 1 } download_file() { dl_url="$1" dl_dest="$2" if command -v curl >/dev/null 2>&1; then curl -fsSL "$dl_url" -o "$dl_dest" elif command -v wget >/dev/null 2>&1; then wget -qO "$dl_dest" "$dl_url" else fail "curl or wget is required to download agburn." fi } compute_sha256() { hash_file="$1" if command -v shasum >/dev/null 2>&1; then shasum -a 256 "$hash_file" | awk '{print $1}' elif command -v sha256sum >/dev/null 2>&1; then sha256sum "$hash_file" | awk '{print $1}' else fail "shasum or sha256sum is required to verify downloads." fi } detect_target() { raw_os="$(uname -s)" case "$raw_os" in Darwin) os_name="macos" ;; Linux) os_name="linux" ;; *) fail "Unsupported operating system: $raw_os (supported: Darwin, Linux)." ;; esac raw_arch="$(uname -m)" case "$raw_arch" in arm64 | aarch64) arch_name="arm64" ;; x86_64 | amd64) arch_name="x64" ;; *) fail "Unsupported CPU architecture: $raw_arch (supported: arm64, aarch64, x86_64, amd64)." ;; esac printf '%s-%s\n' "$os_name" "$arch_name" } resolve_version() { requested="$1" meta_file="$2" if [ "$requested" = "latest" ]; then download_file "$GITHUB_LATEST_API" "$meta_file" resolved="$(sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$meta_file" | head -n 1)" resolved="${resolved#v}" if [ -z "$resolved" ]; then fail "Failed to resolve latest agburn version from $GITHUB_LATEST_API." fi printf '%s\n' "$resolved" return 0 fi printf '%s\n' "${requested#v}" } install_binary() { src_bin="$1" dest_dir="$2" bin_name="$3" tmp_bin="$dest_dir/.${bin_name}.tmp.$$" cp "$src_bin" "$tmp_bin" chmod +x "$tmp_bin" mv -f "$tmp_bin" "$dest_dir/$bin_name" } version="${AGBURN_VERSION:-$DEFAULT_VERSION}" install_dir="${AGBURN_INSTALL_DIR:-}" base_url="${AGBURN_BASE_URL:-$DEFAULT_BASE_URL}" while [ "$#" -gt 0 ]; do case "$1" in --version) [ "$#" -ge 2 ] || fail "--version requires a value." version="$2" shift 2 ;; --version=*) version="${1#--version=}" [ -n "$version" ] || fail "--version requires a non-empty value." shift ;; --dir) [ "$#" -ge 2 ] || fail "--dir requires a value." install_dir="$2" shift 2 ;; --dir=*) install_dir="${1#--dir=}" [ -n "$install_dir" ] || fail "--dir requires a non-empty value." shift ;; -h | --help) usage exit 0 ;; *) fail "Unknown argument: $1 (run with --help for usage)." ;; esac done if [ -z "$install_dir" ]; then [ -n "${HOME:-}" ] || fail "HOME is not set; pass --dir or set AGBURN_INSTALL_DIR." install_dir="$HOME/.local/bin" fi target="$(detect_target)" work_dir="$(mktemp -d)" cleanup() { if [ -n "${work_dir:-}" ]; then rm -rf -- "$work_dir" fi if [ -n "${install_dir:-}" ]; then rm -f -- "$install_dir/.agburn.tmp.$$" "$install_dir/.ccusage.tmp.$$" 2>/dev/null || true fi } trap cleanup EXIT INT TERM version="$(resolve_version "$version" "$work_dir/latest.json")" [ -n "$version" ] || fail "Resolved version is empty." base_url="${base_url%/}" case "$base_url" in */releases/download) release_url="$base_url/v$version" ;; *) release_url="$base_url" ;; esac archive_name="agburn-$version-$target.tar.gz" archive_path="$work_dir/$archive_name" sums_path="$work_dir/SHA256SUMS.txt" download_file "$release_url/$archive_name" "$archive_path" download_file "$release_url/SHA256SUMS.txt" "$sums_path" expected_sha256="$(awk -v name="$archive_name" ' $2 == name || $2 == ("*" name) { print $1; exit } ' "$sums_path" | tr 'A-F' 'a-f')" if [ -z "$expected_sha256" ]; then fail "Checksum entry for $archive_name not found in SHA256SUMS.txt." fi actual_sha256="$(compute_sha256 "$archive_path" | tr 'A-F' 'a-f')" if [ "$actual_sha256" != "$expected_sha256" ]; then fail "SHA-256 mismatch for $archive_name (expected $expected_sha256, got $actual_sha256)." fi stage_dir="$work_dir/stage" mkdir -p "$stage_dir" tar -xzf "$archive_path" -C "$stage_dir" [ -f "$stage_dir/agburn" ] || fail "Archive is missing agburn binary." [ -f "$stage_dir/ccusage" ] || fail "Archive is missing ccusage binary." mkdir -p "$install_dir" install_binary "$stage_dir/agburn" "$install_dir" "agburn" install_binary "$stage_dir/ccusage" "$install_dir" "ccusage" agburn_version="$("$install_dir/agburn" --version 2>/dev/null | head -n 1 || true)" if [ -z "$agburn_version" ]; then agburn_version="$version" else agburn_version="${agburn_version#agburn }" fi ccusage_version="$("$install_dir/ccusage" --version 2>/dev/null | head -n 1 || true)" ccusage_version="${ccusage_version#ccusage }" echo "Installed agburn $agburn_version to $install_dir/agburn" if [ -n "$ccusage_version" ]; then echo "Installed ccusage $ccusage_version to $install_dir/ccusage" else echo "Installed ccusage to $install_dir/ccusage" fi echo "To remove:" echo " rm -f \"$install_dir/agburn\" \"$install_dir/ccusage\""