#!/bin/sh
# Compile tests/testthat/fixtures/proto/*.proto into the descriptor sets the
# tests load, with protoc. Run from the package root after changing a
# .proto there; commit the .binpb files it writes.
#
# None uses --include_imports except all.binpb, so a set holds only its own
# file and its imports must already be in the pool (the well-known types
# always are). The tests join sets to build reversed-order and
# missing-import cases.
set -eu

P=tests/testthat/fixtures/proto
OUT=tests/testthat/fixtures
command -v protoc >/dev/null || { echo "protoc is needed" >&2; exit 1; }

for f in schema legacy editions dep uses_dep decode decode2; do
  protoc --proto_path="$P" --descriptor_set_out="$OUT/$f.binpb" "$P/$f.proto"
done
protoc --proto_path="$P/v2" --descriptor_set_out="$OUT/dep_v2.binpb" "$P/v2/dep.proto"
protoc --proto_path="$P" --include_imports --descriptor_set_out="$OUT/all.binpb" \
  "$P/schema.proto" "$P/legacy.proto" "$P/editions.proto" "$P/uses_dep.proto"

# Provenance of every set in fixtures/, as msg/MANIFEST.tsv records the
# messages'. cycle and unresolved come from tools/make-fixtures.py: protoc
# refuses to write them.
{
  printf 'name\tsource\tsha256\n'
  for f in "$OUT"/*.binpb; do
    name=$(basename "$f" .binpb)
    case "$name" in
    cycle | unresolved) source="tools/make-fixtures.py, descriptor_pb2" ;;
    *) source="$(protoc --version)" ;;
    esac
    printf '%s\t%s\t%s\n' "$name" "$source" "$(shasum -a 256 "$f" | cut -d' ' -f1)"
  done
} > "$OUT/DESCRIPTORS.tsv"

echo "==> wrote $(ls "$OUT"/*.binpb | wc -l | tr -d ' ') descriptor sets with $(protoc --version)"
