#!/bin/sh -e

# Based on the input, figure out if we're going to run ethr 0.x or
# 1.x, run that and provide the input.

WHEREAMI=$(dirname "$0")

TMPBASE=$(mktemp -d)
cleanup()
{
    rm -rf "${TMPBASE}"
}
trap cleanup EXIT

INPUT="${TMPBASE}/input"
cat > "${INPUT}"

# Older versions of the plugin don't put ethr_major_version in the
# participant data.  Count the number of participants that didn't.

NUM_VZEROS=$(jq '
      ."participant-data" as $pd
    | ($pd | length) as $len
    | [ $pd[]
        | select(.ethr_major_version < 1)
      ]
    | length
' "${INPUT}")

case "${NUM_VZEROS}" in
    0)
	# Everybody's 1.x or later.
	TO_RUN=run-ethr1
	;;
    *)
	# At least one participant is 0.x.
	TO_RUN=run-ethr0
	;;
esac

cat "${INPUT}" | "${WHEREAMI}/${TO_RUN}"
