buildscript {
    repositories { mavenCentral() }
    dependencies {
        classpath fileTree("../i2pjars") { include '*.jar' }
        classpath 'gnu.getopt:java-getopt:1.0.13'
    }
}

apply plugin : 'java'

dependencies {
    compile project(":webui")
}

def buildDir = new File("$buildDir")
def zipDir = new File(buildDir, "zip")
def libDir = new File(zipDir, "lib")
def consoleDir = new File(zipDir, "console")
def webAppDir = new File(consoleDir, "webapps")
def keystore = new File(System.getProperty("user.home")+"/.i2p-plugin-keys/plugin-su3-keystore.ks")


String libDirPath() {
    def libDir = new File("$buildDir/zip/lib")
    libDir.listFiles().stream().map({
        "\$PLUGIN/lib/" + it.getName()
        }).collect(java.util.stream.Collectors.joining(','))
}

task pluginConfig {
    doLast {
        def binding = [ "version" : project.version + "-b" + project.buildNumber,
                        "author" : project.author,
                        "signer" : project.signer,
                        "websiteURL" : project.websiteURL,
                        "updateURLsu3" : project.updateURLsu3,
                        "i2pVersion" : project.i2pVersion ]
                        
        def templateFile = new File("$projectDir/templates/plugin.config.template")
        def engine = new groovy.text.SimpleTemplateEngine()
        def output = engine.createTemplate(templateFile).make(binding)
        
        zipDir.mkdirs()
        
        def outputFile = new File(zipDir,"plugin.config")
        outputFile.text = output
    }
}

task pluginDir {
    dependsOn ':webui:assemble'
    doLast { task ->
        libDir.mkdirs()
        def webapp = project(":webui")
        def i2pjars = task.project.fileTree("../i2pjars").getFiles()
        webapp.configurations.runtime.each {
            if (!i2pjars.contains(it)) {
                def dest = new File(libDir, it.getName())
                        java.nio.file.Files.copy(it.toPath(), dest.toPath())
            }
        }
        
        webAppDir.mkdirs()
        def warFile = webapp.configurations.warArtifact.getAllArtifacts().file[0]
        def dest = new File(webAppDir, "MuWire.war")
        java.nio.file.Files.copy(warFile.toPath(), dest.toPath())
        "zip -d ${dest.toString()} *.jar".execute()
    }
}

task webappConfig {
    doLast {
        def cPath = "webapps.MuWire.classpath=" + libDirPath()
        def webappConfig = new File(consoleDir, "webapps.config")
        webappConfig.text = cPath
    }
}

task pack {
    doLast {
        if (project.pack200 == "true") {
            libDir.listFiles().stream().filter( { it.getName().endsWith(".jar") } ).
                filter({it.length() > 512*1024}).forEach {
                println "packing $it"
                def name = it.toString()
                println "pack200 --no-gzip ${name}.pack $name".execute().text
                it.delete()        
            }
        } else {
            println "pack200 disabled"
        }
    }
}

task pluginZip(type: Zip) {
    archiveFileName = "MuWire.zip"
    destinationDirectory = buildDir
    from zipDir
}

task sign {
    doLast {
        def password = project.keystorePassword
        if (password == "") {
            println "enter your keystore password:"
            def reader = new BufferedReader(new InputStreamReader(System.in))
            password = reader.readLine()
        }
        def su3args = []
        def version = project.version + "-b" + project.buildNumber
        su3args << 'sign' << '-t' << '6' << '-c' << 'PLUGIN' << '-f' << '0' << '-p' << password << \
            "$buildDir/MuWire.zip".toString() << "$buildDir/MuWire.su3".toString() << \
            keystore.getAbsoluteFile().toString() << version << project.signer
        println "now enter private key password for signer $project.signer"
        net.i2p.crypto.SU3File.main(su3args.toArray(new String[0]))
    }
}

webappConfig.dependsOn pluginDir
pack.dependsOn webappConfig
pluginZip.dependsOn(webappConfig,pluginConfig,pack)
sign.dependsOn pluginZip
assemble.dependsOn sign
