/*
 * Copyright (c) 2006-2017 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

task createVersionConfig {
    inputs.dir 'src'
    inputs.dir 'res'
    outputs.file new File(buildDir, 'version.config')
    dependsOn classes

    doLast {
        def targetFile = new File(buildDir, 'version.config')

        targetFile.text = ''
        targetFile << "keysections:\n identity\n version\n updater\n bundledplugins_versions\n\n"
        targetFile << "identity:\n name=DMDirc version information\n globaldefault=true\n order=95000\n\n"
        targetFile << "version:\n version=${version}\n\n"
        targetFile << "updater:\n channel=${updaterChannel}\n\n"
        targetFile << "buildenv:\n"
        def compileConfiguration = project.configurations.getByName("compile")
        def resolvedConfiguration = compileConfiguration.resolvedConfiguration
        def resolvedArtifacts = resolvedConfiguration.resolvedArtifacts
        resolvedArtifacts.each { dp ->
            def version = dp.moduleVersion.id
            targetFile << " " + version.group + " " + version.name + " " + version.version + "\n"
        }
    }
}

jar {
    outputs.file "dist/DMDirc.jar"
    dependsOn createVersionConfig

    exclude 'com/dmdirc/version.config'

    from("$buildDir/version.config") {
        into 'com/dmdirc/'
    }

    from { configurations.bundle.collect { it.isDirectory() ? it : zipTree(it) } } {
        exclude 'META-INF/*'
        exclude 'META-INF/maven/**'
    }

    manifest {
        attributes 'Main-Class': 'com.dmdirc.Main'
        attributes 'Bundle-Vendor': 'DMDirc Developers'
        attributes 'Bundle-Copyright': 'Copyright (c) DMDirc Developers 2006-2017'
        attributes 'Bundle-License': 'https://opensource.org/licenses/MIT'
        attributes 'Bundle-Activator': 'com.dmdirc.ClientActivator'
        attributes 'Import-Package': 'org.osgi.framework,javax.swing,javax.swing.text,javax.swing.text.html'
    }

    doLast {
        copy {
            from jar.archivePath
            into "dist/"
            rename ".*", "DMDirc.jar"
        }
    }
}
