/*
 * The MANIFEST.MF files of this project are maintained manually. We have to make sure
 * that the Bundle-Version entries have the current timestamp in their qualifier using
 * the same format as generated by Tycho for Eclipse plug-ins. We also have to generate
 * a proper MANIFEST.MF for the source bundles.
 */

def baseVersion = project.version
if (baseVersion.endsWith('-SNAPSHOT'))
	baseVersion = baseVersion.substring(0, baseVersion.length() - 9)
else if (baseVersion.split('\\.').length == 4)
	baseVersion = baseVersion.substring(0, baseVersion.lastIndexOf('.'))
def qualifiedVersion = baseVersion + '.v' + buildTime

//------------------------------------------------------
// Copy the existing manifest and insert the qualifier

ext.manifestReplacements = new HashMap()
manifestReplacements.put(baseVersion + '.qualifier', qualifiedVersion)

def eclipseSourceReferences() {
	def rootPath = java.nio.file.Paths.get(project.rootProject.projectDir.canonicalPath)
	def projectPath = java.nio.file.Paths.get(project.projectDir.canonicalPath)
	def relativePath = rootPath.relativize(projectPath)
	def url = grgit.remote.list().findAll{ it.name == "origin" }.first().url
	def commitId = grgit.head().id
	return "scm:git:${url};path=\"${relativePath}\"" + (commitId ? ";commitId=${commitId}" : "")
}

manifestReplacements.put("eclipseSourceReferences", eclipseSourceReferences())

File manifestFile = project.file("$buildDir/tmp/genManifest/MANIFEST.MF")

task genManifest(type: Copy) {
	inputs.property('qualifiedVersion', qualifiedVersion)
	from "META-INF/MANIFEST.MF"
	into "$buildDir/tmp/genManifest/"
	doLast {
		def f = manifestFile
		def text = f.text
		for (e in manifestReplacements.entrySet()) {
			text = text.replace(e.key, e.value)
		}
		def writer = new PrintWriter(f)
		writer.print(text)
		writer.close()
	}
}

task configureManifest {
	dependsOn genManifest
	doLast {
		if (manifestFile.isFile()) {
			jar.manifest {
				from manifestFile
			}
		}
	}
}

jar {
	dependsOn configureManifest, genManifest
}

//------------------------------------------------------
// Generate a manifest for the source bundle

def sourcesManifestFile = "$buildDir/tmp/genSourcesManifest/MANIFEST.MF"

task genSourcesManifest {
	outputs.file(sourcesManifestFile)
	inputs.property('qualifiedVersion', qualifiedVersion)
	inputs.property('projectName', project.name)
	inputs.property('projectTitle', project.findProperty('title')).optional(true)
	doLast {
		def f = new File(sourcesManifestFile)
		f.parentFile.mkdirs()
		def writer = new PrintWriter(f)
		writer.println("Manifest-Version: 1.0")
		writer.println("Bundle-ManifestVersion: 2")
		writer.println("Bundle-SymbolicName: ${project.name}.source")
		writer.println("Bundle-Version: ${qualifiedVersion}")
		if (project.hasProperty('title'))
			writer.println("Bundle-Name: ${project.title} Sources")
		else
			writer.println("Bundle-Name: Sources")
		writer.println("Bundle-Vendor: Eclipse Xtext")
		writer.println("Eclipse-SourceBundle: ${project.name};version=\"${qualifiedVersion}\"")
		writer.close()
	}
}

sourcesJar {
	dependsOn genSourcesManifest
	manifest {
		from sourcesManifestFile
	}
}

