sourceSets.test {
	java.srcDirs = ['source']
	resources.srcDirs = [
		'data',
		project(':com.ibm.wala.core.testdata').compileTestJava,
		]
}

def osName = System.getProperty('os.name')
ext.isWindows = osName.startsWith('Windows ')

task downloadDroidBench(type: VerifiedDownload) {
	src 'https://codeload.github.com/secure-software-engineering/DroidBench/zip/DroidBench_2.0'
	dest "$temporaryDir/DroidBench_2.0.zip"
	checksum '16726a48329835140e14f18470a1b4a3'
}

task unpackDroidBench(type: Sync, dependsOn: downloadDroidBench) {
	from(zipTree(downloadDroidBench.dest)) {
		eachFile {
			relativePath new RelativePath(!directory, relativePath.segments[1..-1] as String[])
		}
	}

	into '/tmp/DroidBench'
	includeEmptyDirs false
}

task downloadAndroidSdk(type: VerifiedDownload) {
	def sdkOs
	switch (osName) {
		case ~/Linux/:
			sdkOs = 'linux'
			checksum '444e22ce8ca0f67353bda4b85175ed3731cae3ffa695ca18119cbacef1c1bea0'
			break
		case ~/Mac OS X/:
			sdkOs = 'darwin'
			checksum '4a81754a760fce88cba74d69c364b05b31c53d57b26f9f82355c61d5fe4b9df9'
			break
		case ~/Windows.*/:
			sdkOs = 'windows'
			checksum '7f6037d3a7d6789b4fdc06ee7af041e071e9860c51f66f7a4eb5913df9871fd2'
			break
	}
	def archive = "sdk-tools-$sdkOs-3859397.zip"
	src "https://dl.google.com/android/repository/$archive"
	dest "$temporaryDir/$archive"
	algorithm 'SHA-256'
}

task installAndroidSdk(type: Sync, dependsOn: downloadAndroidSdk) {
	from zipTree(downloadAndroidSdk.dest)
	into temporaryDir

	def buildToolsVersion = '26.0.2'
	ext {
		components = [
			'build-tools': buildToolsVersion,
			'platforms': "android-${buildToolsVersion.tokenize('.')[0]}"
		]
	}

	doLast {
		exec {
			def shell, shellFlags, yes, semicolon, discard
			if (project.isWindows) {
				shell = 'PowerShell'
				shellFlags = '-Command'
				yes = 'echo y'
				semicolon = '`;'
				discard = '$null'
			} else {
				shell = 'sh'
				shellFlags = '-ceu'
				yes = 'yes 2>/dev/null'
				semicolon = /\;/
				discard = '/dev/null'
			}

			def componentArgs = components.collect { "$it.key$semicolon$it.value" }.join ' '
			commandLine shell, shellFlags, "$yes | $temporaryDir/tools/bin/sdkmanager $componentArgs >$discard"
		}
	}
	outputs.cacheIf { true }
}

task copyDxJar(type: Sync, dependsOn: installAndroidSdk) {
	from "${files(installAndroidSdk).singleFile}/build-tools/${installAndroidSdk.components['build-tools']}/lib/dx.jar"
	into 'lib'
}

clean.dependsOn cleanCopyDxJar

compileTestJava.dependsOn copyDxJar
afterEclipseBuildshipImport.dependsOn copyDxJar

task copyAndroidJar(type: Sync, dependsOn: installAndroidSdk) {
	from "${installAndroidSdk.destinationDir}/platforms/${installAndroidSdk.components['platforms']}/android.jar"
	into temporaryDir
}

task downloadSampleCup(type: VerifiedDownload) {
	src 'http://www.cc.gatech.edu/gvu/people/faculty/hudson/java_cup/classes.v0.9e/java_cup/parser.cup'
	dest 'data/sample.cup'
	checksum '76b549e7c6e802b811a374248175ecf4'
}

clean.dependsOn cleanDownloadSampleCup

task downloadSampleLex(type: VerifiedDownload) {
	src 'https://www.cs.princeton.edu/~appel/modern/java/JLex/current/sample.lex'
	dest 'data/sample.lex'
	checksum 'ae887758b2657981d023a72a165da830'
}

clean.dependsOn cleanDownloadSampleLex

dependencies {
	testCompile(
		'junit:junit:4.12',
		'org.osgi:org.osgi.core:4.2.0',
		files("${copyDxJar.destinationDir}/dx.jar"),
		project(':com.ibm.wala.core'),
		project(':com.ibm.wala.dalvik'),
		project(':com.ibm.wala.shrike'),
		project(':com.ibm.wala.util'),
		project(configuration: 'testArchives', path: ':com.ibm.wala.core.tests'),
		)
	testRuntime files("${copyAndroidJar.destinationDir}/android.jar")
}

processTestResources {
	from copyAndroidJar
	from downloadSampleCup
	from downloadSampleLex

	def testdata = project(':com.ibm.wala.core.testdata')
	from testdata.collectJLex
	from testdata.collectTestDataA
	from testdata.downloadJavaCup
}

if (isWindows)
	test.exclude '**/droidbench/**'
else
	processTestResources.dependsOn unpackDroidBench

test {
	maxHeapSize = '800M'
}

task cleanTest(type: Delete) {
	delete(
		'parser.java',
		'report',
		'sym.java',
		)
}

clean.dependsOn cleanTest
