import java.time.LocalDateTime

plugins {
    id 'groovy'
    id 'maven-publish'
    id 'signing'
    id 'java-library'
    id 'idea'
}

//noinspection GroovyUnusedAssignment
version = "$getl_version"
group = 'net.sourceforge.getl'

configurations {
	addJars
	//deployerJars
}

repositories {
	mavenCentral()
}

dependencies {
	api("org.codehaus.groovy:groovy-all:$groovy_version") {
		exclude(group: 'org.codehaus.groovy', module: 'groovy-testng')
	}
	api "org.codehaus.groovy:groovy-dateutil:$groovy_version"
	api "org.codehaus.groovy:groovy-yaml:$groovy_version"

	api 'com.fasterxml.jackson.core:jackson-annotations:2.16.0'

	//implementation 'com.h2database:h2:1.4.200'

	addJars 'net.sf.supercsv:super-csv-java8:2.4.0'
	addJars 'com.h2database:h2:1.4.200'
	addJars 'net.lingala.zip4j:zip4j:2.11.1'
	//noinspection GradlePackageUpdate
	addJars 'com.sun.mail:smtp:1.6.5'
	addJars 'com.sun.mail:mailapi:1.6.5'
	//addJars 'javax.activation:activation:1.1.1'
	//addJars 'javax.mail:mail:1.4.7'
	//noinspection GradlePackageUpdate
	addJars 'com.jcraft:jsch:0.1.55'
	addJars files('file:libs/ftp4j-1.7.2.jar')
	compileOnly fileTree(dir: 'jdbc')

	implementation 'org.xerial:sqlite-jdbc:3.40.0.0'

	implementation 'org.apache.logging.log4j:log4j-api:2.18.0'
	implementation 'org.apache.logging.log4j:log4j-core:2.18.0'
	implementation 'org.slf4j:slf4j-api:1.7.36'
	implementation 'org.slf4j:slf4j-log4j12:1.7.36'

	implementation 'com.typesafe:config:1.4.2'

	//noinspection GradlePackageUpdate
	/*implementation('org.apache.poi:poi-ooxml:4.1.2') {
		exclude group: 'stax'
	}*/
	//implementation 'org.apache.poi:ooxml-schemas:1.4'

	implementation 'org.apache.poi:poi:5.2.2'
	implementation 'org.apache.poi:poi-ooxml:5.2.2'
	implementation 'org.apache.xmlbeans:xmlbeans:5.1.0'
	implementation 'com.github.pjfanning:excel-streaming-reader:3.2.0'
	//implementation 'com.monitorjbl:xlsx-streamer:2.2.0'
	//implementation 'com.github.pjfanning:poi-shared-strings:2.2.0'

	implementation 'org.apache.httpcomponents.client5:httpclient5:5.2'

	implementation 'com.google.code.gson:gson:2.9.0'
	implementation 'org.apache.hadoop:hadoop-auth:3.3.6'
	implementation 'org.apache.hadoop:hadoop-hdfs-client:3.3.6'
	implementation 'org.apache.hadoop:hadoop-common:3.3.6'
	implementation 'javax.servlet:javax.servlet-api:4.0.1'

	implementation 'com.github.albfernandez:javadbf:1.13.2'

	implementation 'org.apache.kafka:kafka-clients:3.6.1'

	implementation 'com.force.api:force-partner-api:55.2.0'

	configurations.implementation.extendsFrom(configurations.addJars)

	//deployerJars 'org.apache.maven.wagon:wagon-http:3.4.3'
	testImplementation 'junit:junit:4.13.2'
	testImplementation  fileTree(dir: 'tests', include: '**/*.jar')
	testImplementation fileTree(dir: 'jdbc')
}

test {
	maxHeapSize = '4G'
	//jvmArgs += ['-Djava.locale.providers=COMPAT,SPI,CLDR']

	(System.properties as Map<String, String>).each { String k, String v ->
		if (k.matches('getl[-]vars[.].+'))
			systemProperty k, v
	}
}

tasks.withType(GroovyCompile) {
  groovyOptions.optimizationOptions.indy = false
  options.incremental = true
  groovyOptions.fork = true
/*'-Dgroovy.antlr4=false'*/
  groovyOptions.forkOptions.jvmArgs += ['-Xmx2g', '-Dgroovy.antlr4.cache.threshold=250']
}

jar {
	from configurations.addJars.collect { it.isDirectory() ? it : zipTree(it) }
	from { ['licenses/getl.readme.txt', 'licenses/getl.license.txt', 'licenses/getl.license_rus.txt'] }
}

processResources.dependsOn "generateProjectConfig"

task("generateProjectConfig") {
	def resDir = "$buildDir/resources/main"
	doLast {
		file(resDir).mkdirs()
		file("$resDir/getl.conf").text = """/* 
Getl Configuration File
Generated: ${LocalDateTime.now().format('yyyy-MM-dd HH:mm')} 
*/
getl {
  jar {
    version='$version'
    compatibility='$getl_compatibility'
    year='2012-${LocalDateTime.now().year}'
  }
}
"""
	}
}

task toPackage(type: Zip) {
	archiveFileName = project.name + '-' + project.version + '.zip'
	from jar.archiveFile
	from fileTree('reports')
}

task copyDependencies(type: Copy) {
	from configurations.runtimeClasspath into "${project.buildDir}/libs.jar"
}

java {
	withSourcesJar()
}

groovydoc {
	use = true
}

task groovydocJar(type: Jar, dependsOn: groovydoc ) {
	classifier 'javadoc' // must use javadoc classifier to be able to deploy to Sonatype
	from groovydoc.destinationDir

}

javadoc {
	if(JavaVersion.current().isJava9Compatible()) {
		options.addBooleanOption('html5', true)
	}
}

jar.finalizedBy toPackage

publishing {
	def repLogin = project.properties."mavenLogin"
	def repPassword = project.properties."mavenPassword"
	def repRelease = project.properties."mavenRepRelease"
	def repSnapshot = project.properties."mavenRepSnapshot"

	publications {
		mavenJava(MavenPublication) {
			artifactId = 'getl'
			groupId = 'net.sourceforge.getl'
			from components.java
			artifact(groovydocJar) {
				classifier = 'javadoc'
			}
			versionMapping {
				usage('java-api') {
					fromResolutionOf('runtimeClasspath')
				}
				usage('java-runtime') {
					fromResolutionResult()
				}
			}
			pom {
				name = 'getl'
				description = 'Getl - based package in Groovy, which automates the work of loading and transforming data'
				url = 'https://github.com/ascrus/getl'
				/*properties = [
						myProp: "value",
						"prop.with.dots": "anotherValue"
				]*/
				licenses {
					license {
						name = 'GNU GENERAL PUBLIC LICENSE, Version 3'
						url = 'https://www.gnu.org/licenses/gpl.txt'
					}
				}
				developers {
					developer {
						id = 'ascrus'
						name = 'Alexsey Konstantinov'
						email = 'ascrus@easydata.ru'
						timezone = 'UTC+3'
					}
				}
				scm {
					/*connection = 'scm:git:git://example.com/my-library.git'
					developerConnection = 'scm:git:ssh://example.com/my-library.git'*/
					url = 'https://github.com/ascrus/getl.git'
				}
			}
		}
	}
	repositories {
		maven {
			url = version.endsWith('SNAPSHOT')?repSnapshot:repRelease
			allowInsecureProtocol = true
			credentials(PasswordCredentials) {
				username = repLogin
				password = repPassword
			}
		}
	}
}

signing {
	sign publishing.publications.mavenJava
}
