import groovy.text.SimpleTemplateEngine

buildscript {
    repositories {
        mavenCentral()
        google()
    }
    dependencies {
        def gradleToolsVersion = "8.7.2"
        classpath "com.android.tools.build:gradle:${gradleToolsVersion}"
    }
}

plugins {
    id('maven-publish')
    id('signing')
    id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

allprojects {
    repositories {
        mavenCentral()
        google()
    }
    tasks.withType(JavaCompile).tap {
        configureEach {
            options.compilerArgs << "-Xlint:deprecation"
        }
    }
}

project.ext {
    if(project.hasProperty('sqlcipherAndroidVersion') && "${sqlcipherAndroidVersion}") {
        libraryVersion = "${sqlcipherAndroidVersion}"
    } else {
        libraryVersion = "undefined"
    }
    minSdkVersion = 21
    androidXSQLiteVersion = "2.2.0"
    roomVersion = "2.5.0"
    androidNdkVersion = "25.2.9519653"
    mavenLocalRepositoryPrefix = "file://"
    if(project.hasProperty('publishLocal') && publishLocal.toBoolean()){
        mavenSnapshotRepositoryUrl = "outputs/snapshot"
        mavenReleaseRepositoryUrl = "outputs/release"
    } else {
        mavenLocalRepositoryPrefix = ""
        mavenSnapshotRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots"
        mavenReleaseRepositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
    }
    if(project.hasProperty('publishSnapshot') && publishSnapshot.toBoolean()){
        mavenVersionName = "${libraryVersion}-SNAPSHOT"
    } else {
        mavenVersionName = "${libraryVersion}"
    }
    if(project.hasProperty('nexusUsername')){
        nexusUsername = "${nexusUsername}"
    }
    if(project.hasProperty('nexusPassword')){
        nexusPassword = "${nexusPassword}"
    }
    nexusUsername = project.hasProperty('nexusUsername') ? "${nexusUsername}" : ""
    nexusPassword = project.hasProperty('nexusPassword') ? "${nexusPassword}" : ""
    nexusStagingProfileId = project.hasProperty('nexusStagingProfileId') ? "${nexusStagingProfileId}" : ""
}

task generateReadMe {
    def readme = new File("README.md")
    def engine = new SimpleTemplateEngine()
    def reader = new FileReader("README.md.template")
    def binding = [
                    libraryVersion: project.ext.libraryVersion,
                    androidXSQLiteVersion: project.ext.androidXSQLiteVersion,
                    androidNdkVersion: project.ext.androidNdkVersion,
                    minSdkVersion: project.ext.minSdkVersion,
                  ]
    def template = engine.createTemplate(reader).make(binding)
    readme.write(template.toString())
}

project.afterEvaluate {
    build.dependsOn generateReadMe
}
