/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a commented-out sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.5/userguide/tutorial_java_projects.html
 */

/*
// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.22'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}
*/

// Simple BC-project uploader

plugins {
    id "com.matthewprenger.cursegradle" version "1.1.2"
}

// Change this version to match the real version
// Only works for 7.99.20 and later.
ext.bc_version=project.getProperty("new_version")
ext.bc_state='beta'
ext.mc_version='1.12.2'
ext.javaVersion = 'Java 8'

ext.dlFolder = new File("bc_download_artifacts/" + bc_version);

def downloadBuildCraftFile(String from, String fileName) {
    def fullUrl = "https://www.mod-buildcraft.com/" + from + "/" + fileName;
    def url = new URL(fullUrl);
    url.openStream().withCloseable { stream ->
        java.nio.channels.Channels.newChannel(stream).withCloseable { rbc ->
            new FileOutputStream(new File(dlFolder,fileName)).withCloseable { fos ->
                // max 24MB - just in case something goes wrong.
                fos.getChannel().transferFrom(rbc, 0, 24_000_000);
            }
        }
    }
}

def downloadBuildCraftRelease(String from, String fileName) {
    downloadBuildCraftFile("releases/BuildCraft/" + bc_version + "/" + from, fileName);
}

// Download the files
    if (!dlFolder.exists()) {
        dlFolder.mkdirs();
        downloadBuildCraftRelease("", "buildcraft-all-" + bc_version + ".jar");
        downloadBuildCraftRelease("", "buildcraft-all-" + bc_version + "-sources.jar");
        downloadBuildCraftRelease("modules", "buildcraft-lib-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules/src", "buildcraft-lib-" + bc_version + "-sources.jar");
        downloadBuildCraftRelease("modules", "buildcraft-core-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-builders-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-energy-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-factory-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-robotics-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-silicon-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-transport-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-compat-" + bc_version + ".jar");
        downloadBuildCraftRelease("modules", "buildcraft-compat-" + bc_version + ".jar");
        downloadBuildCraftFile("build_info_full/BuildCraft/changelog", bc_version + ".txt");
    }

ext.changelogText = new File(dlFolder, bc_version + ".txt");

curseforge {
    apiKey = new File("api_key.txt").text.replace("\n", "");
    options {
        debug = false
        javaVersionAutoDetect = false
        javaIntegration = false
        forgeGradleIntegration = false
    }
    project {
        // All
        id = '61811'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            embeddedLibrary 'buildcraft-lib'
            embeddedLibrary 'buildcraft-core'
            embeddedLibrary 'buildcraft-builders'
            embeddedLibrary 'buildcraft-energy'
            embeddedLibrary 'buildcraft-factory'
            embeddedLibrary 'buildcraft-robotics'
            embeddedLibrary 'buildcraft-silicon'
            embeddedLibrary 'buildcraft-transport'
            embeddedLibrary 'buildcraft-compat'
        }
        mainArtifact new File(dlFolder, "buildcraft-all-" + bc_version + ".jar");
        addArtifact new File(dlFolder, "buildcraft-all-" + bc_version + "-sources.jar");
    }/*
    project {
        // Lib
        id = '311410'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        mainArtifact new File(dlFolder, "buildcraft-lib-" + bc_version + ".jar")
        addArtifact new File(dlFolder, "buildcraft-lib-" + bc_version + "-sources.jar");
    }*/
    project {
        // Core
        id = '231305'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            embeddedLibrary 'buildcraft-lib'
        }
        mainArtifact new File(dlFolder, "buildcraft-core-" + bc_version + ".jar");
    }
    project {
        // Builders
        id = '231307'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
        }
        mainArtifact new File(dlFolder, "buildcraft-builders-" + bc_version + ".jar");
    }
    project {
        // Energy
        id = '231308'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
            optionalDependency 'buildcraft-factory'
        }
        mainArtifact new File(dlFolder, "buildcraft-energy-" + bc_version + ".jar");
    }
    project {
        // Factory
        id = '231309'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
        }
        mainArtifact new File(dlFolder, "buildcraft-factory-" + bc_version + ".jar");
    }
    project {
        // Robotics
        id = '231313'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
        }
        mainArtifact new File(dlFolder, "buildcraft-robotics-" + bc_version + ".jar");
    }
    project {
        // Silicon
        id = '231311'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
        }
        mainArtifact new File(dlFolder, "buildcraft-silicon-" + bc_version + ".jar");
    }
    project {
        // Transport
        id = '231312'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
            optionalDependency 'buildcraft-silicon'
        }
        mainArtifact new File(dlFolder, "buildcraft-transport-" + bc_version + ".jar");
    }
    project {
        // Compat
        id = '233140'
        releaseType = bc_state
        changelog = changelogText
        changelogType = 'markdown'
        addGameVersion mc_version
        addGameVersion javaVersion
        relations {
            requiredDependency 'buildcraft-core'
        }
        mainArtifact new File(dlFolder, "buildcraft-compat-" + bc_version + ".jar");
    }
}
