apply plugin: 'cpp'

String bulletSrcPath = bulletFolder + '/src'

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

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

// C++ sources for binary compilation
sources {
    bulletjme {
        cpp {
            source {
                srcDir 'src/native/cpp'
                srcDir bulletSrcPath
                exclude 'BulletMultiThreaded/GpuSoftBodySolvers/**'
                include '**/*.cpp'
            }
            exportedHeaders {
                srcDir 'src/native/cpp'
                srcDir bulletSrcPath
                include '**/*.h'
            }
        }
    }
}

// Java source sets for IDE acces and source jar bundling / mavenization
sourceSets {
    main {
        java {
            srcDir 'src/native/cpp'
        }
    }
}

// Set of target platforms, will be available based on build system
model {
    platforms{
//    osx_universal { // TODO: universal binary doesn't work?
//        architecture 'x86_64'
//        architecture 'x86'
//        operatingSystem 'osx'
//    }
        osx_x86 {
            architecture "x86"
            operatingSystem "osx"
        }
        osx_x64 {
            architecture "x86_64"
            operatingSystem "osx"
        }
        linux_x86 {
            architecture "x86"
            operatingSystem "linux"
        }
        linux_x86_64 {
            architecture "x86_64"
            operatingSystem "linux"
        }
        windows_x86 {
            architecture "x86"
            operatingSystem "windows"
        }
        windows_x86_64 {
            architecture "x86_64"
            operatingSystem "windows"
        }
    }
}

// Defines created C++ libraries
libraries {
    bulletjme {
    }
    all {
        binaries.all {
            cppCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include"
            if (targetPlatform.operatingSystem.name == "osx") {
                cppCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/darwin"
            } else if (targetPlatform.operatingSystem.name == "linux") {
                cppCompiler.args '-I', "${org.gradle.internal.jvm.Jvm.current().javaHome}/include/linux"
                cppCompiler.args "-fPIC"
                cppCompiler.args "-fpermissive"
                cppCompiler.args "-static-libgcc"
                cppCompiler.args "-static-libstdc++"
                linker.args "-static-libgcc"
                linker.args "-static-libstdc++"
            } else if (targetPlatform.operatingSystem.name == "windows") {
                cppCompiler.args "-I${org.gradle.internal.jvm.Jvm.current().javaHome}/include/win32"
//                cppCompiler.define('WIN32')
//                linker.args 'Shlwapi.lib', 'Advapi32.lib'
            }
        }
    }
}

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

// Unzip bullet if not available
task unzipBullet(type: Copy) {
    def zipFile = file(bulletZipFile)
    def outputDir = file(".")

    from zipTree(zipFile)
    into outputDir
}
unzipBullet.dependsOn {
    def zipFilePath = project.projectDir.absolutePath + File.separator + bulletZipFile
    def zipFile = new File(zipFilePath)
//    println "zipFile path: " + zipFile.absolutePath
//    println "zipFile exists: " + zipFile.exists()
    if (!zipFile.exists()) {
        downloadBullet
    }
}

compileJava.dependsOn {
    def bulletUnzipDir = new File(project.projectDir.absolutePath + File.separator + bulletFolder)
//    println "bulletUnzipDir path: " + bulletUnzipDir.absolutePath
//    println "bulletUnzipDir exists: " + bulletUnzipDir.isDirectory()
    if (!bulletUnzipDir.isDirectory()) {
        unzipBullet
    }
}

//task buildAllExecutables {
//    dependsOn binaries.withType(SharedLibraryBinary).matching {
//        it.buildable
//    }
//}

// Adds all built binaries to java jar task
binaries.withType(SharedLibraryBinary) { binary ->
    if (!buildable) {
        //TODO: obtain elsewhere if not available
        return
    }
    // Get builder of this binary
    def builderTask = binary.tasks.builder
    // Add output to jar file
    jar.into("native/${targetPlatform.operatingSystem.name}/${targetPlatform.architecture.name}") { from builderTask.outputFile }
    // Add depend on build
    jar.dependsOn builderTask
}

// 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)
    }
}