task buildAll << {
    File dstDir = new File(buildDir, "all")
    dstDir.mkdirs()
    
    copy {
        into new File(dstDir, "svnkit-${version}")
        into('lib') {
            from configurations.binaries.files
            exclude '**/*.asc'
            eachFile fullJarNameExpander
            
        }
        into ('bin') {
            from configurations.scripts.files
            exclude '**/logging.properties'
        } 
        into('src') {
            from configurations.sources.files
        }
        into('licenses') {
            from file('src/main/licenses')
        }
        into ('conf') {
            from configurations.scripts.files
            include '**/logging.properties'
            rename { String filename -> return filename + '.disabled' }            
        }
        
        from rootProject.files('LICENSE.txt', 'README.txt', 'CHANGES.txt')
    }
    
    File destfile = new File(distsDir, "org.tmatesoft.svn_${version}.standalone.zip")
    ant.zip(destfile: destfile.absolutePath) {
        zipfileset(dir: dstDir.absolutePath, excludes: '**/bin/**')
        zipfileset(dir: dstDir.absolutePath, includes: '**/bin/**', excludes: '**/bin/*.bat', filemode: '755')
        zipfileset(dir: dstDir.absolutePath, includes: '**/bin/*.bat')
    }
    File destfileNoJNA = new File(distsDir, "org.tmatesoft.svn_${version}.standalone.nojna.zip")
    ant.zip(destfile: destfileNoJNA.absolutePath) {
        zipfileset(dir: dstDir.absolutePath, excludes: '**/bin/**,**/jna*,**/platform*,**/jsch*,**/LICENSE-JNA.txt,**/LICENSE-JSCH.txt')
        zipfileset(dir: dstDir.absolutePath, includes: '**/bin/**', excludes: '**/bin/*.bat', filemode: '755')
        zipfileset(dir: dstDir.absolutePath, includes: '**/bin/*.bat')
    }
}

task buildSources(type: Zip) {
    archiveName = "org.tmatesoft.svn_${version}.src.zip"

    into("svnkit-${version}")

    from rootProject.rootDir

    exclude '.*'
    exclude '**/.*'
    exclude '**/.*/**'

    exclude '**/build/**'
    exclude '**/bin/**'
    exclude '**/target/**'
}

task buildUpdateSite << {
    def tokens = new HashMap()
    File siteDir = new File(buildDir, 'site')
    File pluginsDir = new File(siteDir, 'plugins')
    File featuresDir = new File(siteDir, 'features')
    pluginsDir.mkdirs()
    
    configurations.osgi.resolvedConfiguration.each { conf ->
        conf.resolvedArtifacts.each { artifact ->
            if (artifact.file.name.endsWith('.zip') || artifact.file.name.endsWith('.jar')) {
                org.tmatesoft.build.BuildVersion fileVersion = org.tmatesoft.build.BuildVersion.fromJarFile(artifact.file)
                String name = artifact.getModuleVersion().getId().getName()
                name = name.replace('-', '_')
                tokens[name + '_version'] = fileVersion.bundleVersion

                def pattern = java.util.regex.Pattern.compile("\\d+\\.\\d+\\.\\d+_.+")
                def matcher = pattern.matcher(tokens[name + '_version'])
                if (matcher.matches()) {
                    tokens[name + '_version'] = tokens[name + '_version'].replaceFirst("_", ".")
                }
                tokens[name + '_name'] = fileVersion.bundleSymbolicName
                tokens[name + '_fullName'] = tokens[name + '_name'] + '_' + tokens[name + '_version'] + '.jar'
                //fullJarName(artifact.file) 
                copy {
                   from artifact.file
                   into pluginsDir
                   eachFile fullJarNameExpander
                }
            }
        }
    }
    copy {
        into siteDir
        from 'src/main/update-site'
        include 'site.xml'
        expand(tokens)
    }
    file('src/main/update-site/features').listFiles().each {
        if (it.isDirectory()) {
            String fullName = tokens[it.name.replace('-', '_') + '_fullName']
            if (fullName != null) {
                makeFeatureJar(it, new File(featuresDir, fullName), tokens)
            }
        }
    }

    distsDir.mkdirs()
    File destfile = new File(distsDir, "org.tmatesoft.svn_${version}.eclipse.zip")
    ant.zip(destfile: destfile.absolutePath, basedir: siteDir.absolutePath)
}

def makeFeatureJar(File dir, File dstFile, Map tokens) {
    def tmpDir = new File(buildDir, 'tmp/' + dstFile.getName())
    tmpDir.mkdirs()
    copy {
        into tmpDir
        from dir
        expand(tokens)
    }
    ant.jar(destfile: dstFile.absolutePath, basedir: tmpDir.absolutePath)
    
}

buildAll.dependsOn tasks.clean
buildAll.dependsOn configurations.binaries
buildAll.dependsOn configurations.scripts
buildAll.dependsOn configurations.sources

buildUpdateSite.dependsOn configurations.osgi

task build(dependsOn: [buildAll, buildUpdateSite, buildSources])
