/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

tasks.register('updateLicenses') {
    description = 'Updates the various LICENSE files'
    ext.licensesDir = "${projectDir}/licenses"
    ext.licenseBaseFile = "${licensesDir}/LICENSE-BASE"

    ext.licenseSrcFile = "${projectDir}/LICENSE"
    ext.licenseGroovyDocFile = "${projectDir}/subprojects/groovy-groovydoc/LICENSE"
    ext.licenseJsr223File = "${projectDir}/subprojects/groovy-jsr223/LICENSE"
    ext.licenseJLine3File = "${projectDir}/subprojects/groovy-groovysh/LICENSE"
    ext.licenseBinZipFile = "${licensesDir}/LICENSE-BINZIP"
    ext.licenseDocFile = "${licensesDir}/LICENSE-DOC"
    ext.licenseDocsJarFile = "${licensesDir}/LICENSE-DOCSJAR"
    ext.licenseJarJarFile = "${licensesDir}/LICENSE-JARJAR"
    ext.licenseSdkFile = "${licensesDir}/LICENSE-SDK"
    inputs.files(licenseBaseFile, fileTree(licensesDir).include('*.txt'))
    outputs.files(licenseBinZipFile, licenseDocFile, licenseDocsJarFile, licenseJarJarFile,
        licenseSrcFile, licenseGroovyDocFile, licenseJsr223File, licenseSdkFile)

    doLast {
        def srcFiles = fileTree(licensesDir).include('*-SRC*.txt').sort { it.name }
        def docFiles = fileTree(licensesDir).include('*-DOC*.txt').sort { it.name }
        def jarjarFiles = fileTree(licensesDir).include('*-JARJAR*.txt').sort { it.name }
        def binzipFiles = fileTree(licensesDir) {
            include '*-JARJAR*.txt'
            include '*-BINZIP*.txt'
        }.sort { it.name }
        def groovydocFiles = fileTree(licensesDir) {
            include 'normalize-stylesheet-groovy-groovydoc.txt'
            include 'prism-groovy-groovydoc.txt'
        }.sort { it.name }
        // for the generated-documentation (groovydoc classifier) jars, which
        // bundle the normalize-derived stylesheet and the prism assets
        def docsJarFiles = fileTree(licensesDir) {
            include 'normalize-stylesheet-BINZIP-DOC-SRC.txt'
            include 'prism-BINZIP-DOC-SRC.txt'
        }.sort { it.name }
        def jsr223Files = fileTree(licensesDir).include('jsr223-BINZIP-SRC.txt')
        def jline3Files = fileTree(licensesDir).include('jline3-BINZIP-SRC.txt')
        def licenseHdr = '\n\n------------------------------------------------------------------------\n\n'
        [
            (licenseBinZipFile)      : binzipFiles,
            (licenseDocFile)         : docFiles,
            (licenseDocsJarFile)     : docsJarFiles,
            (licenseJarJarFile)      : jarjarFiles,
            (licenseSrcFile)         : srcFiles,
            (licenseGroovyDocFile)   : groovydocFiles,
            (licenseJsr223File)      : jsr223Files,
            (licenseJLine3File)      : jline3Files,
        ].each { outFile, inFiles ->
            file(outFile).withWriter('utf-8') { writer ->
                writer << ([file(licenseBaseFile)] + inFiles).collect {
                    it.text.replaceAll(/[\n\r]*$/, '')
                }.join(licenseHdr) + '\n'
            }
        }
        file(licenseSdkFile).withWriter { writer ->
            writer << [
                file(licenseBinZipFile).text,
                "This convenience zip embeds Groovy's src and doc zips.\nSee also src/LICENSE " +
                    "and doc/LICENSE files for additional license information."
            ].join(licenseHdr) + '\n'
        }
    }
}

tasks.register('updateNotices') {
    description = 'Updates the various NOTICE files'
    ext.noticesDir = "${projectDir}/notices"
    ext.noticeBaseFile = "${noticesDir}/NOTICE-BASE"

    ext.noticeSrcFile = "${projectDir}/NOTICE"
    ext.noticeGroovyConsoleFile = "${projectDir}/subprojects/groovy-console/NOTICE"
    ext.noticeBinZipFile = "${noticesDir}/NOTICE-BINZIP"
    ext.noticeGrooidFile = "${noticesDir}/NOTICE-GROOID"
    ext.noticeGrooidJarJarFile = "${noticesDir}/NOTICE-GROOIDJARJAR"
    ext.noticeJarJarFile = "${noticesDir}/NOTICE-JARJAR"
    ext.noticeSdkFile = "${noticesDir}/NOTICE-SDK"
    inputs.files(noticeBaseFile, fileTree(noticesDir).include('*.txt'))
    outputs.files(noticeBinZipFile, noticeGrooidFile, noticeGrooidJarJarFile,
        noticeJarJarFile, noticeSrcFile, noticeGroovyConsoleFile, noticeSdkFile)

    doLast {
        def srcFiles = fileTree(noticesDir).include('*-SRC*.txt').sort { it.name }
        def grooidFiles = fileTree(noticesDir).include('*-GROOID*.txt').sort { it.name }
        def jarjarFiles = fileTree(noticesDir).include('*-JARJAR*.txt').sort { it.name }
        def grooidJarjarFiles = fileTree(noticesDir) {
            include '*-JARJAR*.txt'
            include '*-GROOID*.txt'
        }.sort { it.name }
        def binzipFiles = fileTree(noticesDir) {
            include '*-JARJAR*.txt'
            include '*-GROOID*.txt'
            include '*-BINZIP*.txt'
        }.sort { it.name }
        [
            (noticeBinZipFile)       : binzipFiles,
            (noticeGrooidFile)       : grooidFiles,
            (noticeGrooidJarJarFile) : grooidJarjarFiles,
            (noticeJarJarFile)       : jarjarFiles,
            (noticeSrcFile)          : srcFiles,
            (noticeGroovyConsoleFile): files(),
        ].each { outFile, inFiles ->
            file(outFile).withWriter('utf-8') { writer ->
                writer << ([file(noticeBaseFile)] + inFiles).collect {
                    it.text.replaceAll(/[\n\r]*$/, '')
                }.join('\n\n')
            }
        }
        file(noticeSdkFile).withWriter { writer ->
            writer << [
                file(noticeBinZipFile).text,
                "This convenience zip embeds Groovy's src and doc zips.\nSee also src/NOTICE " +
                    "and doc/NOTICE files for additional notice information."
            ].join('\n\n')
        }
    }
}
