import org.apache.tools.ant.taskdefs.condition.Os

version = '4.3.1'
ext.mediaDir = file('media')
ext.libCompileDir = file('lib-compile')

buildDir = file('build/gradle')
ext.externalLibsDir = file("$buildDir/externalLibs")

buildscript {
    repositories {
        flatDir {
            dirs 'lib-compile'
        }
        maven {
            url 'http://jcenter.bintray.com'
        }
    }
    dependencies {
        // Download after install4j 6
        classpath ':i4jgradle'

        // remove the next two entries after gradle-bintray-plugin is updated with pull request
        classpath ':gradle-bintray-plugin'
        classpath('org.codehaus.groovy.modules.http-builder:http-builder:0.6') {
            exclude(module: 'groovy')
        }

        //classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:0.3'
    }
}

allprojects {

    buildDir = new File(rootProject.buildDir, path.substring(1).replaceAll(':', '/'))
    version = rootProject.version

    repositories {
        flatDir {
            dirs libCompileDir
        }
        maven {
            url 'http://jcenter.bintray.com'
        }
    }

    plugins.withType(JavaPlugin).whenPluginAdded {
        dependencies {
            testCompile 'org.testng:testng:6.8.8'
        }

        tasks.withType(JavaCompile).each {JavaCompile compileJava ->
            compileJava.with {
                String java6Home = System.getenv()['JAVA_1_6_HOME']
                if (java6Home) {
                    if (Os.isFamily(Os.FAMILY_MAC)) {
                        options.bootClasspath = "${java6Home}/../Classes/classes.jar"
                    } else {
                        options.bootClasspath = "${java6Home}/jre/lib/rt.jar"
                    }
                } else {
                    println 'Warning: Set JAVA_1_6_HOME to a Java 6 JDK'
                }

                sourceCompatibility = '1.6'
                targetCompatibility = '1.6'
            }
        }

        tasks.withType(Test).each {Test test ->
            test.with {
                useTestNG()
            }
        }
    }
}

task clean << {
    delete externalLibsDir
}

task test()
task dist()

gradle.projectsEvaluated {
    getTasksByName('clean', true).each {task ->
        if (task != clean) {
            clean.dependsOn(task)
        }
    }
    getTasksByName('dist', true).each {task ->
        if (task != dist) {
            dist.dependsOn(task)
        }
    }
    getTasksByName('test', true).each {task ->
        if (task != test) {
            test.dependsOn(task)
        }
    }
}
