#!/usr/bin/env bash set -euo pipefail TOOL_ROOT="$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd -P)" MCP_REPOSITORY="https://github.com/jasonjgardner/blockbench-mcp-plugin.git" MCP_COMMIT="6b069e308fdfc9b0a1c15bc924ca78150815f143" MCP_PATCH="${TOOL_ROOT}/patches/0001-projectcapture-hardening.patch" MCP_PATCH_SHA256="9de0b3767e96db7a68ef4c7082d7c76bf140b4932fc9550ad6e99d9a24a048c3" BUN_VERSION="1.3.14" require_command() { command -v "$1" >/dev/null 2>&1 || { printf 'Fehlendes Werkzeug: %s\n' "$1" >&2 exit 1 } } sha256_file() { if command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | awk '{print $1}' elif command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' else printf 'Weder shasum noch sha256sum ist verfügbar.\n' >&2 exit 1 fi } verify_patch() { local actual actual="$(sha256_file "${MCP_PATCH}")" if [[ "${actual}" != "${MCP_PATCH_SHA256}" ]]; then printf 'Patch-Hash stimmt nicht: erwartet %s, erhalten %s\n' \ "${MCP_PATCH_SHA256}" "${actual}" >&2 exit 1 fi } prepare_source() { local destination="$1" if [[ -z "${destination}" || -e "${destination}" ]]; then printf 'Temporäres Ziel muss neu und nicht vorhanden sein: %s\n' "${destination}" >&2 exit 1 fi verify_patch git clone --quiet --filter=blob:none --no-checkout "${MCP_REPOSITORY}" "${destination}" git -C "${destination}" checkout --quiet --detach "${MCP_COMMIT}" local resolved resolved="$(git -C "${destination}" rev-parse HEAD)" if [[ "${resolved}" != "${MCP_COMMIT}" ]]; then printf 'Upstream-Commit stimmt nicht: %s\n' "${resolved}" >&2 exit 1 fi git -C "${destination}" apply --check "${MCP_PATCH}" git -C "${destination}" apply "${MCP_PATCH}" git -C "${destination}" diff --check } make_stage_parent() { mktemp -d "${TMPDIR:-/tmp}/projectcapture-blockbench-mcp.XXXXXX" } safe_remove_stage() { local stage="$1" local leaf leaf="$(basename -- "${stage}")" if [[ -d "${stage}" && ! -L "${stage}" && "${leaf}" == projectcapture-blockbench-mcp.* ]]; then rm -rf -- "${stage}" else printf 'Unsicheres temporäres Löschziel wurde nicht entfernt: %s\n' "${stage}" >&2 fi } run_bun() { pnpm dlx "bun@${BUN_VERSION}" "$@" } write_dist_checksums() { local output_dir="$1" local checksum_file="${output_dir}/SHA256SUMS" local temporary_file="${checksum_file}.tmp.$$" local artifact : > "${temporary_file}" for artifact in \ hytale_plugin-0.9.1.js \ mcp-about.md \ mcp-icon.svg \ mcp-projectcapture-1.6.1.js; do if [[ -f "${output_dir}/${artifact}" && ! -L "${output_dir}/${artifact}" ]]; then printf '%s %s\n' "$(sha256_file "${output_dir}/${artifact}")" "${artifact}" \ >> "${temporary_file}" fi done mv -- "${temporary_file}" "${checksum_file}" }