apply plugin: "com.android.library"
apply plugin: "kotlin-android"

apply from: "../scripts/publish-module.gradle"

android {
    buildToolsVersion = versions.buildTools
    compileSdkVersion versions.compileSdk
    ndkVersion versions.ndkVersion

    defaultConfig {
        minSdkVersion versions.minSdk
        targetSdkVersion versions.targetSdk
        versionCode versions.versionCode
        versionName versions.versionName
        setProperty("archivesBaseName", "godotopenxr.${versionName}")

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++14"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    namespace = "org.godotengine.plugin.vr.openxr"

    compileOptions {
        sourceCompatibility versions.javaVersion
        targetCompatibility versions.javaVersion
    }

    flavorDimensions "functionality"
    productFlavors {
        full {
            ndk {
                abiFilters "arm64-v8a", "armeabi-v7a"
            }
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=FULL"
                }
            }
        }
        stub {
            externalNativeBuild {
                cmake {
                    arguments "-DFLAVOR=STUB"
                }
            }
        }
    }

    publishing {
        singleVariant("fullRelease") {
            withSourcesJar()
            withJavadocJar()
        }

    singleVariant("stubRelease") {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

dependencies {
    implementation "io.github.m4gr3d:godot:${versions.godotVersion}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlinVersion}"
}

// When using the aar binary only, the *.gdns and *gdnlib files need to be in the same location
// relative to the `assets` directory, so we recreate the relative directories and copy them prior
// to building the aar binary.
final relativeAddonsConfigDir = "addons/godot-openxr/config/"
final sourceDir = "../demo/$relativeAddonsConfigDir"
final targetDir = "src/main/assets/$relativeAddonsConfigDir"

task copyAddonsConfig(type: Copy) {
    doFirst {
        delete(targetDir)
    }
    from sourceDir
    into targetDir
}

final soRootDir = "../thirdparty/oculus_mobile_sdk/OpenXR/Libs/Android"
final supportedBuildTypes = ["debug", "release"]
final supportedAbis = ["arm64-v8a", "armeabi-v7a"]

task copyOpenXrLoaderSo(type: Copy) {
    into("src")

    for (String targetBuildType : supportedBuildTypes) {
        for (String targetAbi : supportedAbis) {
            from("${soRootDir}/${targetAbi}/${targetBuildType.capitalize()}") {
                into("full${targetBuildType.capitalize()}/jniLibs/${targetAbi}")
            }
        }
    }
}

preBuild.dependsOn(copyAddonsConfig)
preBuild.dependsOn(copyOpenXrLoaderSo)
