apply plugin: "java"

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

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

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

task run(dependsOn: classes, type: JavaExec){
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    if(project.hasProperty("appArgs")){
        args Eval.me(appArgs)
    }
}

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

task dist(type: Jar){
    from files(sourceSets.main.output.classesDirs)
    from files(sourceSets.main.output.resourcesDir)
    from{ configurations.compile.collect{ zipTree(it) } }
    from files(project.assetsDir)
    exclude("sprites/**")
    exclude("fonts/**")
    exclude("com/badlogic/gdx/**")
    exclude("bundles/**")

    writeVersion()

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

task dzip(type: Zip){
    from getServerFolder()
    archiveName "$appName-server-${getVersionString()}.zip"
    destinationDir(file("../deploy/"))

    finalizedBy 'cleanup'
}

task cleanup{
    doLast{
        delete{
            delete getServerFolder()
        }
    }
}

task 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
