// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    apply from: 'config.gradle'

    repositories {
        google()
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
        maven { url "https://s01.oss.sonatype.org/content/repositories/snapshots/"}
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$versions.gradlePluginVersion"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlinVersion"
    }
}

plugins {}

apply from: 'config.gradle'

task clean(type: Delete) {
    delete rootProject.buildDir

    // Delete the bin directory for the 'godot_meta_toolkit' addon
    delete("demo/addons/godot_meta_toolkit/.bin")

    dependsOn ':toolkit:clean'
}

/**
 * Build the 'godot_meta_toolkit'
 */
task buildToolkit {
    // Generate the editor gdextension binaries
    dependsOn ':buildSconsArtifacts'

    // Generate the AAR binaries
    dependsOn ':toolkit:build'
}

/**
 * Build the scons artifacts for the project
 */
task buildSconsArtifacts {
    // Find scons' executable path
    File sconsExecutableFile = null
    def sconsName = "scons"
    def sconsExts = (org.gradle.internal.os.OperatingSystem.current().isWindows()
            ? [".bat", ".cmd", ".ps1", ".exe"]
            : [""])
    logger.debug("Looking for $sconsName executable path")
    for (ext in sconsExts) {
        String sconsNameExt = sconsName + ext
        logger.debug("Checking $sconsNameExt")
        sconsExecutableFile = org.gradle.internal.os.OperatingSystem.current().findInPath(sconsNameExt)
        if (sconsExecutableFile != null) {
            // We're done!
            break
        }
        // Check all the options in path
        List<File> allOptions = org.gradle.internal.os.OperatingSystem.current().findAllInPath(sconsNameExt)
        if (!allOptions.isEmpty()) {
            // Pick the first option and we're done!
            sconsExecutableFile = allOptions.get(0)
            break
        }
    }

    // Using `doFirst` so the exception doesn't happen until this task actually runs.
    doFirst {
        if (sconsExecutableFile == null) {
            throw new GradleException("Unable to find executable path for the '$sconsName' command.")
        } else {
            logger.debug("Found executable path for $sconsName: ${sconsExecutableFile.absolutePath}")
        }
    }

    if (sconsExecutableFile != null) {
        // Build the GDExtension library for Android.
        tasks.create(name: "buildGodotMetaToolkitAndroidArm64Debug", type: Exec) {
            executable sconsExecutableFile.absolutePath
            args "--directory=.", "platform=android", "target=template_debug", "arch=arm64", "build_profile=thirdparty/godot_cpp_build_profile/build_profile.json"
        }
        tasks.create(name: "buildGodotMetaToolkitAndroidArm64Release", type: Exec) {
            executable sconsExecutableFile.absolutePath
            args "--directory=.", "platform=android", "target=template_release", "arch=arm64", "build_profile=thirdparty/godot_cpp_build_profile/build_profile.json"
        }

        dependsOn 'buildGodotMetaToolkitAndroidArm64Debug'
        dependsOn 'buildGodotMetaToolkitAndroidArm64Release'

        // Build the GDExtension library for desktop.
        tasks.create(name: "buildGodotMetaToolkitDesktopDebug", type: Exec) {
            executable sconsExecutableFile.absolutePath
            args "--directory=.", "target=template_debug", "build_profile=thirdparty/godot_cpp_build_profile/build_profile.json"
        }
        tasks.create(name: "buildGodotMetaToolkitDesktopRelease", type: Exec) {
            executable sconsExecutableFile.absolutePath
            args "--directory=.", "target=template_release", "build_profile=thirdparty/godot_cpp_build_profile/build_profile.json"
        }

        dependsOn 'buildGodotMetaToolkitDesktopDebug'
        dependsOn 'buildGodotMetaToolkitDesktopRelease'
    }
}