apply plugin: 'com.android.application'

android {
    compileSdkVersion 30

    def versionPropsFile = file('version.properties')
    def vNumber
    def vName
    if (versionPropsFile.canRead()) {
        Properties versionProps = new Properties()
        new FileInputStream(versionPropsFile).withCloseable { stream -> versionProps.load(stream) }
        vNumber = versionProps['VERSION_CODE'].toInteger()
        vName = versionProps['VERSION_NAME'].toString()
    } else throw new FileNotFoundException("Could not read version.properties!")

    defaultConfig {
        applicationId "com.blackforestbytes.simplecloudnotifier"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode vNumber
        versionName vName
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
    namespace 'com.blackforestbytes.simplecloudnotifier'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    implementation 'com.google.android.material:material:1.2.1'
    implementation 'com.google.firebase:firebase-core:18.0.0'
    implementation 'com.google.firebase:firebase-messaging:21.0.0'
    implementation 'com.google.android.gms:play-services-ads:19.5.0'
    implementation 'com.android.billingclient:billing:3.0.1'

    implementation 'com.squareup.okhttp3:okhttp:4.9.0'
    implementation 'com.github.kenglxn.QRGen:android:2.5.0'
    implementation "com.github.DeweyReed:UltimateMusicPicker:2.0.0"
    implementation 'com.github.duanhong169:colorpicker:1.1.5'

    implementation 'net.danlew:android.joda:2.10.7.1'
}

apply plugin: 'com.google.gms.google-services'

tasks.register("updateVersion") {
    group = 'Custom'

    doLast {
        def lastTag = ['git', 'describe', "--abbrev=0", "--tags"].execute().text.trim()

        def versionPropsFile = file('version.properties')
        if (!versionPropsFile.canRead()) throw new FileNotFoundException("Could not read version.properties!")
        Properties versionProps = new Properties()
        new FileInputStream(versionPropsFile).withCloseable { fis -> versionProps.load(fis) }

        def matcher = lastTag =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)$/

        if (!matcher.matches()) throw new Exception("Last Tag ('" + lastTag + "') has invalid format :(")

        def vName = (matcher[0][1] as Integer) + "." + (matcher[0][2] as Integer) + "." + (matcher[0][3] as Integer)
        def vCode = versionProps['VERSION_CODE'] as Integer

        if (new File(".do_publish_beta_release").exists()) new File(".do_publish_beta_release").delete()
        if (new File(".do_publish_prod_release").exists()) new File(".do_publish_prod_release").delete()

        if (vName == versionProps['VERSION_NAME'].toString()) {
            println "This version was already built - skip deployment"
        } else if (vName.endsWith(".0")) {
            println ""
            println "====================================================================="
            println "====================================================================="
            println "(!) This is a new PRODUCTION release - create deployment trigger file"
            println "====================================================================="
            println "====================================================================="
            println ""

            vCode++
            new File(".do_publish_prod_release").createNewFile()

            versionProps['VERSION_NAME'] = vName.toString()
            versionProps['VERSION_CODE'] = vCode.toString()

            versionPropsFile.newWriter().withCloseable { w -> versionProps.store(w, null) }
        } else {
            println ""
            println "==============================================================="
            println "(!) This is a new beta release - create deployment trigger file"
            println "==============================================================="
            println ""

            vCode++
            new File(".do_publish_beta_release").createNewFile()

            versionProps['VERSION_NAME'] = vName.toString()
            versionProps['VERSION_CODE'] = vCode.toString()

            versionPropsFile.newWriter().withCloseable { w -> versionProps.store(w, null) }
        }
    }
}