#!/bin/sh
# Provision the pinned Android build toolchain into TOOLCHAIN_DIR (default
# $HOME/toolchains): JDK 17, Android cmdline-tools, SDK platform, build-tools,
# NDK, and Gradle. Idempotent: re-running only fills what is missing. Writes an
# env.sh to source before building. This is what CI runs to get a build host.
set -eu

root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
. "$root/versions"

dir=${TOOLCHAIN_DIR:-$HOME/toolchains}
sdk="$dir/android-sdk"
mkdir -p "$dir/dl"
jdk="$dir/jdk-$JDK_RELEASE"
cmdline="$sdk/cmdline-tools/$ANDROID_CMDLINE_TOOLS"
goroot="$dir/go-$GO_VERSION"

download() {
	url=$1
	file=$2
	want=$3
	curl -fsSL "$url" -o "$file"
	printf '%s  %s\n' "$want" "$file" | sha256sum -c -
}

# JDK (Temurin, exact pinned release; + must be %2B in the URL).
if [ ! -x "$jdk/bin/java" ]; then
	echo "setup: JDK $JDK_RELEASE"
	rel=$(printf '%s' "$JDK_RELEASE" | sed 's/+/%2B/')
	download "https://api.adoptium.net/v3/binary/version/jdk-${rel}/linux/x64/jdk/hotspot/normal/eclipse" \
		"$dir/dl/jdk-$JDK_RELEASE.tar.gz" "$JDK_SHA256"
	mkdir -p "$jdk"
	tar xzf "$dir/dl/jdk-$JDK_RELEASE.tar.gz" -C "$jdk" --strip-components=1
fi
export JAVA_HOME="$jdk"
export PATH="$JAVA_HOME/bin:$PATH"

# Android cmdline-tools.
if [ ! -x "$cmdline/bin/sdkmanager" ]; then
	echo "setup: cmdline-tools $ANDROID_CMDLINE_TOOLS"
	download "https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_CMDLINE_TOOLS}_latest.zip" \
		"$dir/dl/clt-$ANDROID_CMDLINE_TOOLS.zip" "$ANDROID_CMDLINE_TOOLS_SHA256"
	rm -rf "$dir/dl/clt-$ANDROID_CMDLINE_TOOLS"
	python3 -m zipfile -e "$dir/dl/clt-$ANDROID_CMDLINE_TOOLS.zip" "$dir/dl/clt-$ANDROID_CMDLINE_TOOLS"
	mkdir -p "$sdk/cmdline-tools"
	rm -rf "$cmdline"
	mv "$dir/dl/clt-$ANDROID_CMDLINE_TOOLS/cmdline-tools" "$cmdline"
	chmod +x "$cmdline/bin/"*
fi

# SDK packages.
echo "setup: sdk packages"
yes | "$cmdline/bin/sdkmanager" --licenses >/dev/null
"$cmdline/bin/sdkmanager" \
	"platforms;android-${ANDROID_PLATFORM}" \
	"build-tools;${ANDROID_BUILD_TOOLS}" \
	"ndk;${ANDROID_NDK}" >/dev/null

# Gradle.
if [ ! -x "$dir/gradle-${GRADLE_VERSION}/bin/gradle" ]; then
	echo "setup: Gradle $GRADLE_VERSION"
	download "https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip" \
		"$dir/dl/gradle-$GRADLE_VERSION.zip" "$GRADLE_SHA256"
	python3 -m zipfile -e "$dir/dl/gradle-$GRADLE_VERSION.zip" "$dir"
	chmod +x "$dir/gradle-${GRADLE_VERSION}/bin/gradle"
fi

# Go (pinned; builds rsh).
if [ ! -x "$goroot/bin/go" ]; then
	echo "setup: Go $GO_VERSION"
	download "https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" \
		"$dir/dl/go-$GO_VERSION.tar.gz" "$GO_SHA256"
	mkdir -p "$goroot"
	tar xzf "$dir/dl/go-$GO_VERSION.tar.gz" -C "$goroot" --strip-components=1
fi

# Emit the env to source before building.
cat > "$dir/env.sh" <<EOF
export JAVA_HOME="$jdk"
export GOROOT="$goroot"
export ANDROID_HOME="$sdk"
export ANDROID_SDK_ROOT="$sdk"
export ANDROID_NDK_HOME="$sdk/ndk/${ANDROID_NDK}"
export GRADLE_HOME="$dir/gradle-${GRADLE_VERSION}"
export PATH="\$JAVA_HOME/bin:\$GOROOT/bin:\$GRADLE_HOME/bin:$cmdline/bin:$sdk/build-tools/${ANDROID_BUILD_TOOLS}:\$PATH"
EOF

echo "setup: done; source $dir/env.sh"
