plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

static def gitVersionCode() {
    def cmd = 'git rev-list HEAD --first-parent --count'
    cmd.execute().text.trim().toInteger()
}

static def gitVersionTag() {
    def cmd = 'git describe --tags'
    def version = cmd.execute().text.trim()

    def patternVersion = "^(v\\d+\\.\\d+)(\\.(\\d+))?"
    def matchVersion = version =~ patternVersion

    def versionPref = version
    def versionSub = 0
    if (matchVersion) {
        versionPref = matchVersion[0][1]
        if (matchVersion[0][3]) {
            versionSub = Integer.parseInt(matchVersion[0][3])
        }
    }

    def pattern = "-(\\d+)-g"
    def matcher = version =~ pattern

    if (matcher) {
        version = versionPref + "." + (Integer.parseInt(matcher[0][1]) + versionSub)
    } else {
        version = versionPref + "." + versionSub
    }

    return version
}

android {
    compileSdkVersion 31

    defaultConfig {
        applicationId "soko.ekibun.stitch"
        minSdkVersion 21
        //noinspection OldTargetApi
        targetSdkVersion 31
        versionCode gitVersionCode()
        versionName gitVersionTag()
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    splits {
        abi {
            enable true
            universalApk true
        }
    }
    signingConfigs {
        debug {
            keyAlias 'ekibun'
            keyPassword 'ekibun'
            storeFile file('D:/OneDrive/ekibun.jks')
            storePassword 'ekibun'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    ndkVersion '22.1.7171670'
    externalNativeBuild {
        cmake {
            version '3.22.1'
            path (project.projectDir.toString() + '/src/main/jni/CMakeLists.txt')
        }
    }
}

dependencies {
    implementation 'androidx.core:core-ktx:1.8.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
    implementation 'com.google.code.gson:gson:2.8.9'
}