#!/bin/bash
#
# Check that license files are up to date.
# This script regenerates the license files and compares them with the committed versions.
# If there are differences, it exits with an error.

set -e

# Store original files for comparison
TEMPDIR="$(mktemp -d)"
trap "rm -fr ${TEMPDIR}" EXIT

# Save original license markdown files
for goos in darwin linux windows; do
    cp "third-party-licenses.${goos}.md" "${TEMPDIR}/"
done

# Save the state of third-party directory
cp -r third-party "${TEMPDIR}/third-party.orig"

# Regenerate using the same script
./script/licenses

# Check for any differences in workspace
if ! git diff --exit-code --quiet third-party-licenses.*.md third-party/; then
    echo "License files are out of date:"
    git diff third-party-licenses.*.md third-party/
    echo ""
    printf "\nLicense check failed.\n\nPlease update the license files by running \`./script/licenses\` and committing the output.\n"
    exit 1
fi

echo "License check passed for all platforms."

