import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory

import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption

buildscript {
	repositories {
		maven { url = 'https://maven.minecraftforge.net/' }
		maven { url = 'https://plugins.gradle.org/m2' }
		mavenCentral()
	}
	dependencies {
		classpath ('com.anatawa12.forge:ForgeGradle:1.2-1.0.+') {changing = true}
	}
}

apply plugin: 'forge'
apply plugin: 'curseforge'


if(Files.exists(Paths.get("curseforge.properties"))) {
	// Load CurseForge configuration
	ext.cfprops = parseConfig(file("curseforge.properties"))
}

def version_name = version = mod_version
if(!mod_build_number.isEmpty()) {
	version_name = mod_version + "_X" + mod_build_number
	version = "[${version_name}]"
}
group = "com.hbm" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "HBM-NTM"
compileJava.options.encoding = 'UTF-8'

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

minecraft {
	version = "1.7.10-10.13.4.1614-1.7.10"
	runDir = "eclipse"
}

// A little hack to fix codechicken's crazy maven structure (at least in 1.7.10)
eclipse.classpath.file.whenMerged { cp ->
	// Find all codechicken source jars
	def srcent = cp.entries.findAll { entry -> entry.path.contains("codechicken") && entry.path.endsWith("-src.jar") }
	
	// Remove them from classpath
	cp.entries.removeAll srcent

	// Map the source entries to their dev counterparts based on basename
	Map<String, File> srcmap = new HashMap<String, File>()
	srcent.forEach { entry ->
		def file = new File(entry.path)
		srcmap.put(file.getName().replace("-src.jar", "-dev.jar"), file)
	}
	
	// Create file reference factory
	def fileref = new FileReferenceFactory()
	
	// Find all codechicken development jars
	cp.entries.findAll { entry -> entry.path.contains("codechicken") && entry.path.endsWith("-dev.jar") }.forEach { entry ->
		File srcmapping = new File(entry.path) // Initialize the srcmapping from the dev jar path
		srcmapping = srcmap.get(srcmapping.getName()) // Transform it using the sourcemap
		entry.sourcePath = fileref.fromFile(srcmapping) // Set the source path
	}
}

repositories {
	maven {
		name = 'ModMaven'
		url = 'https://modmaven.dev'
	}
	maven {
		name = "gt"
		url = "https://gregtech.mechaenetia.com/"
	}
	//maven {
	//	name = "CurseForge"
	//	url = "https://minecraft.curseforge.com/api/maven/"
	//}
	maven {
		name = "Jitpack"
		url = "https://jitpack.io"
	}
	maven {
		name = "CurseMaven"
		url = "https://cursemaven.com"
	}
	maven {
        name = "OpenComputers"
        url = "https://maven.cil.li/"
    }
}

dependencies {
	implementation 'codechicken:CodeChickenCore:1.7.10-1.0.4.29:dev'
	compileOnly 'codechicken:CodeChickenCore:1.7.10-1.0.4.29:src'
	
	implementation 'codechicken:CodeChickenLib:1.7.10-1.1.3.140:dev'
	compileOnly 'codechicken:CodeChickenLib:1.7.10-1.1.3.140:src'
	
	implementation 'codechicken:NotEnoughItems:1.7.10-1.0.3.74:dev'
	compileOnly 'codechicken:NotEnoughItems:1.7.10-1.0.3.74:src'

	compileOnly "inventorytweaks:InventoryTweaks:1.59-dev:deobf"

	implementation "li.cil.oc:OpenComputers:MC1.7.10-1.5.+:api"

	compileOnly "com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta.56-GTNH:dev"
}

processResources {
	// this will ensure that this task is redone when the versions change.
	inputs.property "version", project.version
	inputs.property "mcversion", project.minecraft.version

	// replace stuff in mcmod.info, nothing else
	from(sourceSets.main.resources.srcDirs) {
		include 'mcmod.info'
		
		// replace version and mcversion
		filesMatching('mcmod.info') {
			// replace version, mcversion and credits
			expand([
					version: version_name,
					credits: project.credits
			])
		}
	}
		
	// copy everything else, thats not the mcmod.info
	from(sourceSets.main.resources.srcDirs) {
		exclude 'mcmod.info'
	}
}

// add AT to meta-inf
jar {
    manifest {
        attributes 'FMLAT': 'HBM_at.cfg'
    }
}

task version {
	doFirst {
		println project.version
	}
}

if(Files.exists(Paths.get("curseforge.properties"))) {
	curse {
		apiKey = cfprops.api_key
		projectId = cfprops.project_id
		releaseType = "release"

		displayName = "Hbm's Nuclear Tech Mod " + version_name.replace("_", "") + " for Minecraft 1.7.10"

		gameVersions.addAll([
				"Forge",
				"Java 8",
				"Client", "Server"
		])

		if (Files.exists(Paths.get("changelog"))) {
			changelog = String.join("\r\n", Files.readAllLines(Paths.get("changelog")))

			// Perform a backup of the changelog and create a new file for next changes
			doLast {
				Files.move(Paths.get("changelog"), Paths.get("changelog.bak"), StandardCopyOption.REPLACE_EXISTING)
				Files.createFile(Paths.get("changelog"))
			}
		}
	}
}

// Properties file parsing helper
static def parseConfig(File config) {
	config.withReader {
		def prop = new Properties()
		prop.load(it)
		return (new ConfigSlurper().parse(prop))
	}
}
