configurations {
    scripts
}

project.ext.packageName = 'org.tmatesoft.svn.cli'

project.ext.scripts = ['jsvn' : "${packageName}.SVN",  
           'jsvnadmin' : "${packageName}.SVNAdmin", 
           'jsvndumpfilter' : "${packageName}.SVNDumpFilter", 
           'jsvnlook' : "${packageName}.SVNLook", 
           'jsvnsync' : "${packageName}.SVNSync", 
           'jsvnversion' : "${packageName}.SVNVersion"]
 
jar << {
    
    File scriptsDir = new File(buildDir, 'scripts')
    scriptsDir.mkdirs()
    
    def classpath = []
    configurations.compile.files.each {
        classpath += fullJarName(it)
    }
    classpath += jar.archiveName
    
    scripts.keySet().each { scriptName ->
        def mainclassName = scripts[scriptName]
        copy {
            from file("src/main/scripts/jsvn")
            into scriptsDir
            filter {
                it = expandClasspath(it, classpath)
                it = it.replace('@mainclass@', mainclassName)
            }
            eachFile {
                it.name = scriptName
            }
        }
        copy {
            from file("src/main/scripts/jsvn.bat")
            into scriptsDir
            filter {
                it = expandClasspath(it, classpath)
                it = it.replace('@mainclass@', mainclassName)
            }
            eachFile {
                it.name = scriptName + '.bat'
            }
        }
    }
    
    copy {
        from file("src/main/scripts/jsvnsetup.openvms")
        into scriptsDir
        filter {
            it = expandClasspath(it, classpath)
        }
    }
    copy {
        from file("src/main/conf/logging.properties")
        into scriptsDir
    }

    configurations.scripts.artifacts.add(new
        org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact("svn-cli-scripts", "dir", "dir", null, new  Date(), scriptsDir))
}