apply plugin: 'application'

group = 'org.asciidoctor'
version = '1.0.0-SNAPSHOT'

repositories {
  mavenCentral()
  mavenLocal() // snags valid artifacts from local Maven repository
}

configurations {
  dist
}

dependencies {
  // xml-resolver needed to fix buggy resolver in JDK
  runtime 'xml-resolver:xml-resolver:1.2'
  // avalon dependencies need to be specified to align with version available
  compile 'org.apache.avalon.framework:avalon-framework-api:4.3.1'
  compile 'org.apache.avalon.framework:avalon-framework-impl:4.3.1'
  compile 'org.apache.xmlgraphics:fop:2.1'
  runtime 'net.sourceforge.jeuclid:jeuclid-fop:3.1.9'
  runtime 'net.sf.xslthl:xslthl:2.1.0'
  // To enable hyphenation, uncomment the next line, then run `./gradlew clean` then use `./fopub`
  //runtime 'net.sf.offo:fop-hyph:2.0'
  dist 'net.sf.docbook:docbook-xsl:1.79.1:ns-resources@zip'
  dist 'org.neo4j.build.plugins:neo4j-docbook-xml:4.5'
}

mainClassName = 'org.apache.fop.cli.Main'
applicationDefaultJvmArgs = ['-Dxml.catalog.files=APP_DIR/catalog.xml']
//applicationDefaultJvmArgs = ['-Dxml.catalog.files=/etc/xml/catalog']

installDist {
  destinationDir = file("$buildDir/${project.name}")
}

task extractDocbookXsl(type: Copy) {
  def archive = configurations.dist.find { it.name.endsWith('docbook-xsl-1.79.1-ns-resources.zip') }
  def outputDir = file("$buildDir/unpacked/docbook")
  from(zipTree(archive)) {
    // strip root docbook/ directory
    eachFile { it.path = it.path.substring(it.relativePath.segments[0].length()) }
  }
  into outputDir
  includeEmptyDirs = false
}

task extractDocbookXml(type: Copy) {
  def archive = configurations.dist.find { it.name.endsWith('docbook-xml-4.5.jar') }
  def outputDir = file("$buildDir/unpacked/docbook-dtds")
  from(zipTree(archive))
  into outputDir
}

// put the official DocBook resources (XSL, images, etc) into the dist
applicationDistribution.from(extractDocbookXsl) {
  into "docbook"
}

// put the DocBook DTDs into the dist
applicationDistribution.from(extractDocbookXml) {
  into "docbook/xml-dtd-4.5"
}

CreateStartScripts startScripts = project.startScripts
startScripts.with {
  doLast {
    unixScript.text = unixScript.text.replaceFirst('APP_DIR', '\\$APP_DIR')
    windowsScript.text = windowsScript.text.replaceFirst('APP_DIR', '%APP_DIR%')
  }
}
