configurations {
    warArtifact
    jarArtifact
}

apply plugin : 'war'

dependencies {
    providedCompile(project(':core')) {
        transitive = false
    }
    implementation fileTree("../i2pjars") { include '*.jar' }
}

war {
    from 'src/main/css'
    from ('src/main/images', {
        into "images"
    })
    from ('src/main/js', {
        into "js"
    })
    from ('src/main/resources', {
        into "WEB-INF/classes/com/muwire/webui"
    })
    webInf { 
        from "$buildDir/compiledJsps" 
        into "classes"
    }
    excludes = new HashSet(['**/*.jsp', '**/*.jsi'])
    webXml = file("$buildDir/tmp_jsp/web.xml")
}

task precompileJsp {
    doLast {
        ant.taskdef (name : 'jasper', 
            classname: 'org.apache.jasper.JspC', 
            classpath: configurations.compileClasspath.asPath)
        def generated = new File("$buildDir/tmp_jsp")
        generated.mkdirs()
        ant.jasper(package: 'com.muwire.webui',
                    classPath : sourceSets.main.runtimeClasspath.asPath,
                    uriroot: webAppDir,
                    outputDir: "$buildDir/tmp_jsp",
                    compilerSourceVM: project.sourceCompatibility,
                    compilerTargetVM: project.targetCompatibility,
                    webXmlFragment: "$buildDir/tmp_jsp/web.xml.jasper")
        def output = new File("$buildDir/compiledJsps")
        output.mkdirs()
        ant.javac(srcDir: 'build/tmp_jsp',
                    classPath : sourceSets.main.runtimeClasspath.asPath,
                    debug : true,
                    includeAntRuntime : false,
                    deprecation : "on",
                    source: project.sourceCompatibility,
                    target: project.targetCompatibility,
                    destDir:file("$buildDir/compiledJsps"))

    }
}

task generateWebXML {
    doLast {
        def template = new File("$projectDir/templates/web.xml.template")
        def templateText = template.text
        def jasper = new File("$buildDir/tmp_jsp/web.xml.jasper")
        templateText = templateText.replaceAll("__JASPER__", jasper.text)
        templateText = templateText.replaceAll("__VERSION__", project.version)
        templateText = templateText.replaceAll("__BUILD_NUMBER__", project.buildNumber)
        def webXml = new File("$buildDir/tmp_jsp/web.xml")
        webXml.text = templateText
    }
}

// compile the po files and put them in the jar
task bundle {
    doLast {
        // run bundle-messages.sh
        println 'starting bundle-messages'
        println "webui/bundle-messages.sh".execute().text
        println 'finished bundle-messages'
        // compile java files in build/messages-src
        ant.mkdir(dir: "$buildDir/compiledMessages")
        ant.javac(srcDir: "$buildDir/messages-src",
                    classPath : sourceSets.main.runtimeClasspath.asPath,
                    debug : false,
                    includeAntRuntime : false,
                    source: project.sourceCompatibility,
                    target: project.targetCompatibility,
                    destDir:file("$buildDir/compiledMessages"))
        // add resulting classes to build/libs/webui-(version).jar
        ant.jar(destfile: "$buildDir/libs/webui-${version}.jar",
                basedir: "$buildDir/compiledMessages",
                includes: '**/messages_*.class',
                update: 'true')
    }
}

// rebuild the english po file for uploading to transifex
task poupdate {
    doLast {
        // run bundle-messages.sh
        println 'starting bundle-messages -p'
        println "webui/bundle-messages.sh -p".execute().text
        println 'finished bundle-messages -p'
    }
}

precompileJsp.dependsOn compileJava
generateWebXML.dependsOn precompileJsp
bundle.dependsOn precompileJsp
poupdate.dependsOn precompileJsp
war.dependsOn generateWebXML, bundle

artifacts {
    warArtifact war
    jarArtifact jar
}
