#!/usr/bin/env sh set -eu repo="${MEGA_WALLET_CLI_REPO:-megaeth-labs/wallet-cli}" asset_prefix="${MEGA_WALLET_CLI_ASSET_PREFIX:-mega-wallet-cli}" version="${MEGA_WALLET_CLI_VERSION:-}" usage() { cat <<'USAGE' Usage: install [options] Bootstrap the MegaETH MOSS CLI installer from GitHub Releases. Options are forwarded to the release-owned installer. Common options: --version VERSION Release tag to install (default: latest release) --repo OWNER/REPO GitHub repo to fetch from (default: megaeth-labs/wallet-cli) --prefix DIR Prefix used when --bin-dir is omitted (default: ~/.local) --bin-dir DIR Directory for the mega wrapper (default: /bin) --install-root DIR Versioned install root (default: ~/.mega/wallet-cli) --no-skill Skip installing the bundled agent skill --skill-agent AGENT Skill target: codex, claude, hermes, openclaw, or all --dry-run Print actions without writing install files -h, --help Show installer help Examples: curl -fsSL https://account.megaeth.com/install | sh curl -fsSL https://account.megaeth.com/install | sh -- --version v0.1.4 USAGE } pending="" for arg in "$@"; do if [ -n "$pending" ]; then case "$pending" in repo) repo="$arg" ;; version) version="$arg" ;; esac pending="" continue fi case "$arg" in --repo) pending="repo" ;; --repo=*) repo="${arg#--repo=}" ;; --version) pending="version" ;; --version=*) version="${arg#--version=}" ;; -h|--help) usage exit 0 ;; esac done if [ -n "$pending" ]; then printf 'error: missing value for --%s\n' "$pending" >&2 exit 2 fi case "$repo" in */*) ;; *) printf 'error: --repo must be OWNER/REPO\n' >&2 exit 2 ;; esac error() { printf 'error: %s\n' "$1" >&2 exit 1 } need() { command -v "$1" >/dev/null 2>&1 || error "missing required command: $1" } github_api_get() { url="$1" if [ -n "${GITHUB_TOKEN:-}" ]; then curl -fsSL -H "Authorization: Bearer $GITHUB_TOKEN" "$url" else curl -fsSL "$url" fi } download() { url="$1" output="$2" if [ -n "${GITHUB_TOKEN:-}" ]; then curl -fL -H "Authorization: Bearer $GITHUB_TOKEN" "$url" -o "$output" else curl -fL "$url" -o "$output" fi } sha256_file() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' else shasum -a 256 "$1" | awk '{print $1}' fi } latest_version() { github_api_get "https://api.github.com/repos/$repo/releases/latest" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 } need curl if ! command -v sha256sum >/dev/null 2>&1 && ! command -v shasum >/dev/null 2>&1; then error "missing required command: sha256sum or shasum" fi if [ -z "$version" ]; then printf 'info: Resolving latest MegaETH MOSS CLI release for %s\n' "$repo" >&2 version="$(latest_version)" fi [ -n "$version" ] || error "could not resolve release version" case "$version" in */*|"") error "invalid release version: $version" ;; esac installer_asset="$asset_prefix-$version-install.sh" base_url="https://github.com/$repo/releases/download/$version" installer_url="$base_url/$installer_asset" checksum_url="$installer_url.sha256" tmp_dir="$(mktemp -d 2>/dev/null || mktemp -d -t mega-wallet-cli-install)" trap 'rm -rf "$tmp_dir"' EXIT installer_path="$tmp_dir/$installer_asset" checksum_path="$tmp_dir/$installer_asset.sha256" printf 'info: Downloading MegaETH MOSS CLI installer %s\n' "$version" >&2 download "$installer_url" "$installer_path" || error "failed to download $installer_url. This release may not include the release-owned installer asset yet." download "$checksum_url" "$checksum_path" || error "failed to download $checksum_url" expected_checksum="$(awk '{print $1}' "$checksum_path" | head -n 1)" actual_checksum="$(sha256_file "$installer_path")" [ "$expected_checksum" = "$actual_checksum" ] || error "checksum mismatch for $installer_asset" sh "$installer_path" "$@"