/*#######################################################
 *
 *   Maintained by Gregor Santner, 2017-
 *   https://gsantner.net/
 *
 *   License of this file: Apache 2.0 (Commercial upon request)
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
#########################################################*/
// Top-level build file where you can add configuration options common to all sub-projects/modules.

import java.text.SimpleDateFormat

buildscript {
    ext {
        version_gradle_tools = "3.2.1"
        version_plugin_kotlin = "1.3.11"
        enable_plugin_kotlin = false

        version_compileSdk = 28
        version_buildTools = "28.0.3"
        version_minSdk = 17

        // https://developer.android.com/topic/libraries/support-library/
        version_library_appcompat = "28.0.0" //androidx
        // https://github.com/JakeWharton/butterknife/releases
        version_library_butterknife = "8.8.1" //9.0.0-rc2
        // https://github.com/guardianproject/NetCipher/releases
        version_library_netcipher = "2.0.0-alpha1"
    }

    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral()
    }

    dependencies {
        classpath "com.android.tools.build:gradle:${version_gradle_tools}"
        classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
        if (project.enable_plugin_kotlin) {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${version_plugin_kotlin}"
        }

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url 'https://maven.google.com' }
        jcenter()
        maven { url "https://jitpack.io" }
        mavenCentral()
    }

    tasks.matching { task -> task.name.matches('.*generate.*Resources') }.all {
        task -> task.dependsOn copyRepoFiles
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

final String[] ROOT_TO_RAW_COPYFILES = ["README.md", "CHANGELOG.md", "CONTRIBUTORS.md", "LICENSE.txt", "LICENSE.md", "LICENSE"]
task copyRepoFiles(type: Copy) {
    from rootProject.files(ROOT_TO_RAW_COPYFILES)
    into "app/src/main/res/raw"
    rename { String fileName -> fileName.replace(fileName, fileName.toLowerCase()) }
}


@SuppressWarnings(["UnnecessaryQualifiedReference", "SpellCheckingInspection", "GroovyUnusedDeclaration"])
// Returns used android languages as a buildConfig array: {'de', 'it', ..}"
static String findUsedAndroidLocales() {
    Set<String> langs = new HashSet<>()
    new File('.').eachFileRecurse(groovy.io.FileType.DIRECTORIES) {
        final foldername = it.name
        if (foldername.startsWith('values-') && !it.canonicalPath.contains("build" + File.separator + "intermediates")) {
            new File(it.toString()).eachFileRecurse(groovy.io.FileType.FILES) {
                if (it.name.toLowerCase().endsWith(".xml") && it.getCanonicalFile().getText('UTF-8').contains("<string")) {
                    langs.add(foldername.replace("values-", ""))
                }
            }
        }
    }
    return '{' + langs.collect { "\"${it}\"" }.join(",") + '}'
}

ext.getGitHash = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', 'HEAD'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    } catch (Exception ignored) {
        return 'unknown'
    }
}

@SuppressWarnings(["UnnecessaryQualifiedReference", "SpellCheckingInspection", "GroovyUnusedDeclaration"])
// Returns the build date in a RFC3339 compatible format. TZ is always converted to UTC
static String getBuildDate() {
    final SimpleDateFormat RFC3339_LIKE = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")
    RFC3339_LIKE.setTimeZone(TimeZone.getTimeZone("UTC"))
    return RFC3339_LIKE.format(new Date())
}