String jmeBulletNativeProjectPath = '../jme3-bullet-native'

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 acces 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) {
    def zipFile = file(localZipFile)
    def outputDir = file(localUnzipPath)
//    println "unzipBullet zipFile = " + zipFile.absolutePath
//    println "unzipBullet outputDir = " + outputDir.absolutePath

    from zipTree(zipFile)
    into outputDir
}

unzipBullet.dependsOn {
    def zipFile = file(localZipFile)
//    println "zipFile path: " + zipFile.absolutePath
//    println "zipFile exists: " + zipFile.exists()

    if (!zipFile.exists()) {
        downloadBullet
    }
}

// Copy Bullet files to jni directory
task copyBullet(type: Copy) {
    def sourceDir = file(bulletSrcPath)
    def outputDir = new File(jniPath)
//    println "copyBullet sourceDir = " + sourceDir
//    println "copyBullet outputDir = " + outputDir

    from sourceDir
    into outputDir
}

copyBullet.dependsOn {
    def bulletUnzipDir = file(localZipFolder)
//    println "bulletUnzipDir: " + bulletUnzipDir.absolutePath
//    println "bulletUnzipDir exists: " + bulletUnzipDir.exists()
//    println "bulletUnzipDir isDirectory: " + bulletUnzipDir.isDirectory()
    if (!bulletUnzipDir.isDirectory()) {
        unzipBullet
    }
}

// Copy jME cpp native files to jni directory
task copyJmeCpp(type: Copy) {
    def sourceDir = new File(jmeCppPath)
    def outputDir = new File(jniPath)
//    println "copyJmeCpp sourceDir = " + sourceDir
//    println "copyJmeCpp outputDir = " + outputDir

    from sourceDir
    into outputDir
}

// Copy jME android native files to jni directory
task copyJmeAndroid(type: Copy) {
    def sourceDir = new File(jmeAndroidPath)
    def outputDir = new File(jniPath)
//    println "copyJmeAndroid sourceDir = " + sourceDir
//    println "copyJmeAndroid outputDir = " + outputDir

    from sourceDir
    into outputDir
}

task buildBulletNativeLib(type: Exec, dependsOn: [copyJmeAndroid, 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()
}

//task updatePreCompiledBulletLibs(type: Copy, dependsOn: generateNativeHeaders) {
task updatePreCompiledBulletLibs(type: Copy, dependsOn: buildBulletNativeLib) {
    def sourceDir = new File(ndkOutputPath)
    def outputDir = new File(bulletPreCompiledLibsDir)
//    println "updatePreCompiledBulletLibs sourceDir: " + sourceDir
//    println "updatePreCompiledBulletLibs outputDir: " + outputDir

    from sourceDir
    into outputDir
}

// Copy pre-compiled libs to build directory (when not building new libs)
task copyPreCompiledBulletLibs(type: Copy) {
    def sourceDir = new File(bulletPreCompiledLibsDir)
    def outputDir = new File(ndkOutputPath)
//    println "copyPreCompiledBulletLibs sourceDir: " + sourceDir
//    println "copyPreCompiledBulletLibs outputDir: " + outputDir

    from sourceDir
    into outputDir
}

// 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 dowload task
class MyDownload extends DefaultTask {
    @Input
    String sourceUrl

    @OutputFile
    File target

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