#!/usr/bin/env bash

# Run the script and capture the output
output=$(node scripts/format-changelog.js CHANGELOG.md)

# Check if the script ran successfully
if [ $? -eq 0 ]; then
    echo "Script ran successfully. Running diff..."
    # Run diff on the original file and the output
    git diff --color --no-index <(echo "$output") CHANGELOG.md
    # If diff finds differences, inform the user and exit with a non-zero status
    if [ $? -ne 0 ]; then
        echo -e "\nDifferences found. You can apply the changes locally with:"
        echo "  git diff --no-index <(node scripts/format-changelog.js CHANGELOG.md) CHANGELOG.md | git apply"
        exit 1
    else
        echo "No differences found. Exiting with status 0."
        exit 0
    fi
else
    echo "Script encountered an error:"
    echo "$output"
    exit 1
fi