/*
 * Ubuntu Countdown Widget
 * Copyright (C) 2022 Roberto Leinardi
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */

plugins {
    id 'ubuntucountdownwidget.android-app-conventions'
}

def getVersionName = {
    try {
        def tagName = 'git describe --abbrev=0 --tags'.execute().text.trim()
        def tagHash = 'git rev-list --tags --max-count=1'.execute().text.trim()
        def commitCount = Integer.parseInt(('git rev-list --count ' + tagHash + '..HEAD').execute().text.trim())

        if (commitCount > 0) {
            return String.format("%s.%d", tagName, commitCount)
        } else {
            return tagName
        }
    }
    catch (ignored) {
        return null
    }
}
def commitCount = 'git rev-list --count HEAD'.execute().text.trim()
def isKeystoreInfoAvailable = file(KEYSTORE_PATH).exists() && file(KEYSTORE_PROPERTIES_PATH).exists()

android {
    defaultConfig {
        applicationId config.android.applicationId
        archivesBaseName = "ubuntucountdownwidget"
        applicationId "com.leinardi.ubuntucountdownwidget"
        versionCode commitCount.isNumber() ? (commitCount as int) + 200 : 0
        versionName getVersionName()
    }


    buildTypes {
        debug {
            applicationIdSuffix ".debug"
        }
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            if (isKeystoreInfoAvailable) {
            }
        }
    }
}

task updateVersionFile() {
    doLast {
        project.rootProject.file("version.txt").text =
                "${project.android.defaultConfig.versionName}+${project.android.defaultConfig.versionCode}\n"
    }
}

preBuild.dependsOn updateVersionFile

dependencies {
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation libs.androidx.appcompat
    implementation libs.material
    implementation "androidx.preference:preference-ktx:1.2.0"
    implementation "androidx.legacy:legacy-preference-v14:1.0.0"
    implementation "com.github.gabrielemariotti.changeloglib:changelog:2.1.0"
    implementation libs.timber
}
