#!/bin/bash
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Script intended to be used on gen_fhir_protos rules.
# The only argument to this should be a gen_fhir_protos.
#
# e.g., for rule in //foo/bar/BUILD,
# gen_fhir_protos(
#   name = "quux",
#   ...
# ),
#
# The protos can be generated by running this from the root directory:
# bazel/generate_protos.sh foo/bar:quux
#
# This will generate foo/bar/quux.proto in the source directory

target=$1
tokens=(${target/:/ })
target_dir=${tokens[0]}
if [[ ${target_dir:0:2} != "//" ]]; then
  echo "Taget must be absolute and begin with //"
  exit 1
fi
dir=${target_dir#"//"}
label=${tokens[1]}

source $(dirname "$BASH_SOURCE")/generate_protos_utils.sh

# Build protos, and then copy them into source
try_build "$dir:${label}_proto_zip"

unzip_to_src "${label}"
