import java.util.function.BiConsumer
import java.util.stream.Stream
import java.nio.file.Files

System.setProperty("file.encoding", "UTF-8")

def logFile = new File(buildDir.absoluteFile,"build-logging/build-log-${System.currentTimeMillis()}.log")
gradle.addBuildListener(new BuildLogger(logFile))

buildscript {
    repositories {
        google()
        mavenLocal()
        maven { url 'https://repo1.maven.org/maven2' /*maven-central with HTTPS*/ }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.13.0'
        classpath 'com.github.triplet.gradle:play-publisher:3.7.0'
        classpath 'net.ltgt.gradle:gradle-errorprone-plugin:4.3.0'
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.9.3'
        classpath 'com.diffplug.spotless:spotless-plugin-gradle:7.2.1'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.0'
    }
}

allprojects {
    repositories {
        google()
        mavenLocal()
        gradlePluginPortal()
        maven { url 'https://repo1.maven.org/maven2' /*maven-central with HTTPS*/ }
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    }
}

apply from: "${rootDir}/gradle/root_all_projects_ext.gradle"
apply from: "${rootDir}/gradle/fdroid_yaml_output.gradle"

def cleanOthers = tasks.register("cleanOthers", Delete) {
    delete buildDir
    //we do not want to delete the current log file
    logFile.parentFile.listFiles().each {
        if (it != null && it.absolutePath != logFile.absolutePath) {
            delete it
        }
    }

    //deleting all beta/production change-logs
    Stream<Path> files = Files.walk(rootDir.toPath())
    files.sorted(Comparator.reverseOrder())
            .map { it.toFile() }
            .filter { it.name == 'beta.txt' || it.name == 'production.txt' }
            .forEach { it.deleteOnExit() }
    files.close()
}

tasks.register('clean').configure({
    it.finalizedBy(cleanOthers)
})
