import com.badlogicgames.packr.Packr
import com.badlogicgames.packr.PackrConfig

apply plugin: "java"

sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]

project.ext.mainClassName = "io.anuke.mindustry.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../core/assets")

def JDK_DIR = "$System.env.PACKR_DIR"
def ICON_DIR = new File("core/assets/icons/icon.icns")

task run(dependsOn: classes, type: JavaExec){
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true

    if(System.getProperty("os.name").toLowerCase().contains("mac")){
        jvmArgs("-XstartOnFirstThread", "-Djava.awt.headless=true")
    }

    if(project.hasProperty("args")){
        args Eval.me(project.getProperties()["args"])
    }

    if(args.contains("debug")){
        main = "io.anuke.mindustry.DebugLauncher"
    }
}


task dist(type: Jar, dependsOn: classes){
    from files(sourceSets.main.output.classesDirs)
    from files(sourceSets.main.output.resourcesDir)
    from {configurations.compile.collect {zipTree(it)}}
    from files(project.assetsDir)

    archiveName = "${appName}.jar"

    manifest{
        attributes 'Main-Class': project.mainClassName
    }
}


PackrConfig.Platform.values().each{ platform ->
    task "packr${platform.toString()}"{
        def platformName = platform.toString().replace('64', '').replace('32', '').replace('MacOS', 'Mac')

        dependsOn dist

        doLast{
            copy{
                into "build/packr/"
                rename("${appName}.jar", "desktop.jar")
                from "build/libs/${appName}.jar"
            }

            delete{
                delete "build/packr/output/"
            }

            if(platform == PackrConfig.Platform.Windows32 || platform == PackrConfig.Platform.Windows64){
                copy{
                    into "build/packr/output"
                    from "${JDK_DIR}/templates/${platform.toString().toLowerCase()}"
                }

                copy{
                    into "build/packr/output/jre"
                    rename("${appName}.jar", "desktop.jar")
                    from "build/libs/${appName}.jar"
                }
            }else{
                def config = new PackrConfig()
                config.with{
                    config.executable = appName
                    config.platform = platform
                    verbose = true
                    bundleIdentifier = getPackage() + ".mac"
                    iconResource = ICON_DIR
                    outDir = file("build/packr/output")
                    mainClass = project.ext.mainClassName
                    classpath = ["desktop/build/packr/desktop.jar"]
                    removePlatformLibs = ["desktop/build/packr/desktop.jar"]

                    vmArgs = ["Djava.net.preferIPv4Stack=true"]
                    minimizeJre = "desktop/packr_minimize.json"
                    jdk = JDK_DIR + "jdk-${platform.toString().toLowerCase()}.zip"

                    if(platform == PackrConfig.Platform.MacOS){
                        vmArgs += "XstartOnFirstThread"
                    }
                }

                new Packr().pack(config)

                if(platform == PackrConfig.Platform.Linux64){
                    copy{
                        into "build/packr/output/jre/"
                        from "build/packr/output/desktop.jar"
                    }

                    delete{
                        delete "build/packr/output/desktop.jar"
                    }

                    file("build/packr/output/config.json").text = file("build/packr/output/config.json").text.replace("desktop.jar", "jre/desktop.jar")
                }

                if(platform == PackrConfig.Platform.MacOS){
                    copy{
                        into "build/packr/${appName}.app/Contents/"
                        from "build/packr/Contents/"
                    }

                    delete{
                        delete "build/packr/Contents/"
                    }
                }
            }
        }

        task "zip${platform.toString()}"(type: Zip){
            from "build/packr/output"
            archiveName "${generateDeployName(platform.toString())}.zip"
            destinationDir(file("../deploy"))

            doLast{
                delete{
                    delete "build/packr/"
                }
            }
        }

        finalizedBy "zip${platform.toString()}"
    }
}
