//apply plugin:'application'

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

task run(dependsOn: 'build', type:JavaExec) {
    main = mainClass
    classpath = sourceSets.main.runtimeClasspath
    if (System.properties['os.name'].toLowerCase().contains('mac')) {
        jvmArgs "-XstartOnFirstThread"
        jvmArgs "-Djava.awt.headless=true"
    }

    if (System.properties['java.util.logging.config.file'] != null) {
        systemProperty "java.util.logging.config.file", System.properties['java.util.logging.config.file']
    }

    if( assertions  == "true" ){
        enableAssertions = true;
    }
}

dependencies {
    compile project(':jme3-blender')
    compile project(':jme3-core')
    compile project(':jme3-desktop')
    compile project(':jme3-effects')
//    compile project(':jme3-bullet')
//    compile project(':jme3-bullet-native')
    compile project(':jme3-jbullet')
    compile project(':jme3-jogg')
    compile project(':jme3-jogl')
    compile project(':jme3-lwjgl')
//    compile project(':jme3-lwjgl3')
    compile project(':jme3-networking')
    compile project(':jme3-niftygui')
    compile project(':jme3-plugins')
    compile project(':jme3-terrain')
    compile project(':jme3-testdata')
}

jar.doFirst{
    manifest {
        attributes('Manifest-Version'       : '1.0',
//                'Created-By'             : vendor,
//                'Specification-Title'    : appName,
//                'Specification-Version'  : jmeVersion,
//                'Specification-Vendor'   : "jMonkeyEngine",
//                'Implementation-Title'   : appName,
//                'Implementation-Version' : version,
//                'Implementation-Vendor'  : vendor,
                'Main-Class'             : getProperty('mainClass'),
                // Add dependencies to manifest, remove version
                'Class-Path'             : configurations.compile.resolvedConfiguration.resolvedArtifacts.collect {
                                                        'lib/' +
                                                        it.name +
                                                        (it.classifier != null ? '-' + it.classifier : '') +
                                                        '.' + it.extension }.join(' ')
        )
    }
}

task dist (dependsOn: ['build', ':jme3-jogl:jar', ':jme3-bullet:jar', ':jme3-android:jar', \
                       ':jme3-android-native:jar', ':jme3-bullet-native-android:jar', \
                       ':jme3-bullet-native:jar']) {
    doLast {
        // Copy all dependencies to ../dist/lib, remove versions from jar files
        configurations.compile.resolvedConfiguration.resolvedArtifacts.each { artifact ->
            copy {
                from artifact.file
                into '../dist/lib'
                if(artifact.classifier != null){
                    rename { "${artifact.name}-${artifact.classifier}.${artifact.extension}" }
                } else{
                    rename { "${artifact.name}.${artifact.extension}" }
                }
            }
        }
        copy {
            from jar.archivePath
            into '../dist'
            rename { "jMonkeyEngine3.jar" }
        }
        // Copy JOGL packages, remove version
        def config = project(':jme3-jogl').configurations.runtime.copyRecursive({ !(it instanceof ProjectDependency); })
        config.resolvedConfiguration.resolvedArtifacts.each {artifact ->
            copy{
                from artifact.file
                into '../dist/opt/jogl/lib'
                if(artifact.classifier != null){
                    rename { "${artifact.name}-${artifact.classifier}.${artifact.extension}" }
                } else{
                    rename { "${artifact.name}.${artifact.extension}" }
                }
            }
        }
        copy {
            from project(':jme3-jogl').jar.archivePath
            into '../dist/opt/jogl'
            rename {project(':jme3-jogl').name+".jar"}
        }

        // Copy bullet packages, remove version
        copy {
            from project(':jme3-bullet').jar.archivePath
            into '../dist/opt/native-bullet'
            rename {project(':jme3-bullet').name+".jar"}
        }
        copy {
            from project(':jme3-bullet-native').jar.archivePath
            into '../dist/opt/native-bullet'
            rename {project(':jme3-bullet-native').name+".jar"}
        }

        // Copy android packages, remove version
        copy {
            from project(':jme3-android').jar.archivePath
            into '../dist/opt/android'
            rename {project(':jme3-android').name+".jar"}
        }
        copy {
            from project(':jme3-android-native').jar.archivePath
            into '../dist/opt/android'
            rename {project(':jme3-android-native').name+".jar"}
        }
        copy {
            from project(':jme3-bullet-native-android').jar.archivePath
            into '../dist/opt/native-bullet'
            rename {project(':jme3-bullet-native-android').name+".jar"}
        }
    }
}