sourceSets.main.java.srcDirs = [ "src/" ]

sourceCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

ext {
	mainClassName = "com.watabou.pd.IOSLauncher"
	mainExecutable = "IOSLauncher"
}

// Extracts native libs (*.a) from the native-ios.jar and places them
// under build/libs/ios/.
task copyNatives << {
  file("build/libs/ios/").mkdirs();
  configurations.natives.files.each { jar ->
    def outputDir = null
    if (jar.name.endsWith("natives-ios.jar")) outputDir = file("build/libs/ios")
    if (outputDir != null) {
      copy {
        from zipTree(jar)
        into outputDir
        include "*.a"
      }
    }
  }
}

// Updates a robovm.xml file.
task updateRoboVMXML << {
  def xml = file('robovm.xml')

  if (!xml.exists()) {
    return
  }
  
  // Find all native (*.a) libraries beneath libs
  def libtree = fileTree(dir: 'build/libs', include: '**/*.a')
  
  def config = new groovy.util.XmlParser().parse(xml)
  config.libs.each {libs ->
      libs.children().clear()
      libtree.each { File file ->
          libs.appendNode('lib', 'build/libs/ios/' + file.getName())
      }
  }
  
  def writer = new FileWriter(xml)
  def printer = new XmlNodePrinter(new PrintWriter(writer))
  printer.setPreserveWhitespace true
  printer.print(config)
}

task updateRoboVMProps << {
	ant.propertyfile(file: 'robovm.properties') {
		entry (key: 'app.version', value: version)
		entry (key: 'app.build', value: versionCode)
		entry (key: 'app.name', value: appTitle)
		entry (key: 'app.id', value: appId)
		entry (key: 'app.mainclass', value: mainClassName)
		entry (key: 'app.executable', value: mainExecutable)
	}
}

updateRoboVMXML.dependsOn copyNatives
build.dependsOn updateRoboVMXML, updateRoboVMProps
tasks.eclipse.dependsOn updateRoboVMXML, updateRoboVMProps

launchIPhoneSimulator.dependsOn build
launchIPadSimulator.dependsOn build
launchIOSDevice.dependsOn build
createIPA.dependsOn build


eclipse.project {
    name = appName + "-ios"
    natures 'org.robovm.eclipse.RoboVMNature'
}