apply plugin: 'java'
repositories{
  mavenCentral()
}
def propertyFile = file "src/main/java/w2phtml/project.properties"
def propertyDestFile = file "src/main/tmp/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', project.version )
mkdir "src/main/tmp/w2phtml"
propertyDestFile.withWriter { properties.store(it, null) }

sourceCompatibility = 1.8
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'
    bundledLibs group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    bundledLibs group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
    compile 'junit:junit:4.12'
    bundledLibs group: 'com.miglayout', name: 'miglayout-swing', version: '5.2'
    bundledLibs group: 'org.apache.jena', name: 'jena-core', version: '3.15.0'
    bundledLibs group: 'org.apache.jena', name: 'jena-arq', version: '3.15.0'
    bundledLibs group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
    bundledLibs group: 'com.opencsv', name: 'opencsv', version: '5.1'
    bundledLibs group: 'org.apache.commons', name: 'commons-imaging', version: '1.0-alpha2'

    configurations.compile.extendsFrom(configurations.bundledLibs)
}
jar {
    archiveName 'w2phtml.jar'
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": project.version,
           "Main-Class" : "w2phtml.Application",
           "Class-Path" : "jasp.jar parser.jar")
    }

    from {
            configurations.bundledLibs.collect { it.isDirectory() ? it : zipTree(it) }
    }
    from('src/main/java') {
        include '**/*.properties'
        include '**/*.rdf'
        include '**/*.xml'
        include 'pro/litvinovg/w2phtml/gui/resources/*.png'
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude 'org/openoffice/**/*'
        exclude 'w2phtml/project.properties'
    }
    from('src/main/tmp') {
        include 'w2phtml/project.properties'
    }
}
task xhtml(type: Jar) {
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": project.version,
                   "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 '.gradle'
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude '**/*.java'
	    include 'w2phtml/pageSplitters/**/*.class'
        include 'pro/litvinovg/**/*.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/**/*'
        include 'net/**/*'
}

task xhtml2(type: Jar) {
    archiveName 'writer2phtml.jar'
    manifest {
        attributes("Implementation-Title": rootProject.name,
                   "Implementation-Version": project.version,
                   "RegistrationClassName" : "pro.litvinovg.w2phtml.RegistrationHandler",
                   "Class-Path" : "jasp.jar parser.jar")
    }
    baseName = "writer2phtml"
    from {
      configurations.bundledLibs.collect {
        exclude 'module-info.class'
        exclude '.gradle'
        exclude '**/LICENSE*'
        exclude '**/DEPENDENCIES'
        exclude '**/NOTICE*'
        exclude '**/pom.xml'
        exclude 'steps.txt'
        exclude 'META-INF/services'

        it.isDirectory() ? it : zipTree(it)
      }
    }
    from (sourceSets.main.output){
        exclude '**/*Test.class'
        exclude '**/AllTests.class'
        exclude '**/*.java'
        include '**/*.classes'
        include 'etc/*'
        include 'com/**'
        include 'com/**/*'
        include 'w2phtml/pageSplitters/**/*.class'
        include 'pro/litvinovg/**/*.class'
        include 'pro/litvinovg/**/*.png'
        include 'w2phtml/api/**/*.class'
        include 'w2phtml/*.class'
        include 'w2phtml/*.properties'
        include 'w2phtml/base/**/*.class'
        include 'w2phtml/rdf/**/*.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'
        include 'org/**/*'
        include 'net/**/*'
        include 'logback.xml'
        include 'ch/**/*'
        include '**/*.properties'
        exclude '.gradle'
    }
    from ('src/main/java'){
        exclude '.gradle'
        include 'w2phtml/xhtml/**/*.properties'
        include 'w2phtml/xhtml/**/**/*.properties'
        include 'pro/litvinovg/**/*.classes'
        include 'logback.xml'
        include '**/*.png'
        exclude 'w2phtml/project.properties'
    }
    from('src/main/tmp') {
        exclude '.gradle'
        include 'w2phtml/project.properties'
    }
}


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' 
    from 'releasenotes.txt'
    include '*'
}

task oxtNew(type: Zip){
    dependsOn = [ 'xhtml2', 'setVersion' ]
    archiveName 'w2phtml-new.oxt'
    from ('src/main/oxt2'){
        include '*'
        include '**'
        exclude '.gradle'
        exclude 'description.xml'
    }
    from('src/main/tmp') {
        include 'description.xml'
    }
    from ('build/libs/writer2phtml.jar'){
        include '*'
    }
    from 'idl/writer2paginatedhtml.rdb'
    from 'releasenotes.txt'
    include '*'
}

task setVersion(){   
   ant.taskdef(name: 'xmltask', classpath: 'buildPlugins/xmltask.jar', classname: 'com.oopsconsultancy.xmltask.ant.XmlTask') 
   ant.xmltask(source: 'src/main/oxt2/description.xml', dest: 'src/main/tmp/description.xml', report:'true') {
     replace(path: "//*[local-name()='version']/@value", withText: "${project.version}")
   }
}