#!/bin/bash -eu
# SPDX-FileCopyrightText: 2026 Uwe Fechner
# SPDX-License-Identifier: MIT

# Create a new release by posting to JuliaRegistrator via issue #13
#
# Usage:  bin/release
#
# This script extracts the latest release notes from CHANGELOG.md
# and posts them as a comment to GitHub issue #13 using the GitHub CLI.

set -euo pipefail

cd "$(dirname "$0")/.."

CHANGELOG="CHANGELOG.md"

if [ ! -f "$CHANGELOG" ]; then
    echo "Error: $CHANGELOG not found" >&2
    exit 1
fi

# Check that we are on the main branch
branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$branch" != "main" ]; then
    echo "Error: Must be on the 'main' branch (currently on '$branch')" >&2
    exit 1
fi

# Check that the working tree is clean (no staged, unstaged, or untracked changes)
if [ -n "$(git status --porcelain)" ]; then
    echo "Error: There are uncommitted or untracked changes. Please commit or stash them first." >&2
    exit 1
fi

# Check that the checked-in default manifests match the active ones, so a fresh
# install (which copies the .default over) resolves to exactly what was tested
for MANIFEST in "Manifest-v1.12.toml" "Manifest-v1.13.toml"; do
    MANIFEST_DEFAULT="${MANIFEST}.default"
    if [ ! -f "$MANIFEST" ] || [ ! -f "$MANIFEST_DEFAULT" ]; then
        echo "Error: $MANIFEST and/or $MANIFEST_DEFAULT not found." >&2
        exit 1
    fi
    if ! diff -q "$MANIFEST_DEFAULT" "$MANIFEST" > /dev/null; then
        echo "Error: $MANIFEST_DEFAULT does not match $MANIFEST." >&2
        echo "       Run 'cp $MANIFEST $MANIFEST_DEFAULT' and commit it, or reinstall to sync." >&2
        exit 1
    fi
done

# Extract the first release section: from "## KiteViewers v..." up to
# (but not including) the next "## KiteViewers" or end of file.
release_notes=$(awk '
    /^## KiteViewers / {
        if (version == "") {
            version = $2
            next
        }
        exit
    }
    version != "" { print }
' "$CHANGELOG")

if [ -z "$release_notes" ]; then
    echo "Error: Could not find release notes in $CHANGELOG" >&2
    exit 1
fi

# Get version string (e.g. "v0.6.0")
version=$(awk '/^## KiteViewers v/ {print $3; exit}' "$CHANGELOG")

# Check that the version in Project.toml matches the version in CHANGELOG.md
project_version=$(awk -F'"' '/^version = / {print $2; exit}' Project.toml)
changelog_version="${version#v}"
if [ "$project_version" != "$changelog_version" ]; then
    echo "Error: Version mismatch: Project.toml has '$project_version', CHANGELOG.md has '$changelog_version'" >&2
    exit 1
fi

echo "About to trigger registration for: KiteViewers $version"
echo ""
echo "Release notes:"
echo "$release_notes"
echo ""
read -r -p "Continue? [y/N] " response
if [ "$response" != "y" ] && [ "$response" != "Y" ]; then
    echo "Aborted."
    exit 0
fi

# Build the comment body and pipe it to gh
{
    echo '@JuliaRegistrator register'
    echo ""
    echo "Release notes:"
    echo ""
    echo "$release_notes"
} | gh issue comment 13 \
    --repo "OpenSourceAWE/KiteViewers.jl" \
    --body-file -

echo ""
echo "Done! Registration comment posted to https://github.com/OpenSourceAWE/KiteViewers.jl/issues/13"
