String jmeBulletNativeProjectPath = project(":jme3-bullet-native").projectDir

String localUnzipPath = jmeBulletNativeProjectPath
String localZipFile = jmeBulletNativeProjectPath + File.separator + bulletZipFile
String localZipFolder = jmeBulletNativeProjectPath + File.separator + bulletFolder
String bulletSrcPath = localZipFolder + File.separator + 'src'

String jmeAndroidPath = 'src/native/android'
String jmeCppPath = jmeBulletNativeProjectPath + '/src/native/cpp'

//Working directories for the ndk build.
String ndkWorkingPath = "${buildDir}" + '/bullet'
String jniPath = ndkWorkingPath + '/jni'
String ndkOutputPath = ndkWorkingPath + '/libs'

//Pre-compiled libs directory
String bulletPreCompiledLibsDir = 'libs'

if (!hasProperty('mainClass')) {
    ext.mainClass = ''
}

dependencies {
    compile project(':jme3-bullet')
}

// Java source sets for IDE access and source jar bundling / mavenization
sourceSets {
    main {
        java {
            srcDir jmeCppPath
            srcDir jmeAndroidPath
        }
    }
}

// Download bullet if not available
task downloadBullet(type: MyDownload) {
    sourceUrl = bulletUrl
    target = file(localZipFile)
}

// Unzip bullet if not available
task unzipBullet(type: Copy) {
    from zipTree(localZipFile)
    into file(localUnzipPath)
}

unzipBullet.dependsOn {
    if (!file(localZipFile).exists()) {
        downloadBullet
    }
}

// Copy Bullet files to jni directory
task copyBullet(type: Copy) {
    from file(bulletSrcPath)
    into file(jniPath)
}

copyBullet.dependsOn {
    if (!file(localZipFolder).isDirectory()) {
        unzipBullet
    }
}

// Copy jME cpp native files to jni directory
task copyJmeCpp(type: Copy) {
    from file(jmeCppPath)
    into file(jniPath)
}

// Copy jME android native files to jni directory
task copyJmeAndroid(type: Copy) {
    from file(jmeAndroidPath)
    into file(jniPath)
}

task buildBulletNativeLib(type: Exec, dependsOn: [copyJmeAndroid, ':jme3-bullet:compileJava', copyJmeCpp, copyBullet]) {
//    args 'TARGET_PLATFORM=android-9'
//    println "buildBulletNativeLib ndkWorkingPath: " + ndkWorkingPath
//    println "buildBulletNativeLib rootProject.ndkCommandPath: " + rootProject.ndkCommandPath
    workingDir ndkWorkingPath
    executable rootProject.ndkCommandPath
    args "-j" + Runtime.runtime.availableProcessors()
}

/* The following two tasks: We store a prebuilt version in the repository, so nobody has to build
 * natives in order to build the engine. When building these natives however, the prebuilt libraries
 * can be updated (which is what the CI does). That's what the following two tasks do
 */

task updatePreCompiledBulletLibs(type: Copy, dependsOn: buildBulletNativeLib) {
    from file(ndkOutputPath)
    into file(bulletPreCompiledLibsDir)
}

// Copy pre-compiled libs to build directory (when not building new libs)
task copyPreCompiledBulletLibs(type: Copy) {
    from file(bulletPreCompiledLibsDir)
    into file(ndkOutputPath)
}

// ndkExists is a boolean from the build.gradle in the root project
// buildNativeProjects is a string set to "true"
if (ndkExists && buildNativeProjects == "true") {
    // build native libs and update stored pre-compiled libs to commit
    compileJava.dependsOn { updatePreCompiledBulletLibs }
} else {
    // use pre-compiled native libs (not building new ones)
    compileJava.dependsOn { copyPreCompiledBulletLibs }
}

jar.into("lib") { from ndkOutputPath }


// Helper class to wrap ant download task
class MyDownload extends DefaultTask {
    @Input
    String sourceUrl

    @OutputFile
    File target

    @TaskAction
    void download() {
       ant.get(src: sourceUrl, dest: target)
    }
}