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

import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.locks.ReentrantLock

// Set group for Maven
allprojects {
    if (!project.hasProperty('group') || project.group.length() == 0) {
        project.ext.group = 'org.apache.gobblin'
        project.group = project.ext.group
    }
}

// Nexus publishing
ext.buildProperties.ensureDefined('publishToNexus')
if (ext.publishToNexus) {
    plugins.apply('maven')
    // Workaround for a bug in gradle's "maven" plugin. See https://discuss.gradle.org/t/error-in-parallel-build/7215/3
    project.setProperty("org.gradle.parallel", "false")
}

ext.signArtifacts = !project.doNotSignArtifacts

// Maven POM generation is not thread safe, so serialize all the Upload tasks we can use `--parallel`.
// https://issues.gradle.org/browse/GRADLE-2492
// When we start building with 2.3 and later we should remove this and just add a common output dir for all tasks and let Gradle serialize them
def lock = new ReentrantLock()
def available = lock.newCondition()
def busy = new AtomicBoolean()
def serializedTasks = []
allprojects {
    tasks.matching { it.name == "generatePom" || it instanceof Upload }.all {
        serializedTasks << it
        doFirst {
            lock.lock()
            while (busy.get()) {
                available.await()
            }
            busy.set(true)
        }
    }
}

gradle.taskGraph.afterTask {
    if (it in serializedTasks && lock.heldByCurrentThread) {
        busy.set(false)
        available.signal()
        lock.unlock()
    }
}

ext.pomAttributes = {
    packaging 'jar'
    // optionally artifactId can be defined here
    name 'Apache Gobblin (incubating)'
    description 'A distributed data integration framework for streaming and batch data ecosystems.'
    url 'https://gobblin.apache.org/'

    scm {
        url 'https://git-wip-us.apache.org/repos/asf?p=incubator-gobblin.git;a=tree'
        connection 'scm:http://git-wip-us.apache.org/repos/asf/incubator-gobblin.git'
        developerConnection 'scm:https://git-wip-us.apache.org/repos/asf/incubator-gobblin.git'
    }

    licenses {
        license {
            name 'The Apache License, Version 2.0'
            url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        }
    }
}

subprojects {
    plugins.withType(JavaPlugin) {
        plugins.apply('maven')

        install {
            repositories {
                mavenInstaller {
                    mavenLocal()
                    pom.project {
                        packaging 'jar'
                        name 'Apache Gobblin (incubating)'
                        description 'A distributed data integration framework for streaming and batch data ecosystems.'
                        url 'https://gobblin.apache.org/'
                    }
                }
            }
        }

        // Publishing of maven artifacts for subprojects
        if (rootProject.ext.publishToNexus) {
            if (rootProject.ext.signArtifacts) {
                plugins.apply('signing')
            }

            uploadArchives {
                repositories {
                    mavenDeployer {
                        beforeDeployment { MavenDeployment deployment ->
                            if (rootProject.ext.signArtifacts) {
                                signing.signPom(deployment)
                            }
                        }

                        repository(url: rootProject.nexusArtifactRepository) {
                            authentication(userName: rootProject.ext.nexusUsername, password: rootProject.ext.nexusPassword)
                        }

                        snapshotRepository(url: rootProject.nexusArtifactSnapshotRepository) {
                            authentication(userName: rootProject.ext.nexusUsername, password: rootProject.ext.nexusPassword)
                        }

                        pom.project pomAttributes
                    }
                }
            }

            if (rootProject.ext.signArtifacts) {
                signing {
                    sign configurations.archives
                }
            }
        }
    }
}
