#!/bin/sh

# jq filter to check that the wit-manifest contins a given commit (\1) and package (\2)
filter='.[]|select(.commit == "\1")|select(.name == "\2")'

# only check that all submodules have a corresponding wit package
# the wit-manifest.json may have packages that are not submoduled
# e.g. api-chisel3-sifive
git submodule status \
| sed -r "s/[ -+U]([a-zA-Z0-9]+) ([a-zA-Z0-9\-]+) ?.*/$filter/g" \
| xargs -d '\n' -I % sh -c "jq -e '%' wit-manifest.json"

if [ $? -eq 0 ]; then
  echo "Submodules are in sync with wit-manifest"
else
  echo "ERROR! Submodules are out of sync with wit-manifest!"
  exit 1
fi
