def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY')
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
    apply plugin: 'com.android.application'
}
else {
    apply plugin: 'com.android.library'
}
apply plugin: 'kotlin-android'

android {
    if (buildAsApplication) {
        namespace "io.github.kichikuou.xsystem35"
    }
    compileSdkVersion 34
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 36
        versionName "2.18.0" + gitRevision()
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_APP_PLATFORM=android-19", "-DANDROID_STL=c++_static"
                // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
                abiFilters 'armeabi-v7a', 'arm64-v8a'
            }
        }
    }
    signingConfigs {
        release {
            storeFile rootProject.file('keystore.jks')
            storePassword System.getenv('KEYSTORE_PASSWORD')
            keyAlias System.getenv('KEY_ALIAS')
            keyPassword System.getenv('KEY_PASSWORD')
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (rootProject.file('keystore.jks').exists()) {
                signingConfig signingConfigs.release
            }
        }
    }
    applicationVariants.all { variant ->
        tasks["merge${variant.name.capitalize()}Assets"]
            .dependsOn("externalNativeBuild${variant.name.capitalize()}")
    }
    if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
        sourceSets.main {
            jniLibs.srcDir 'libs'
        }
        aaptOptions {
            // Disable asset compression for fonts.
            // Without this, text rendering will be very slow.
            noCompress 'ttf', 'otf'
        }
        externalNativeBuild {
            cmake {
                version '3.22.1'
                path 'jni/CMakeLists.txt'
            }
        }
       
    }
    lint {
        abortOnError false
    }

    if (buildAsLibrary) {
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith(".aar")) {
                    def fileName = "org.libsdl.app.aar"
                    output.outputFile = new File(outputFile.parent, fileName)
                }
            }
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
}

static String gitRevision() {
    ' (git ' + "git rev-parse --short HEAD".execute().text.trim() + ')'
}
