plugins {
	id 'eclipse'
}

eclipse.project.natures 'org.eclipse.pde.PluginNature'

sourceSets.test.java.srcDirs = ['src']


////////////////////////////////////////////////////////////////////////
//
//  download and extract kawa 3.0 "kawa.jar"
//

task downloadKawa(type: VerifiedDownload) {
	ext.version = '3.0'
	final def archive = "kawa-${version}.zip"
	src "https://ftp.gnu.org/pub/gnu/kawa/$archive"
	dest "$temporaryDir/$archive"
	checksum '2713e6dfb939274ba3b1d36daea68436'
}

task extractKawa(type: Sync, dependsOn: downloadKawa) {
	from(zipTree(downloadKawa.dest)) {
		include "kawa-${downloadKawa.version}/lib/kawa.jar"
		eachFile {
			relativePath new RelativePath(!directory, relativePath.lastName)
		}
	}
	into temporaryDir
	includeEmptyDirs false
	outputs.file "$temporaryDir/kawa.jar"
}


////////////////////////////////////////////////////////////////////////
//
//  specialized task for Kawa compilation into jar archive
//

@CacheableTask
class CompileKawaJar extends Jar {

	@Nested final def compile = project.task("compileKawaInto${name.capitalize()}", type: JavaExec) {
		dependsOn(project.extractKawa)
		final def kawaJar = project.files(project.extractKawa)[0]
		classpath kawaJar
		main 'kawa.repl'

		args '-d', temporaryDir
		outputs.dir temporaryDir

		logging.captureStandardError LogLevel.INFO
		args '--main', '-C'
	}

	CompileKawaJar() {
		compile.dependsOn(dependsOn)
		from compile
		version null
	}

	void setArgs(Object... args) {
		compile.args args
	}
}


////////////////////////////////////////////////////////////////////////
//
//  download, unpack, and build kawa chess
//

task downloadKawaChess(type: VerifiedDownload) {
	ext.commitHash = 'f1d2dcc707a1ef19dc159e2eaee5aecc8a41d7a8'
	src "https://github.com/ttu-fpclub/kawa-chess/archive/${commitHash}.zip"
	dest "$temporaryDir/kawa-chess.zip"
	checksum 'cf29613d2be5f476a475ee28b4df9d9e'
}

task unpackKawaChess(type: Sync, dependsOn: downloadKawaChess) {
	from zipTree(downloadKawaChess.dest)
	into temporaryDir
	ext.top = "$destinationDir/kawa-chess-${downloadKawaChess.commitHash}"
}

task buildChessJar(type: CompileKawaJar, dependsOn: unpackKawaChess) {
	final def schemePath = { "$unpackKawaChess.top/${it}.scm" }
	final def schemeFiles = files(['chess', 'gui', 'img', 'main', 'pos'].collect(schemePath))
	args schemePath('main')
	inputs.files schemeFiles
	baseName 'kawachess'
}


////////////////////////////////////////////////////////////////////////
//
//  build the kawa test jar
//

task buildKawaTestJar(type: CompileKawaJar) {
	final def schemeFile = file('kawasrc/test.scm')
	args schemeFile
	inputs.files schemeFile
	baseName 'kawatest'
}


////////////////////////////////////////////////////////////////////////
//
//  download and extract "bcel-5.2.jar"
//

task downloadBcel(type: VerifiedDownload) {
	ext.version = '5.2'
	def archive = "bcel-${version}.tar.gz"
	src "http://archive.apache.org/dist/jakarta/bcel/binaries/$archive"
	dest "$temporaryDir/$archive"
	checksum '19bffd7f217b0eae415f1ef87af2f0bc'
	useETag false
}

task extractBcel(type: Copy, dependsOn: downloadBcel) {
	from(tarTree(downloadBcel.dest)) {
		include "bcel-${downloadBcel.version}/bcel-${downloadBcel.version}.jar"
		eachFile {
			relativePath new RelativePath(!directory, relativePath.lastName)
		}
	}
	into projectDir
	includeEmptyDirs false
	outputs.file "bcel-${downloadBcel.version}.jar"
}

task cleanExtractBcel(type: Delete) {
	delete files(extractBcel)[0]
}

clean.dependsOn cleanExtractBcel



////////////////////////////////////////////////////////////////////////
//
//  download "java-cup-11a.jar"
//

task downloadJavaCup(type: VerifiedDownload) {
	def archive = 'java-cup-11a.jar'
	src "http://www2.cs.tum.edu/projects/cup/$archive"
	dest "$projectDir/$archive"
	checksum '2bda8c40abd0cbc295d3038643d6e4ec'
}

task cleanDownloadJavaCup(type: Delete) {
	delete downloadJavaCup
}

clean.dependsOn cleanDownloadJavaCup


////////////////////////////////////////////////////////////////////////
//
//  collect "JLex.jar"
//

task collectJLex(type: Jar) {
	from project(':com.ibm.wala.cast.java.test.data').compileTestJava
	include 'JLex/'
	baseName 'JLex'
	version null
	destinationDir projectDir
}

task cleanCollectJLex(type: Delete) {
	delete collectJLex
}

clean.dependsOn cleanCollectJLex


////////////////////////////////////////////////////////////////////////
//
//  generate "hello_hash.jar"
//

task downloadOcamlJava(type: VerifiedDownload) {
	def version = '2.0-alpha1'
	ext.basename = "ocamljava-$version"
	def archive = "${basename}.tar.gz"
	src "http://www.ocamljava.org/downloads/download.php?version=$version-bin"
	dest "$temporaryDir/$archive"
	checksum '45feec6e3889f5073a39c2c4c84878d1'
}

task unpackOcamlJava(type: Sync, dependsOn: downloadOcamlJava) {
	from tarTree(downloadOcamlJava.dest)
	into temporaryDir
}

task generateHelloHashJar(type: JavaExec, dependsOn: unpackOcamlJava) {
	def ocamlSource = file('ocaml/hello_hash.ml')
	inputs.file ocamlSource

	def jarTarget = file('hello_hash.jar')
	outputs.file jarTarget
	outputs.cacheIf { true }

	def ocamlJavaJar = new File("$unpackOcamlJava.destinationDir/$downloadOcamlJava.basename/lib/ocamljava.jar")
	inputs.file ocamlJavaJar
	classpath ocamlJavaJar

	main 'ocaml.compilers.ocamljavaMain'
	args ocamlSource, '-o', jarTarget
}

task cleanGenerateHelloHashJar(type: Delete) {
	delete generateHelloHashJar
	delete fileTree('ocaml') {
		exclude '*.ml'
		exclude '.gitignore'
	}
}

clean.dependsOn cleanGenerateHelloHashJar


////////////////////////////////////////////////////////////////////////
//
//  collect "com.ibm.wala.core.testdata_1.0.0.jar"
//

task collectTestData(type: Jar) {
	version '1.0.0'
	archiveName "${baseName}_${version}.${extension}"
	from compileTestJava
	from 'classes'
	includeEmptyDirs false
	destinationDir projectDir
}

task cleanCollectTestData(type: Delete) {
	delete collectTestData
}

clean.dependsOn cleanCollectTestData


////////////////////////////////////////////////////////////////////////
//
//  collect "com.ibm.wala.core.testdata_1.0.0a.jar"
//

task collectTestDataA(type: Jar) {
	version '1.0.0a'
	archiveName "${baseName}_${version}.${extension}"
	from compileTestJava
	from 'classes'
	includeEmptyDirs false
	destinationDir projectDir
	exclude (
		'**/CodeDeleted.class',
		'**/SortingExample.class',
		'**/A.class',
		)
}

task cleanColllectTestDataA(type: Delete) {
	delete collectTestDataA
}

clean.dependsOn cleanCollectTestDataA
