#!/usr/bin/env bash set -euo pipefail for command in bun curl jq sha256sum; do command -v "$command" >/dev/null || { printf 'Required command not found: %s\n' "$command" >&2; exit 1; } done config="${OPENCODE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/opencode}" mkdir -p "$config" config=$(cd "$config" && pwd -P) mkdir -p "$config/stanp-opencode" "$config/plugins" install=$(mktemp -d "$config/stanp-opencode/install.XXXXXXXX") cleanup() { if [[ -n "$install" ]]; then rm -rf "$install"; fi } trap cleanup EXIT api='https://git.krtss.de/api/v1/repos/StanPonomarev/stanp-opencode/releases/latest' curl --fail-with-body --silent --show-error --location --proto '=https' --proto-redir '=https' "$api" -o "$install/release.json" for asset in stanp-opencode.tgz stanp-opencode.tgz.sha256; do url=$(jq -er --arg name "$asset" '[.assets[] | select(.name == $name)] | if length == 1 then .[0] | select((.id | type) == "number" and (.browser_download_url | type) == "string" and (.browser_download_url | startswith("https://"))) | "\(.browser_download_url)?asset_id=\(.id)" else error("Missing or duplicate release asset: " + $name) end' "$install/release.json") curl --fail-with-body --silent --show-error --location --proto '=https' --proto-redir '=https' "$url" -o "$install/$asset" done read -r checksum _ < "$install/stanp-opencode.tgz.sha256" if [[ ! "$checksum" =~ ^[a-fA-F0-9]{64}$ ]]; then printf 'Invalid release checksum\n' >&2 exit 1 fi hash_output=$(sha256sum "$install/stanp-opencode.tgz") read -r actual _ <<< "$hash_output" if [[ ! "$actual" =~ ^[a-fA-F0-9]{64}$ || "$actual" != "$checksum" ]]; then printf 'Release checksum mismatch; installation unchanged\n' >&2 exit 1 fi printf '{"private":true,"type":"module"}\n' > "$install/package.json" (cd "$install" && bun add --production --ignore-scripts --no-cache ./stanp-opencode.tgz) bun -e ' if (!await Bun.file(process.argv[1]).exists()) { throw new Error("Latest release lacks the OpenCode V2 directory entrypoint. Publish a V2-compatible release first."); } const { default: plugin } = await import(process.argv[1]); if (!plugin || plugin.id !== "stanp-opencode" || typeof plugin.setup !== "function") { throw new Error("Latest release is not a native OpenCode V2 plugin. Publish a V2-compatible release first."); } ' "$install/node_modules/stanp-opencode/index.js" tag=$(jq -r '.tag_name' "$install/release.json") ln -s "../stanp-opencode/$(basename "$install")/node_modules/stanp-opencode" "$install/plugin" mv -Tf "$install/plugin" "$config/plugins/stanp-opencode" install="" printf 'Installed stanp-opencode %s into %s/plugins/stanp-opencode\nRestart opencode2 to load it.\n' "$tag" "$config"