plugins {
    id("de.fayard.refreshVersions") version "0.60.5"
}

rootDir.eachFile(groovy.io.FileType.DIRECTORIES) { File parent ->

    String[] ignoreFolders = ["buildSrc", "fastlane", "submodules", "node_modules", "gradle", "build", ".maestro"]
    if (!parent.name.startsWith(".") && !ignoreFolders.contains(parent.name)) {
        Boolean shouldAddProject = false
        parent.eachFile {
            if (it.name.contains("build.gradle")) {
                shouldAddProject = true
                return
            }
        }
        if (shouldAddProject) {
            include ":${parent.name}"
        } else {
            parent.eachFile(groovy.io.FileType.DIRECTORIES) { child -> // We only one level deep
                Boolean shouldAddSubProject = false
                child.eachFile {
                    if (it.name.contains("build.gradle")) {
                        shouldAddSubProject = true
                        return
                    }
                }
                if (shouldAddSubProject) {
                    include ":${child.name}"; project(":${child.name}").projectDir = new File("${parent.name}/${child.name}")
                }
            }
        }
    }
}

rootProject.children.each { subproject ->

    if (subproject.name == "vpn") {
        subproject.buildFileName = "${subproject.name}-build.gradle"
    }
}


buildCache {

    def getFile = { dir, filename ->
        File file = new File("$dir$File.separator$filename")
        file?.exists() ? file : null
    }

    def getLocalProperties = { dir ->
        def file = getFile(dir, "local.properties")
        if (!file) {
            return null
        }

        Properties properties = new Properties()
        properties.load(file.newInputStream())
        return properties
    }

    local {
        def properties = getLocalProperties(rootDir)
        if (properties != null) {
            enabled = "true" == properties.getProperty("local.build.cache", "true")
        } else {
            enabled = true
        }
        // configure local build cache directory so that it is local to the project dir
        directory = new File(rootDir, 'build-cache')
        removeUnusedEntriesAfterDays = 7
    }
}
