import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
        .ofPattern("yyyy-MM-dd HH:mm:ssZ")
        .withZone(ZoneOffset.systemDefault())

project.ext.dependenciesWithJetty = configurations.compileClasspath.resolve().findAll { file ->
    return /*!file.name.contains("servlet-api") && */ !file.name.contains("-sources")
}

project.ext.jettylessDependencies = configurations.compileClasspath.resolve().findAll { file ->
    return !file.name.contains("jetty") && !file.name.contains("-sources")
}

project.ext.projectManifest = manifest {
    def manifestClasspath = project.ext.dependenciesWithJetty.collect { it.getName() }.join(',')
    attributes 'Main-Class': 'io.github.azagniotov.stubby4j.Main',
            'Package': 'io.github.azagniotov.stubby4j',
            'Class-Path': manifestClasspath,
            'Implementation-Title': "${stubbyProjectName}",
            'Implementation-Version': "${stubbyProjectVersion}",
            'Implementation-Vendor': 'Alexander Zagniotov',
            'Built-By': 'Alexander Zagniotov',
            'Built-Date': DATE_TIME_FORMATTER.format(Instant.now()),
            'Built-With': "gradle-${project.getGradle().getGradleVersion()}, groovy-${GroovySystem.getVersion()}",
            'Created-By': System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')'
}

project.ext.projectManifestNoJetty = manifest {
    def manifestClasspath = project.ext.jettylessDependencies.collect { it.getName() }.join(',')
    attributes 'Main-Class': 'io.github.azagniotov.stubby4j.Main',
            'Package': 'io.github.azagniotov.stubby4j',
            'Class-Path': manifestClasspath,
            'Implementation-Title': "${stubbyProjectName}",
            'Implementation-Version': "${stubbyProjectVersion}",
            'Implementation-Vendor': 'Alexander Zagniotov',
            'Built-By': 'Alexander Zagniotov',
            'Built-Date': DATE_TIME_FORMATTER.format(Instant.now()),
            'Built-With': "gradle-${project.getGradle().getGradleVersion()}, groovy-${GroovySystem.getVersion()}",
            'Created-By': System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')'
}

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    description = 'Creates a shaded/uberjar/fatjar of stubby4j'
    archiveFileName = "${stubbyProjectName}-${stubbyProjectVersion}.jar"
    manifest = project.ext.projectManifest
    from sourceSets.main.output
    from(project.ext.dependenciesWithJetty.collect { zipTree(it) }) {
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
        exclude 'META-INF/*.INF'
        exclude 'META-INF/*.inf'
    }
    doLast {
        println ":" + name + " Packaged the following dependencies: " + project.ext.dependenciesWithJetty.collect { File file -> file.name }
    }
}

task stubbySkinnyJar(type: Jar) {
    description = 'Creates a skinny/no-dependencies of stubby4j'
    archiveBaseName = "${stubbyProjectName}"
    archiveVersion = "${stubbyProjectVersion}"
    archiveClassifier = "no-dependencies"
    archiveExtension = "jar"
    manifest = project.ext.projectManifest
    from sourceSets.main.output

    doLast {
        println ":" + name + " Packaged classes for skinny JARs from: " + sourceSets.main.output.classesDirs.asPath
    }
}

task stubbyNoJettyJar(type: Jar) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    description = 'Creates a shaded/uberjar/fatjar of stubby4j WITHOUT Jetty classes'
    archiveBaseName = "${stubbyProjectName}"
    archiveVersion = "${stubbyProjectVersion}"
    archiveClassifier = "no-jetty"
    archiveExtension = "jar"
    manifest = project.ext.projectManifestNoJetty
    from sourceSets.main.output
    from(project.ext.jettylessDependencies.collect { zipTree(it) }) {
        exclude 'META-INF/*.SF'
        exclude 'META-INF/*.DSA'
        exclude 'META-INF/*.RSA'
        exclude 'META-INF/*.INF'
        exclude 'META-INF/*.inf'
    }
    doLast {
        println ":" + name + " Packaged the following dependencies: " + project.ext.jettylessDependencies.collect { File file -> file.name }
    }
}

task stubbyJavadocJar(type: Jar) {
    description = 'Creates a Javadoc JAR of stubby4j'
    archiveBaseName = "${stubbyProjectName}"
    archiveVersion = "${stubbyProjectVersion}"
    archiveClassifier = "javadoc"
    archiveExtension = "jar"
    from tasks.getByName(JavaPlugin.JAVADOC_TASK_NAME)
}

task stubbySourceJar(type: Jar) {
    description = 'Creates a Source JAR of stubby4j'
    archiveBaseName = "${stubbyProjectName}"
    archiveVersion = "${stubbyProjectVersion}"
    archiveClassifier = "sources"
    archiveExtension = "jar"
    from sourceSets.main.allJava
}

