#!/bin/bash -ev

# Updates prebuilts/jdk/jdk25 from build server. (ab/openjdk)
# Usage:
#    $ cd <REPO>
#    $ repo start jdk25_update prebuilts/jdk/jdk25
#    $ prebuilts/jdk/jdk25/update.sh <BUILD_NUMBER>
#    $ repo upload --cbr prebuilts/jdk/jdk25


BUILD_NUMBER="${1:?Specify build number}"
LINUX_ZIP=/tmp/$$.linux.zip

FETCH_ARTIFACT=/google/data/ro/projects/android/fetch_artifact
cd "$(dirname $0)"

rm -rf staging
mkdir -p staging

$FETCH_ARTIFACT --bid ${BUILD_NUMBER} --disable_progressbar --target linux_openjdk25 jdk.zip ${LINUX_ZIP}
$FETCH_ARTIFACT --bid ${BUILD_NUMBER} --disable_progressbar --target linux_openjdk25 manifest_${BUILD_NUMBER}.xml staging/manifest.xml

rm -rf staging/linux-x86
mkdir -p staging/linux-x86
(cd staging/linux-x86; unzip -q ${LINUX_ZIP})
touch staging/linux-x86/MODULE_LICENSE_GPL

rm -f ${LINUX_ZIP}

# Copy the RBE toolchain inputs files from the previous version to the staging
# directory.
cp linux-x86/bin/*_remote_toolchain_inputs staging/linux-x86/bin

version=$(staging/linux-x86/bin/java -version 2>&1 | grep "OpenJDK Runtime Environment" | sed -e 's/.*(\(.*\))/\1/')

rm -rf linux-x86 manifest.xml
mv staging/* .
rmdir staging
git add linux-x86 manifest.xml
git commit -m "Update to JDK $version" --edit
