/*
 * Copyright Consensys Software Inc.
 *
 * This file is dual-licensed under either the MIT license or Apache License 2.0.
 * See the LICENSE-MIT and LICENSE-APACHE files in the repository root for details.
 *
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

import groovy.io.FileType
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.tooling.BuildException

class CheckSpdxHeader extends DefaultTask {
    private String rootPath
    private String spdxHeader
    private String filesRegex
    private String excludeRegex

    @Input
    String getRootPath() {
        return rootPath
    }

    void setRootPath(final String rootPath) {
        this.rootPath = rootPath
    }

    @Input
    String getSpdxHeader() {
        return spdxHeader
    }

    void setSpdxHeader(final String spdxHeader) {
        this.spdxHeader = spdxHeader
    }

    @Input
    String getFilesRegex() {
        return filesRegex
    }

    void setFilesRegex(final String filesRegex) {
        this.filesRegex = filesRegex
    }

    @Input
    String getExcludeRegex() {
        return excludeRegex
    }

    void setExcludeRegex(final String excludeRegex) {
        this.excludeRegex = excludeRegex
    }

    @TaskAction
    void checkHeaders() {
        def filesWithoutHeader = []

        new File(rootPath).traverse(
                type: FileType.FILES,
                nameFilter: ~/${filesRegex}/,
                excludeFilter: ~/${excludeRegex}/
        ) {
            f ->
                if (!f.getText().contains(spdxHeader)) {
                    filesWithoutHeader.add(f)
                }
        }

        if (!filesWithoutHeader.isEmpty()) {
            throw new BuildException("Files without headers: " + filesWithoutHeader.join('\n'), null)
        }
    }
}

javadoc {
  options.addStringOption('Xdoclint:all', '-quiet')
  options.addStringOption('Xwerror', '-html5')
  options.encoding = 'UTF-8'
}

task checkSpdxHeader(type: CheckSpdxHeader) {
  rootPath = "${projectDir}"
  spdxHeader = "* SPDX-License-Identifier: MIT OR Apache-2.0"
  filesRegex = "(.*.java)|(.*.groovy)"
  excludeRegex = [
    "(.*/.gradle/.*)",
    "(.*/.idea/.*)",
    "(.*/out/.*)",
    "(.*/build/.*)",
    "(.*/src/[^/]+/generated/.*)",
  ].join("|")
}

tasks.check.dependsOn(checkSpdxHeader)
