plugins {
    id 'java-library'
}

apply plugin: 'application'

application {
    mainClassName='net.i2p.router.RouterLaunch'
    applicationName='i2p'
}

dependencies {
    api project(':router')
    api project(':apps:ministreaming')
    api project(':apps:streaming')
    api project(':apps:i2ptunnel')
    api project(':apps:jetty')
    api project(':apps:i2psnark')
    api project(':apps:systray')
    api project(':apps:BOB')
    api project(':apps:sam')
    api project(':apps:routerconsole')
    api project(':apps:desktopgui')
    api project(':apps:jrobin')
    api project(':apps:addressbook')
    api project(':apps:susidns')
    api project(':apps:susimail')
    api project(':apps:i2pcontrol')
    api project(':apps:imagegen')
    api project(':core')
    api project(':router')
    api project(path : ':installer', configuration: 'jbigi')
    api files("../apps/susidns/src/lib/standard.jar")
    api files("../apps/susidns/src/lib/jstl.jar")
}

import java.nio.file.*
import java.util.zip.*

def projects = [':apps:routerconsole',
                ':apps:addressbook',
                ':apps:i2psnark',
                ':apps:imagegen',
                ':apps:i2pcontrol',
                ':apps:susidns',
                ':apps:susimail',
                ':apps:i2ptunnel'].collect {project(it)}

def rootDir = project.getRootProject().getRootDir()
def buildDir = new File("$buildDir")
def webappDir = new File(buildDir, 'webapps')
def geoipDir = new File(buildDir, "geoip")

task copyWars {
    doLast {
        webappDir.mkdirs()
        projects.each { p ->
            p.configurations.archives.getArtifacts().getFiles().
                filter({it.getName().endsWith('war')}).each { file ->
                println "copying war $file exists ${file.exists()}"
                File target = new File(webappDir, file.getName())
                Files.copy(file.toPath(), target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING)
            }
        } 
    }
}

projects.each { p ->
    copyWars.dependsOn p.tasks['assemble']    
}

task copyGeoip() {
    doLast {
        geoipDir.mkdirs()
        File target = new File(geoipDir, "GeoLite2-Country.mmdb")
        File source = new File("$rootDir/installer/resources/GeoLite2-Country.mmdb.gz")
        InputStream is = new GZIPInputStream(new FileInputStream(source))
        java.nio.file.Files.copy(is, target.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING)
    }
}

jar.dependsOn copyWars,copyGeoip

distributions {
    main {
        contents {
            from('../installer/resources/') {
                exclude '*.gz*'
            }
            from(webappDir.getAbsolutePath()) {
                into 'webapps'
            }
            from(geoipDir.getAbsolutePath()) {
                into 'geoip'
            }
        }
    }
}
