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

project.ext.mainClassName = "mindustry.server.ServerLauncher"
project.ext.assetsDir = new File("../core/assets")

ext.getServerFolder = {
    return "../deploy/${appName}-server-${getVersionString()}"
}

tasks.register('run', JavaExec){
    dependsOn classes
    mainClass = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    if(project.hasProperty("appArgs")){
        args Eval.me(appArgs)
    }
    if(project.hasProperty("jvmArgs")){
        jvmArgs((List<String>) Eval.me(project.getProperties()["jvmArgs"]))
    }
}

tasks.register('debug', JavaExec){
    dependsOn classes
    mainClass = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

tasks.register('dist', Jar){
    dependsOn configurations.runtimeClasspath
    from files(sourceSets.main.output.classesDirs)
    from files(sourceSets.main.output.resourcesDir)
    from{ configurations.runtimeClasspath.collect{ it.isDirectory() ? it : zipTree(it) } }
    from files(project.assetsDir)
    exclude("bloomshaders/**")
    exclude("sprites/**")
    exclude("music/**")
    exclude("sounds/**")
    exclude("fonts/**")
    exclude("bundles/**")
    exclude("cubemaps/**")
    exclude("config/**")
    exclude("cursors/**")
    exclude("shaders/**")
    exclude("icons/icon.icns")
    exclude("icons/icon.ico")
    exclude("icons/icon_64.png")
    exclude("**hs_err**.log")

    duplicatesStrategy = 'exclude'

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

tasks.register('dzip', Zip){
    from getServerFolder()
    archiveFileName = "${generateDeployName('server')}.zip"
    destinationDirectory = file("../deploy/")

    finalizedBy 'cleanup'
}

tasks.register('cleanup'){
    doLast{
        delete{
            delete getServerFolder()
        }
    }
}

tasks.register('deploy'){
    dependsOn "dist"

    finalizedBy 'dzip'

    doLast{
        copy{
            from "build/libs/server-release.jar"
            into getServerFolder()
            rename("server-release.jar", "server.jar")
        }

        copy{
            from "server_template"
            into getServerFolder()
        }
    }
}

dist.dependsOn classes
