#!/bin/bash
# Find builds here:
# https://ci.android.com/builds/branches/aosp-main/grid
set -e

# Change directory to cuttlefish_prebuilts
# (assumes the script is at cuttlefish_prebuilts/scripts/update-trusty)
cd "$(dirname $(dirname "$(realpath $0)"))"

if [ $# == 1 ]
then
build=$1
else
	echo "Usage: $0 <build>"
	exit 1
fi

download_trusty_file()
{
  local target=$1
  local elf_file=$2

  local ci_url="https://ci.android.com/builds/submitted/${build}/${target}/latest/raw"
  local out_file="trusty/$elf_file"
  echo "Downloading $elf_file..."
  curl -fL "$ci_url/$elf_file" -o $out_file

  echo "Adding $elf_file to current branch"
  git add $out_file
}

mkdir -p trusty

download_trusty_file "trusty_generic_x86_64" "generic-x86_64.lk.elf"
download_trusty_file "trusty_generic_x86_64_test" "generic-x86_64-test.lk.elf"

echo "Committing build $build..."
git commit -m "Update Trusty prebuilts to $build

Test: Presubmit"

topic="trusty_cf_prebuilts_$build"
echo "Uploading topic $topic"
repo upload -c . --topic=$topic
