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

def acceptFile = new File(gradle.gradleUserHomeDir, "gradle-scans-license-agree.txt")
def env = System.getenv()
boolean isCI = env.CI || env.TRAVIS || env.TEAMCITY_VERSION
boolean hasAccepted = isCI || env.GRADLE_SCANS_ACCEPT=='yes' || acceptFile.exists() && acceptFile.text.trim() == 'yes'
boolean hasRefused = env.GRADLE_SCANS_ACCEPT=='no' || acceptFile.exists() && acceptFile.text.trim() == 'no'

buildScan {
    if (hasAccepted) {
        termsOfServiceAgree = 'yes'
        publishAlways()
    } else if (!hasRefused) {
        gradle.buildFinished {
            println """
This build uses Gradle Build Scans to gather statistics, share information about 
failures, environmental issues, dependencies resolved during the build and more.
Build scans will be published after each build, if you accept the terms of 
service, and in particular the privacy policy.

Please read 
   
    https://gradle.com/terms-of-service 
    https://gradle.com/legal/privacy

and then:

  - set the `GRADLE_SCANS_ACCEPT` to `yes`/`no` if you agree with/refuse the TOS
  - or create the ${acceptFile} file with `yes`/`no` in it if you agree with/refuse

And we'll not bother you again. Note that build scans are only made public if 
you share the URL at the end of the build.
"""
        }
    }
}