#!/usr/bin/env bash
set -euo pipefail

project_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
linux_root="$project_root/Linux"
jdk_home="$(bash "$project_root/scripts/bootstrap-linux-jdk.sh" | tail -n 1)"
bash "$project_root/scripts/bootstrap-dependencies.sh"
source "$project_root/scripts/drvfs-gradle-compat.sh"
configure_drvfs_gradle_compatibility "$linux_root" "$project_root"

export JAVA_HOME="$jdk_home"
export PATH="$JAVA_HOME/bin:$PATH"
export GRADLE_USER_HOME="$linux_root/.gradle-user"
export GRADLE_OPTS="-Xmx3g -Xms64m -Dfile.encoding=UTF-8 -Duser.country=US -Duser.language=en -Duser.variant"
mkdir -p "$linux_root/.kotlin/sessions"

bash "$project_root/application/gradlew" \
    -p "$project_root/application" \
    --no-daemon \
    --no-configuration-cache \
    --max-workers=8 \
    -Dorg.gradle.internal.instrumentation.agent=false \
    "-Dorg.gradle.jvmargs=-Xmx3g -Xms64m -Dfile.encoding=UTF-8" \
    --project-cache-dir "$linux_root/.gradle-project-cache" \
    "-Pmaptools.buildRoot=$linux_root/build" \
    "-Pkotlin.project.persistent.dir=$linux_root/.kotlin" \
    :core:desktopTest test :desktopApp:createDistributable
