apply plugin: 'groovy'
apply plugin: 'application'
apply plugin: PublishJarsPlugin


application {
  mainClass = "nz.org.riskscape.engine.cli.Main"
  applicationName = "riskscape"
}

publishJars {
  artifactId = 'engine-app'
}

dependencies {
  api project(':engine:core')
  api project(':engine:common')
  implementation project(':engine:i18n')
  testImplementation project(':test:shared')
  testImplementation group: 'org.geotools', name: 'gt-imagemosaic', version: project.property('geotoolsVersion')
  implementation group: 'org.jline', name: 'jline-terminal', version: project.property('jlineVersion')
  implementation group: 'org.jline', name: 'jline-terminal-jna', version: project.property('jlineVersion')
  implementation group: 'org.jline', name: 'jline-reader', version: project.property('jlineVersion')
  implementation group: 'ch.qos.logback', name: 'logback-classic', version: project.property('logbackVersion')
}

def defaultScriptsTask = tasks['startScripts']
def unixScript = resources.text.fromFile('src/main/bin/unixStartScript.txt')
def windowsScript = resources.text.fromFile('src/main/bin/windowsStartScript.txt')
defaultScriptsTask.unixStartScriptGenerator.template = unixScript
defaultScriptsTask.windowsStartScriptGenerator.template = windowsScript

javadoc {
  exclude 'picocli/**'
}

test {
  // Set the RISKSCAPE_HOME varible to change the default home directory. These tests
  // require a clean settings.ini file. If we don't set this then if you have enabled
  // plugins etc in your setup ($HOME/.config/riskscape/settings.ini) then the tests may
  // break
  environment "RISKSCAPE_HOME", "src/test/resources/home-empty"
}

// this task copies all the plugins from riskscape plugin projects in to the install dir
// the distribution plugin is then configured to include build/install/plugins in to build/install/riskscape
// It would be nice if we could avoid this intermediate copy, and get the installDist task to pull the plugins
// directly from their own build dirs to avoid double handling them, but I'm just not sure how to do that -
// presumably we'd need to 'wrap' the installDist task, or replace it, so that the dirs are copied at that stage.
// note that copying the plugins in to build/install/riskscape doesn't work - the installdist plugin cleans that
// dir before assembling it
def assemblePlugins = task('assemblePlugins').doLast {
  def pluginProjects = project(':').subprojects.findAll { it.extensions.findByName('riskscapePlugin') != null }

  def intoBase = 'build/plugins/'

  for (pluginProject in pluginProjects) {

    def intoDir = intoBase +
      (pluginProject.riskscapePlugin.enabledByDefault ? 'enabled/' : 'optional/') +
      pluginProject.riskscapePlugin.id

    def workSpec = copy {
      from(pluginProject.riskscapePlugin.installDir) {
	include "*.jar"
      }
      from(pluginProject.riskscapePlugin.installDir) {
        include "*.py"
      }
      into intoDir
    }
  }
}

// installdist needs all the plugins to be ready for inclusion
project.tasks.find {it.name == 'installDist' }.dependsOn('assemblePlugins')

def cal = new GregorianCalendar()
distributions {
  main {
    contents {
      from("../core/src/main/i18n") {
        into "i18n"
        include "*.properties"
      }
      from("src/main/i18n") {
        into "i18n"
        include "*.properties"
      }
      from("build/plugins/enabled") {
        into 'plugins'
      }
      from("build/plugins/optional") {
        into 'plugins-optional'
      }
      from("../..") {
        include "LICENSE"
        include "COPYRIGHT"
        expand([CURRENT_YEAR: cal.get(Calendar.YEAR)])
      }
    }
  }
}
