/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * Licensed 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.
 */

// Upload to Bintray
apply plugin: 'com.jfrog.bintray'

def getBintrayUsername() {
    return hasProperty('bintrayUsername') ? property('bintrayUsername') : System.getenv('BINTRAY_USERNAME')
}

def getBintrayApiKey() {
    return hasProperty('bintrayApiKey') ? property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')
}

def getBintrayGpgPassword() {
    return hasProperty('bintrayGpgPassword') ? property('bintrayGpgPassword') : System.getenv('BINTRAY_GPG_PASSWORD')
}

def getMavenCentralUsername() {
    return hasProperty('mavenCentralUsername') ? property('mavenCentralUsername') : System.getenv('MAVEN_CENTRAL_USERNAME')
}

def getMavenCentralPassword() {
    return hasProperty('mavenCentralPassword') ? property('mavenCentralPassword') : System.getenv('MAVEN_CENTRAL_PASSWORD')
}

def shouldSyncWithMavenCentral() {
    return hasProperty('syncWithMavenCentral') ? property('syncWithMavenCentral').toBoolean() : false
}

bintray {
    user = getBintrayUsername()
    key = getBintrayApiKey()
    configurations = ['archives']
    pkg {
        repo = bintrayRepo
        userOrg =  bintrayUserOrg
        name = bintrayName
        desc = bintrayDescription
        websiteUrl = projectUrl
        issueTrackerUrl = issuesUrl
        vcsUrl = scmUrl
        licenses = projectLicenses
        publish = true
        publicDownloadNumbers = true
        version {
            desc = bintrayDescription
            gpg {
                sign = true
                passphrase = getBintrayGpgPassword()
            }
            mavenCentralSync {
                sync = shouldSyncWithMavenCentral()
                user = getMavenCentralUsername()
                password = getMavenCentralPassword()
                close = '1' // If set to 0, you have to manually click release
            }
        }
    }
}
