import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream

initscript {
   repositories {
        maven { url 'http://jitpack.io' }
   }
   dependencies {
        classpath 'com.github.johni0702:gradle-reproducible-builds-plugin:a524ada'
        classpath 'com.github.johni0702:gradle-ecj-plugin:795736c'
   }
}

def convertRepoToHttp = { repo ->
    if (repo instanceof MavenArtifactRepository && repo.url.toString().startsWith('https://')) {
        URL url = repo.url.toURL()
        repo.url = new URL("http", url.getHost(), url.getPort(), url.getFile()).toURI()
    }
}

def setupDependencySubstitution = { Configuration configuration ->
    configuration.resolutionStrategy.eachDependency { dep ->
        def req = dep.requested
        if (req.group == 'net.minecraftforge.gradle' && req.name == 'ForgeGradle' && req.version == '2.0-SNAPSHOT') {
            // FG 2.1 works for MC 1.8 as well, as long as only CI builds are performed (fails when applying source patches)
            dep.useTarget 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT'
        }
        if (req.group == 'de.oceanlabs.mcp' && req.name == 'mcinjector') {
            switch (req.version) {
                case '3.2-SNAPSHOT':
                    dep.useTarget 'com.github.ModCoderPack:MCInjector:05aa80e'
                    break
                case '3.4-SNAPSHOT':
                    dep.useTarget 'com.github.ModCoderPack:MCInjector:7258466'
                    break
            }
        }
        if (req.group == 'net.minecraftforge.srg2source' && req.name == 'Srg2Source') {
            switch (req.version) {
                case '3.2-SNAPSHOT':
                    /* Fails to build on jitpack because of broken javadocs
                    dep.useTarget 'com.github.MinecraftForge:Srg2Source:95b5927'
                    */
                    break
                case '3.3-SNAPSHOT':
                    // Note: 3.3-SNAPSHOT is actually 5f11e29 but that doesn't build due to broken javadoc
                    dep.useTarget 'com.github.MinecraftForge:Srg2Source:dcad1a6'
                    break
                case '4.0-SNAPSHOT':
                    dep.useTarget 'com.github.MinecraftForge:Srg2Source:ea4ea62'
                    break
                default:
                    throw new IllegalStateException("Don't know how to replace Srg2Source version $req.version")
            }
        }
        if (req.group == 'net.minecraftforge' && req.name == 'fernflower' && req.version == '2.0-SNAPSHOT') {
            dep.useTarget 'com.github.MinecraftForge:FernFlowerLegacy:114aebe'
        }
        /* Could theoretically be built by jitpack but that fails for some reason
        if (req.group == 'net.minecraftforge' && req.name == 'forgeflower' && req.version == '1.0.342-SNAPSHOT') {
            dep.useTarget 'com.github.MinecraftForge:ForgeFlower:41d2b9e'
        }
        */
    }
}

allprojects {
    buildscript {
        def localRepo = rootProject.file('gradle/reprod/deps/repo')
        if (localRepo.exists()) repositories.maven { url localRepo.getAbsolutePath() }
        repositories.all convertRepoToHttp
        configurations.all setupDependencySubstitution
    }
    repositories { mavenCentral() } // for ecj
    repositories.all convertRepoToHttp
    apply plugin: de.johni0702.gradle.ReproducibleBuildsPlugin, to: project
    afterEvaluate {
        if (project.plugins.hasPlugin('java')) {
            apply plugin: xink.gradle.ecj.EcjPlugin, to: project

            ecj {
                warn = ['none']
            }
        }
    }

    def localRepo = rootProject.file('gradle/reprod/deps/repo')
    if (!localRepo.exists()) {
        localRepo = rootProject.file('../repo') // building deps
    }
    localRepo = localRepo.getAbsolutePath()
    repositories {
        maven { url localRepo }
        maven { url 'https://jitpack.io' }
    }
    afterEvaluate {
        if (project.plugins.hasPlugin('maven')) {
            uploadArchives {
                repositories.mavenDeployer {
                    repository url: 'file://localhost/' + localRepo
                }
            }
        }
    }

    configurations.all setupDependencySubstitution

    project.tasks.all { task ->
        if (task.name == 'reobfJar' && task.metaClass.respondsTo(task, 'getJar')) {
            task.doLast {
                println 'Manually repacking archive after reobfJar task'
                project.plugins.findPlugin(de.johni0702.gradle.ReproducibleBuildsPlugin.class).stripJar(task.getJar())
            }
        }
        if (task.name == 'reobf') {
            task.doLast {
                println 'Manually repacking archive(s) after reobf task'
                task.outputs.files.each { file ->
                    project.plugins.findPlugin(de.johni0702.gradle.ReproducibleBuildsPlugin.class).stripJar(file)
                }
            }
        }
    }
}
