apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

configurations {
    jarjar
	jarjardeps
}

task repackageTask(type: Jar) { repackJar ->
	repackJar.baseName = "ews-android-api-repackaged-dependencies"

	doLast() {
		project.ant {
			taskdef name: "jarjar", classname: "com.tonicsystems.jarjar.JarJarTask", classpath: configurations.jarjar.asPath
			jarjar(destfile: repackJar.archivePath) {
				configurations.jarjardeps.each { originalJar ->
					zipfileset(src: originalJar)
				}

				rule(pattern: "org.apache.commons.**", 	result: "android.org.apache.commons.@1")
				rule(pattern: "org.apache.http.**", 	result: "android.org.apache.http.@1")
				rule(pattern: "javax.xml.stream.**", 	result: "android.javax.xml.stream.@1")
				rule(pattern: "javax.xml.namespace.**", result: "android.javax.xml.namespace.@1")
				rule(pattern: "javax.naming.**", 		result: "android.javax.naming.@1")
				rule(pattern: "javax.xml.bind.**", 		result: "android.javax.xml.bind.@1")
				rule(pattern: "javax.xml.XMLConstants", result: "android.javax.xml.XMLConstants")
				rule(pattern: "com.bea.xml.stream.MXParserFactory", result: "com.sun.xml.stream.ZephyrParserFactory") // inputFactory
				rule(pattern: "com.bea.xml.stream.XMLOutputFactoryBase", result: "com.sun.xml.stream.ZephyrWriterFactory") // outputFactory
				rule(pattern: "com.bea.xml.stream.EventFactory", result: "com.sun.xml.stream.events.ZephyrEventFactory") // eventFactory
			}
		}
	}
}

dependencies {
	jarjar 'com.googlecode.jarjar:jarjar:1.3'
	
	// httpclient and its dependencies
	jarjardeps("org.apache.httpcomponents:httpclient:4.4.1@jar")
	jarjardeps("commons-codec:commons-codec:1.9@jar")
	jarjardeps("org.apache.httpcomponents:httpcore:4.4.1@jar")
	
	jarjardeps("org.apache.commons:commons-lang3:3.4@jar")
	jarjardeps("commons-logging:commons-logging:1.2@jar")
	
	jarjardeps("javax.xml.bind:jaxb-api:2.2.11@jar")
	jarjardeps fileTree(dir: 'libs', include: ['jndi-1.2.1.jar'])
	
	// sjsxp and its dependencies
	jarjardeps("com.sun.xml.stream:sjsxp:1.0.2@jar")
	jarjardeps fileTree(dir: 'libs', include: ['jsr173_1.0_api.jar'])

	compile(files(repackageTask))
	compile 'joda-time:joda-time:2.8'
	compile 'dnsjava:dnsjava:2.1.6'
}

jar {
	dependsOn repackageTask
	from(zipTree(repackageTask.archivePath)) {
		include "android/**"
		include "com/sun/**"
		include "META-INF/**"

		rename { fileName ->
			if (fileName.startsWith('javax.xml.stream.')) {
				return "android.${fileName}"
			}
			return fileName
		}
	}
}
