// 1. a build script for jaxb plugin (currently 1.3.6 version can not be applied as a plugin)
buildscript {

    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.6'
        classpath 'gradle.plugin.nl.javadude.gradle.plugins:license-gradle-plugin:0.13.1'
    }

}

// 2. group and version for all projects
subprojects {
    group = 'com.netflix.imfutility'
    version = '1.0-itunes-beta'
}

// 3. define groups of projects with common configuration
project.ext {
    // all Java projects
    javaSubprojectList = subprojects

    // Java subprojects that generates Java clases from xsd (with jaxb plugin)
    xsdJavaSubprojectList = subprojects.findAll {
        it.name == 'dpp-conversion-input-xsd' ||
                it.name == 'itunes-conversion-input-xsd' ||
                it.name == 'imf-conversion-core' ||
                it.name == 'imf-conversion-common'
    }

    // Java projects that produce a test artifacts (.jar with tests)
    // to create dependencies between tests in subprojects
    testArtifactsJavaSubprojectList = subprojects.findAll {
        it.name == 'dpp-conversion' ||
                it.name == 'itunes-conversion' ||
                it.name == 'imf-conversion-core'
    }

}

// 4. configure all Java projects
configure(project.javaSubprojectList) {

    // 4.1 Java plugin

    apply plugin: 'java'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
        jcenter()
        flatDir {
            dirs '../ttml-java/dist', '../itunes-metadata-film/dist', '../itunes-metadata-tv/dist'
        }
    }

    dependencies {
        compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
        compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.5'
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }

    tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
    }

    // 4.2 Findbugs plugin

    apply plugin: 'findbugs'

    findbugs {
        sourceSets = [sourceSets.main]
        excludeFilter = rootProject.file("config/findbugs/findbugs-excludeFilter.xml")
    }

    tasks.withType(FindBugs) {
        // exclude generated classes
        // all generated classes must be in 'generated' package!
        classes = classes.filter {
            !it.path.contains('generated')
        }
        // create the reports in HTML
        reports {
            xml.enabled = false
            html.enabled = true
        }
    }

    // 4.4. Checkstyle plugin.
    apply plugin: 'checkstyle'

    checkstyle {
        sourceSets = [project.sourceSets.main]
        configFile = rootProject.file('config/checkstyle/checkstyle.xml')
    }

    tasks.withType(Checkstyle) {
        exclude '**/generated/**'
    }

    // 4.5. license task
    apply plugin: 'com.github.hierynomus.license'
    license {
        header rootProject.file('config/GPL_HEADER')
        ext.year = 2016
        ext.name = "Netflix, Inc."
        ext.product = "IMF Conversion Utility"
        excludes(["**/generated/**", "**/xsd/imf/**", "**/*.xjb", "**/*.xml", "**/*.hdr", "**/*.mxf"])
    }

}

// 5. configure Java projects that generate Java classes from XSD (jaxb plugin)
configure(project.xsdJavaSubprojectList) {
    // 5.1 apply jaxb plugin

    apply plugin: 'com.github.jacobono.jaxb'

    System.setProperty('javax.xml.accessExternalSchema', 'file')

    dependencies {
        jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41'
        jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
        jaxb 'javax.xml.bind:jaxb-api:2.2.7'
        jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.6.5"
        jaxb "org.jvnet.jaxb2_commons:jaxb2-basics:0.6.4"
        jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-annotate:0.6.4"
        jaxb "org.jvnet.jaxb2_commons:jaxb2-value-constructor:3.0"
    }

    jaxb {
        // all XSDs must be under the xsd folder!
        xsdDir = "${project.name}/src/main/resources/xsd"
        // all XJBs must be under the xjb folder!
        bindingsDir = "${project.name}/src/main/resources/xjb"
        bindings = ["**/*.xjb"]
        xjc {
            taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task"
            // generatePackages must be specified in xjb bindings
            args = ["-Xinheritance", "-Xannotate", "-Xsimplify"]
            destinationDir = "src/generated/java"
        }
    }

    // 5.2 include generated code in main and test source sets
    sourceSets {
        main {
            java {
                srcDir 'src/generated/java'
            }
        }

        test {
            java {
                srcDir 'src/generated/java'
            }
        }

    }

    // 5.3 delete generated code on clean
    clean {
        delete 'src/generated'
        delete 'schema'
    }

    // 5.4 execute xjc task (generate all classes) before compilation
    compileJava.dependsOn(xjc)
}

// 6. configure Java classes with tests
configure(project.testArtifactsJavaSubprojectList) {
    // 6.1 create a .jar with tests to be used as a dependency for other subprojects with dependent tests
    configurations {
        testArtifacts.extendsFrom testCompile
    }

    task jarTest(type: Jar, dependsOn: testClasses) {
        from sourceSets.test.output
        from sourceSets.test.resources
        classifier = 'test'
    }

    artifacts {
        testArtifacts jarTest
    }

}

// 7. copy the final distribution (of imf-conversion-main) to the build folder of the root project
apply plugin: 'base'

task copyDistr(type: Copy) {
    from 'imf-conversion-main/build/distributions'
    into "$buildDir"
}
task copyInstall(type: Copy) {
    from 'imf-conversion-main/build/install'
    into 'install'
}

clean {
    delete 'install'
}

copyDistr.dependsOn(subprojects.build)
copyInstall.dependsOn(':imf-conversion-utility:installDist')
build.finalizedBy(copyDistr, copyInstall)
