#!/usr/bin/env bash
# Build a p2 update site for the thoughtframe-eclipse plugin.
# Install in Eclipse via:  Help > Install New Software > Add > file:/<repo>  (or a hosted URL).
#
# Usage: eclipse/make-update-site.sh [output-dir]        (default: build/dist/site)
#   VERSION=1.2.3   fixed version  (default: <manifest base>.<UTC yyyyMMddHHmm> - so every
#                   build is a newer, DISTINCT version and Eclipse offers it as an upgrade).
# Requires a local Eclipse launcher (override with ECLIPSE=/path/to/eclipse).
set -euo pipefail

here="$(cd "$(dirname "$0")" && pwd)"     # .../eclipse
root="$(cd "$here/.." && pwd)"            # repo root
out="${1:-$root/build/dist/site}"
eclipse="${ECLIPSE:-/home/ian/eclipse/jee-2026-06/eclipse/eclipse}"

base="$(grep -E '^Bundle-Version:' "$here/META-INF/MANIFEST.MF" | awk '{print $2}')"
base="${base%.qualifier}"
version="${VERSION:-$base.$(date -u +%Y%m%d%H%M)}"
bundle="thoughtframe-eclipse_${version}"
feature="org.thoughtframe.eclipse.feature_${version}"
lander="$root/thoughtframe/webapp/WEB-INF/base/thoughtframe/eclipse/index.html"

echo "version: $version"

stage="$(mktemp -d)"
trap 'rm -rf "$stage"' EXIT
pub="$stage/publish"
# FeaturesAndBundlesPublisher reads bundles from publish/plugins and features from publish/features.
mkdir -p "$pub/plugins/$bundle" "$pub/features/$feature"

echo "staging bundle + feature"
cp -r "$here/META-INF" "$here/plugin.xml" "$pub/plugins/$bundle/"
cp -r "$here/bin/." "$pub/plugins/$bundle/"
cp -r "$here/lib" "$here/engine" "$here/webapp" "$pub/plugins/$bundle/"
# runtime node data is machine state - never publish it
rm -rf "$pub/plugins/$bundle/webapp/WEB-INF/opensearch" "$pub/plugins/$bundle/webapp/WEB-INF/elastic"
sed -i "s/^Bundle-Version:.*/Bundle-Version: ${version}/" "$pub/plugins/$bundle/META-INF/MANIFEST.MF"
# Stamp the resolved version into the feature: both the feature element and the referenced
# plugin - but NOT the XML declaration's version="1.0".
sed -E '/^<\?xml/! s/version="[^"]*"/version="'"$version"'"/g' "$root/feature/feature.xml" > "$pub/features/$feature/feature.xml"
cp "$root/feature/build.properties" "$pub/features/$feature/"

if [ ! -x "$eclipse" ]; then
  echo "no Eclipse launcher at $eclipse - set ECLIPSE=/path/to/eclipse" >&2
  exit 1
fi

echo "publishing p2 repo -> $out"
rm -rf "$out"; mkdir -p "$out"
"$eclipse" -nosplash -consoleLog -data "$stage/ws" \
  -application org.eclipse.equinox.p2.publisher.FeaturesAndBundlesPublisher \
  -metadataRepository "file:$out" \
  -artifactRepository "file:$out" \
  -categoryDefinition "$root/feature/category.xml" \
  -source "$pub" \
  -compress \
  -publishArtifacts

# The bundle ships as a normal JAR. p2 unpacks it on install because the manifest declares
# Eclipse-BundleShape: dir (see eclipse/META-INF/MANIFEST.MF) - that is what makes engine/
# and webapp/ real directories, which EngineIsland needs (FileLocator.toFileURL). A *folder*
# artifact is the p2 bundle-pool convention, local only; a remote repo serves the jar.

# --- lander + version breadcrumb ----------------------------------------------------
cp "$lander" "$out/index.html"
printf '\n<p class="tf-build">Latest build: <code>%s</code></p>\n' "$version" >> "$out/index.html"
printf '%s\n' "$version" > "$out/version.txt"

# A p2.index tells the client exactly which factories to use, so it reads content.jar /
# artifacts.jar directly instead of probing (and mis-detecting) the repository type - which
# otherwise makes the Collect phase fail with "No repository found containing ...".
cat > "$out/p2.index" <<'IDX'
version=1
metadata.repository.factory.order=content.jar,\!
artifact.repository.factory.order=artifacts.jar,\!
IDX

echo; echo "p2 site: $out  (version ${version})"
ls -1 "$out"
