#!/usr/bin/env bash

set -euo pipefail

script="$(basename "${BASH_SOURCE[0]}")"
script_dir="$( (builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) )"

function usage() {
  cat <<EOF
Usage: $script <launch-file> <destination>

Syncs testdata targets from the given launch file to the specified destination
directory. Respects the directory structure of the testdata bucket.
EOF
}

if (( $# != 2 )); then
  usage >&2
  exit 1
elif [ "$1" == "-h" ] || [ "$1" == "--help" ] ||
     [ "$2" == "-h" ] || [ "$2" == "--help" ]; then
  usage
  exit 0
fi

script_dir="$( (builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) )"

launch="$1"
destination="$2"

get_targets="$script_dir/get_testdata_targets.py"
sync="$script_dir/s3_sync.py"
bucket="zoox-testdata"

for target in $($get_targets "$launch"); do
  full_destination="$destination/$target"
  $sync "$bucket/$target" "$(dirname "$full_destination")"
  echo "$full_destination"
done
