#!/bin/bash
# Note -^ /usr/bin/env isn't recognized from within "exec env -".

# This script allows aadl.bzl to use ctx.actions.run() in such a way that a
# unique file is generated as an output for each aadl_library() target.
#
# The first argument sent to this script is the path of the output file expected
# by bazel to exist if and only if the build command should succeed.

set -euo pipefail

bin_path="$1"
shift
out_filename="$(/bin/readlink -f "$1")"
files=$(/bin/readlink -f "$@")
cd "${bin_path}.runfiles/zoox"
../ocarina/bin/ocarina $files

# Thanks to pipefail, above, this won't execute if ocarina returns a non-zero
# exit status, causing the parent "bazel build" invocation to fail since
# no output file will have been created.
echo "" > "$out_filename"  # Note: "touch" isn't builtin, so doesn't work here.
