import org.gradle.api.artifacts.*

apply plugin: 'base' // To add "clean" task to the root project.
//apply plugin: 'java-library-distribution'

// This is applied to all sub projects
subprojects {
    // Don't add to native builds
    //    if(!project.name.endsWith('native')){
    apply from: rootProject.file('common.gradle')
    //    }
}

task(run, dependsOn: ':jme3-examples:build', type: JavaExec) {
    main = 'jme3test.TestChooser'
    classpath = files(subprojects.collect {project -> 
            project.sourceSets*.compileClasspath})
//    classpath = sourceSets.main.runtimeClasspath
//    args 'mrhaki'
//    systemProperty 'simple.message', 'Hello '
}

defaultTasks 'run'

task libDist(dependsOn: subprojects.build) << {
//    description 'Builds and copies the engine binaries, sources and javadoc to build/libDist'
    File libFolder = mkdir("$buildDir/libDist/lib")
    File sourceFolder = mkdir("$buildDir/libDist/sources")
    File javadocFolder = mkdir("$buildDir/libDist/javadoc")
    subprojects.each {project ->
        if(project.ext.mainClass == ''){
            project.tasks.withType(Jar).each {archiveTask ->
                if(archiveTask.classifier == "sources"){
                    copy {
                        from archiveTask.archivePath
                        into sourceFolder
                    }
                } else if(archiveTask.classifier == "javadoc"){
                    copy {
                        from archiveTask.archivePath
                        into javadocFolder
                    }
                } else{
                    copy {
                        from archiveTask.archivePath
                        into libFolder
                    }
                }
            }
        }
    }
}

task copyLibs(type: Copy){
//    description 'Copies the engine dependencies to build/libDist'
    from {
        subprojects*.configurations*.compile*.copyRecursive({ !(it instanceof ProjectDependency); })*.resolve()
    }
    
    into "$buildDir/libDist/lib-ext" //buildDir.path + '/' + libsDirName + '/lib'
}

task dist{
    description 'Creates a distribution of all jme3 binaries, sources, javadoc and external libraries under build/libDist'
}
dist.dependsOn libDist
dist.dependsOn copyLibs

task mergedJavadoc(type: Javadoc, description: 'Creates Javadoc from all the projects.') {
    title = 'All modules'
    destinationDir = new File(project.buildDir, 'merged-javadoc')

    // Note: The closures below are executed lazily.
    source subprojects.collect {project -> 
        project.sourceSets*.allJava
    } 
    classpath = files(subprojects.collect {project -> 
            project.sourceSets*.compileClasspath}) 
    //    source {
    //        subprojects*.sourceSets*.main*.allSource
    //    }
    //    classpath.from {
    //        subprojects*.configurations*.compile*.copyRecursive({ !(it instanceof ProjectDependency); })*.resolve()
    //    }
}

task wrapper(type: Wrapper, description: 'Creates and deploys the Gradle wrapper to the current directory.') {
    gradleVersion = '1.10'
}
