#!/bin/sh
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

case "$0" in
	*/*)
		me=${0%/*}
		;;
	*)
		me=.
		;;
esac

. "$me"/gradle/wrapper/gradle-wrapper.properties

if ! [ -f "$me"/gradle-bin.zip ]; then
	curl -L -o "$me"/gradle-bin.zip "$distributionUrl"
fi

if [ x"$(sha256sum < "$me"/gradle-bin.zip)" != x"$distributionSha256Sum  -" ]; then
	rm -f "$me"/gradle-bin.zip
	echo >&2 "Checksum mismatch of gradle.zip!"
	exit 1
fi

mkdir -p "$me"/gradle-bin

if ! [ -f "$me"/gradle-bin/*/bin/gradle ]; then
	pwd=$(pwd)
	rm -rf "$me"/gradle-bin/*
	cd "$me"/gradle-bin
	unzip ../gradle-bin.zip
	cd "$pwd"
fi

exec "$me"/gradle-bin/*/bin/gradle "$@"
