/*
 * 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.
 */

configurations {
    plugin {
        transitive = false
    }
}

dependencies {
    plugin group: 'com.dmdirc', name: 'ui_swing', version: '+', changing: true
    plugin group: 'com.dmdirc', name: 'parser_irc', version: '+', changing: true
    plugin group: 'com.dmdirc', name: 'tabcompletion_bash', version: '+', changing: true
    plugin group: 'com.dmdirc', name: 'tabcompletion_mirc', version: '+', changing: true
}

task createFatVersionConfig {
    inputs.file new File(buildDir, 'version.config')
    outputs.file new File(buildDir, 'fat.version.config')
    dependsOn createVersionConfig

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

        java.nio.file.Files.copy(sourceFile.toPath(), targetFile.toPath(),
                java.nio.file.StandardCopyOption.REPLACE_EXISTING)

        targetFile << "\nbundledplugins_versions:\n"
        configurations.plugin.each { p ->
            targetFile << " " + p.name.replaceFirst('-', '=').replaceAll('.jar$', '\n')
        }
    }
}

task('fatjar', type: Jar) {
    dependsOn createFatVersionConfig
    baseName = 'fat-client'

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

    into('plugins') {
        from configurations.plugin
        rename '(.*?)-.*(\\.jar)', '$1$2'
    }

    with jar {
        exclude 'com/dmdirc/version.config'
    }

    manifest {
        attributes 'Main-Class': 'com.dmdirc.Main'
    }

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