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

zrn_file="$1"
zrn_root="$(echo "$zrn_file" | sed --expression='s#\.zrn$##')"

source_zrn_pb="$zrn_root.zrn"
source_zrn_pbtxt="$zrn_root.zrn.pbtxt"
release_zrn="$(mktemp --suffix=.zrn.release)"

function on_exit() {
  rm "$release_zrn"
}
trap on_exit EXIT

echo Updating postprocess manifest in $zrn_file

# Produce a release ZRN (zrn::pb::ReleaseZrn) by invoking the postprocess
# command-line tool.
mapping/zrn/postprocess/postprocess_release \
  -in="$source_zrn_pb" \
  -out="$release_zrn"

# Extract the source ZRN (zrn::pb::ZooxRoadNetwork) from the release ZRN.
mapping/zrn/tools/release_to_source -in="$release_zrn" -out="$source_zrn_pb"

# Convert the binary source ZRN to text.
mapping/zrn/tools/zrn_to_pbtxt -in="$source_zrn_pb" -out="$source_zrn_pbtxt"
