plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

String getTime() {
    String time = (('git log --pretty=format:%ci HEAD -1'.execute().text.trim()).split(" ")[0])
    String[] date = time.split("-")
    String versionCode = ""
    for (int i = 0; i < date.length; i++) {
        versionCode = versionCode + date[i]
    }
    print("time = " + time + ", versiocode = " + versionCode)
    return versionCode
}
// Auto-incrementing commit count based on counting commits to HEAD (Build #543)
def gitCommitCount = Integer.parseInt('git rev-list HEAD --count'.execute().text.trim())
def gitSHA = ('git rev-parse --short HEAD'.execute().text.trim())
String time = getTime()
// I want to use git tags as my version names (1.2.543)
def gitCurrentTag = 'git describe --tags --abbrev=0'.execute([], project.rootDir).text.trim()
def name = gitCurrentTag + "." + gitCommitCount + "." + gitSHA + "." + time
android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "ernest.linuxcmd"
        minSdkVersion 19
        targetSdkVersion 33
        versionCode gitCommitCount
        versionName name
        multiDexEnabled true
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
    signingConfigs {
        release {

            storeFile file(KEYSTORE)

            storePassword System.getenv("KEYSTORE_PASSWORD")

            keyAlias System.getenv("KEY_ALIAS")

            keyPassword System.getenv("KEY_PASSWORD")
        }
    }


    buildTypes {
        release {
            minifyEnabled false
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
    packagingOptions {
        resources {
            pickFirsts += ['META-INF/LICENSE-LGPL-3.txt',
                           'META-INF/LICENSE-LGPL-2.1.txt',
                           'META-INF/LICENSE-LGPL-W3C-TEST',
                           'META-INF/LICENSE-W3C-TEST',
                           'META-INF/DEPENDENCIES']
        }
    }
    applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = 'LinuxCmd-' + defaultConfig.versionName + '.apk'
        }
    }
    configurations {
        all {
            exclude module: 'commons-logging'
        }
    }
    namespace 'ernest.linuxcmd'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    androidTestImplementation('androidx.test.espresso:espresso-core:3.4.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.preference:preference:1.1.1'
    implementation "androidx.multidex:multidex:2.0.1"
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'org.commonmark:commonmark:0.21.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'com.vladsch.flexmark:flexmark-all:0.64.0'
}
