apply plugin: 'java'
repositories{
  mavenCentral()
}
def releaseVersion = "0.5.9"
def propertyFile = file "src/main/java/w2phtml/project.properties"
Properties properties = new Properties()
propertyFile.withReader { properties.load(it) }
properties.setProperty('releaseDate', new Date().format('HH:mm:ss dd-MM-YYYY'))
properties.setProperty('releaseVersion', releaseVersion )
propertyFile.withWriter { properties.store(it, null) }

sourceCompatibility = 1.8
jar {
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": releaseVersion,
		   "Main-Class" : "w2phtml.Application",
		   "Class-Path" : "jasp.jar parser.jar")
    }
}
configurations{
    bundledLibs
}
dependencies{
    bundledLibs 'org.libreoffice:jurt:5.3.2'
    bundledLibs 'org.libreoffice:juh:5.3.2'
    bundledLibs 'org.libreoffice:ridl:5.3.2'
    bundledLibs 'org.libreoffice:unoil:5.3.2'
    bundledLibs 'org.json:json:20190722'
    compile 'junit:junit:4.12'
    bundledLibs group: 'org.apache.jena', name: 'jena-core', version: '3.14.0'
    bundledLibs group: 'com.opencsv', name: 'opencsv', version: '5.1'
    bundledLibs files('idl')
    configurations.compile.extendsFrom(configurations.bundledLibs)
}
jar {
    from {
            configurations.bundledLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
    from('src/main/java') {
        include '**/*.properties'
        include '**/*.xml'
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude 'org/openoffice/**/*'
    }
}
task xhtml(type: Jar) {
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": releaseVersion,
		   "Built-By": "litvinovg",
                   "RegistrationClassName" : "org.openoffice.da.comp.writer2xhtml.W2XRegistration",
                   "Class-Path" : "jasp.jar parser.jar")
    }
    baseName = "writer2xhtml-filter"
    from { 
	configurations.bundledLibs.collect { 
	    it.isDirectory() ? it : zipTree(it) 
	} 
    }
    from sourceSets.main.output 
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        include 'org/openoffice/da/comp/wl2common/**/*.class'
        include 'org/openoffice/da/comp/writer2xhtml/**/*.class'
        include 'org/openoffice/da/comp/wl2common/**/*.properties'
	include 'w2phtml/api/**/*.class'
	include 'w2phtml/base/**/*.class'
	include 'w2phtml/epub/**/*.class'
	include 'w2phtml/office/**/*.class'
	include 'w2phtml/util/**/*.class'
	include 'w2phtml/xhtml/**/*.class'
	include 'w2phtml/xhtml/**/*.xml'
	include 'w2phtml/xmerge/**/*.class'
    from 'src/main/java'
	include 'w2phtml/xhtml/**/*.properties'
	include 'w2phtml/xhtml/**/**/*.properties'
    from sourceSets.main.output
    include 'org/**/*'
}

task oxt(type: Zip){
    dependsOn = [ 'xhtml', 'setVersion' ]
    archiveName 'w2phtml.oxt'
    from 'src/main/oxt' 
    include '*' 
    include '**' 
    from 'build/libs/writer2xhtml-filter.jar'
    include '*' 
    from 'src/main/idl/writer2xhtml'
    include 'writer2xhtml.rdb' 
}
task setVersion(){   
   ant.taskdef(name: 'xmltask', classpath: 'buildPlugins/xmltask.jar', classname: 'com.oopsconsultancy.xmltask.ant.XmlTask') 
   ant.xmltask(source: 'src/main/oxt/description.xml', dest: 'src/main/oxt/description.xml', report:'true') {
     replace(path: "//*[local-name()='version']/@value", withText: "${releaseVersion}")
   }
}