/*
 *  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 org.gradle.api.internal.artifacts.publish.DefaultPublishArtifact

ext.bintrayUser = project.hasProperty('bintrayUser') ? project.bintrayUser : System.getenv('BINTRAY_USER')
ext.bintrayPassword = project.hasProperty('bintrayKey') ? project.bintrayKey : System.getenv('BINTRAY_KEY')

if (!bintrayUser) {
    // try to read from properties
    def bintrayFile = file('bintray.properties')
    if (bintrayFile.exists()) {
        def props = new Properties()
        props.load(bintrayFile.newReader())
        ext.bintrayUser = props.getProperty('bintrayUser','')
        ext.bintrayPassword = props.getProperty('bintrayPassword','')
    }
}

allprojects {
    apply plugin: 'artifactory'

    artifactory {
        contextUrl = 'https://oss.jfrog.org'
        resolve {
            repository {
                repoKey = 'libs-release'
            }
        }
        publish {
            repository {
                repoKey = 'oss-snapshot-local' //The Artifactory repository key to publish to
                //when using oss.jfrog.org the credentials are from Bintray. For local build we expect them to be found in
                //~/.gradle/gradle.properties, otherwise to be set in the build server
                username = rootProject.bintrayUser
                password = rootProject.bintrayPassword
            }
        }
    }
}

artifactoryPublish {
    mavenDescriptor = new File("$projectDir/target/poms/pom-groovy.xml")
    doFirst{
        def curDate = new Date()
        def additionalFiles = [
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "jar", "jar", null,
                        curDate, file("$projectDir/target/libs/groovy-all-${version}.jar")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}.jar"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "jar", "jar", 'sources',
                        curDate, file("$projectDir/target/libs/groovy-all-${version}-sources.jar")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}-sources.jar"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "jar", "jar", 'javadoc',
                        curDate, file("$projectDir/target/libs/groovy-all-${version}-javadoc.jar")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}-javadoc.jar"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "jar", "jar", 'groovydoc',
                        curDate, file("$projectDir/target/libs/groovy-all-${version}-groovydoc.jar")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}-groovydoc.jar"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "jar", "jar", 'indy',
                        curDate, file("$projectDir/target/libs/groovy-all-${version}-indy.jar")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}-indy.jar"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-all", "pom", "pom", null,
                        curDate, file("$projectDir/target/poms/pom-all.xml")), 'artifacts', "org/codehaus/groovy/groovy-all/${version}/groovy-all-${version}.pom"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-binary", "zip", "zip", null,
                        curDate, distBin.archivePath), 'artifacts', "org/codehaus/groovy/groovy-binary/${version}/groovy-binary-${version}.zip"),
                gradleDeployDetails(new DefaultPublishArtifact("groovy-binary", "pom", "pom", null,
                        curDate, file("$projectDir/target/poms/pom-binary.xml")), 'artifacts', "org/codehaus/groovy/groovy-binary/${version}/groovy-binary-${version}.pom"),

        ]
        tasks.withType(Jar).matching { it.name.startsWith('backport') }.all {
            additionalFiles << gradleDeployDetails(
                    new DefaultPublishArtifact(baseName, "jar", "jar", classifier, curDate, archivePath),
                    'artifacts',
                    "org/codehaus/groovy/${baseName}/${version}/${archiveName}")
            if (!classifier) {
                additionalFiles << gradleDeployDetails(
                        new DefaultPublishArtifact(baseName, "pom", "pom", null, curDate, file("$projectDir/target/poms/pom-${baseName - 'groovy-'}.xml")),
                        'artifacts',
                        "org/codehaus/groovy/${baseName}/${version}/${baseName}-${version}.pom")
            }
        }
        additionalFiles.each { deployDetails.add(it) }
    }
}

artifactoryPublish.dependsOn('backportJars')