#!/bin/bash --norc
set -euo pipefail

name="$1"

backup="$(mktemp --suffix=.zrn)"
function onexit() {
  rm --force "$backup"
}
trap onexit EXIT

pbbin="vehicle/zrn/$name.zrn"
pbtxt="vehicle/zrn/$name.zrn.pbtxt"

echo Creating backup file: $backup
cp "$pbbin" "$backup"

echo Updating $pbbin...
bazel run //mapping/zrn/tools:import_export -- --in="//$pbbin"
bazel run //mapping/zrn/release -- --in="//$pbbin" || true

echo Comparing backup to $pbbin:
bazel run //mapping/zrn/tools:diff_zrn -- --a="$backup" --b="//$pbbin" || true

echo Updating $pbtxt...
bazel run //mapping/zrn/tools:zrn_to_pbtxt -- --in=//$pbbin --out=//$pbtxt
