/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * 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.
 */

import org.gradle.internal.os.OperatingSystem

buildscript {
    repositories {
        maven { url '../../prebuilts/gradle-plugin' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

apply from: 'version.gradle'

String platform = OperatingSystem.current().isMacOsX() ? 'darwin' : 'linux'
File fullSdkPath = file("${rootDir}/../../prebuilts/fullsdk-${platform}")
if (!fullSdkPath.exists()) {
    throw GradleException("missing fullsdk")
}

gradle.ext.currentSdk = 28
ext.buildToolsVersion = '27.0.3'
project.ext.fullSdkPath = fullSdkPath
project.ext.androidJar = files("${fullSdkPath}/platforms/android-${gradle.currentSdk}/android.jar")

File props = file("local.properties")
props.write "sdk.dir=${fullSdkPath.getAbsolutePath()}"

/*
 * With the build server you are given two env variables.
 * The OUT_DIR is a temporary directory you can use to put things during the build.
 * The DIST_DIR is where you want to save things from the build.
 *
 * The build server will copy the contents of DIST_DIR to somewhere and make it available.
 */
if (System.env.DIST_DIR != null && System.env.OUT_DIR != null) {
    buildDir = new File(System.env.OUT_DIR + '/gradle/frameworks/support/build').getCanonicalFile()
    project.ext.distDir = new File(System.env.DIST_DIR).getCanonicalFile()
} else {
    buildDir = file('../../out/host/gradle/frameworks/support/build')
    project.ext.distDir = file('../../out/dist')
}

ext.supportRepoOut = new File(buildDir, 'support_repo')

// upload anchor for subprojects to upload their artifacts
// to the local repo.
task dist(type: Zip)  {
    group = BasePlugin.BUILD_GROUP
    description 'Builds distribution artifacts.'

    from project.ext.supportRepoOut
    into 'm2repository'
    destinationDir project.ext.distDir
    baseName = String.format("top-of-tree-m2repository-%s", project.multidexVersion)

    doLast {
        logger.warn "Compressed maven artifacts to ${archivePath}"
    }
}

subprojects {
    // Change buildDir first so that all plugins pick up the new value.
    project.buildDir = project.file("${project.parent.buildDir}/../${project.name}/build")

    apply plugin: 'maven'

    version = rootProject.multidexVersion
    group = 'androidx.multidex'

    dist.dependsOn project.tasks.uploadArchives

    project.plugins.whenPluginAdded { plugin ->
        if ("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
            project.android.buildToolsVersion = rootProject.buildToolsVersion
        }
    }
}