#!/usr/bin/env bash
set -xeuo pipefail

function main() {
  local imagery_database zrn zrn_imagery_version
  imagery_database=mapping/imagery/imagery.pbjson
  zrn="$1"
  zrn_imagery_version="$(mapping/zrn/tools/imagery_version --in="$zrn")"
  readonly imagery_database zrn imagery_version

  if [ -z "$zrn_imagery_version" ]; then
    echo Imagery name is not set in ZRN \'$zrn\' >&2
    exit 1
  fi

  if ! jq --raw-output '.imageries[].name' "$imagery_database" | \
      grep --quiet "$zrn_imagery_version"; then
    echo Imagery name \'$zrn_imagery_version\' not found in imagery database >&2
    exit 1
  fi
}

main "$@"
