#!/bin/sh
set -eu

if [ "${LEMC_PROVIDER_SECRET+x}" = x ] || [ "${LEMC_PROFILE_OPERATION+x}" = x ]; then
  echo "the recipe received Provider-only input" >&2
  exit 78
fi

integration_dir=${LEMC_INTEGRATION_TUTORIAL_DISPOSABLE_PROVIDER_DIR:-}
if [ -z "$integration_dir" ] || [ ! -f "$integration_dir/verification.txt" ] ||
  [ -L "$integration_dir/verification.txt" ]; then
  echo "the recipe did not receive the fixed integration context" >&2
  exit 78
fi
IFS= read -r verification <"$integration_dir/verification.txt" || true
if [ "$verification" != "verified-by-provider" ]; then
  echo "the recipe received invalid integration context" >&2
  exit 78
fi

if ! printf '%s\n' "${LEMC_PROVIDER_RESULTS_YAML:-}" |
  grep -Fx 'tutorial-disposable-provider:' >/dev/null ||
  ! printf '%s\n' "${LEMC_PROVIDER_RESULTS_YAML:-}" |
    grep -Fx '    verified: true' >/dev/null; then
  echo "the recipe did not receive the validated Provider result" >&2
  exit 78
fi

printf '%s\n' 'lemc.output;integration context received; recipe did not receive the Secret'
