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

static def releaseTime() {
    return new Date().format("yyyy-MM-dd_HHmmss", TimeZone.getTimeZone("UTC"))
}

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId 'org.flyve.mdm.agent'
        minSdkVersion 16
        targetSdkVersion 28
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }
    flavorDimensions "mdm"
    productFlavors {
        fcm {
            dimension "mdm"
        }
        mqtt {
            dimension "mdm"
            applicationIdSuffix ".mqtt"
        }
    }
    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = outputFileName
            if (outputFile != null && outputFile.endsWith('.apk')) {
                def fileName = outputFile.replace("app",
                        "${defaultConfig.applicationId}_${releaseTime()}")
                outputFileName = fileName

            }
        }
    }
    dexOptions {
        preDexLibraries true
        maxProcessCount 8
    }
    lintOptions {
        abortOnError false
    }
    defaultConfig {
        multiDexEnabled true
    }
}

sonarqube {
    properties {
        property "sonar.sourceEncoding", "UTF-8"
    }
}

dependencies {
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation('tools.fastlane:screengrab:1.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:2.21.0'
    androidTestImplementation 'org.mockito:mockito-android:2.21.0'
    // MQTT libraries
    mqttImplementation('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
        exclude module: 'support-v4'
        transitive = true
    }
    mqttImplementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.0'
    mqttImplementation 'com.madgag.spongycastle:core:1.54.0.0'
    mqttImplementation 'com.madgag.spongycastle:prov:1.54.0.0'
    mqttImplementation 'com.madgag.spongycastle:pkix:1.54.0.0'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.google.zxing:core:3.3.0'
    //Firebase
    fcmImplementation 'com.google.firebase:firebase-core:16.0.5'
    fcmImplementation 'com.google.firebase:firebase-messaging:17.3.4'
    // Google libraries
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'android.arch.persistence.room:runtime:1.1.1'
    annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
    // Logger
    implementation 'com.orhanobut:logger:2.1.1'
    // Android Inventory Library
    implementation 'org.flyve:inventory:1.4.0@aar'
    // Bug snag
    implementation 'com.bugsnag:bugsnag-android:4.1.3'
    implementation project(':policies')
}


import groovy.json.JsonSlurper

import java.util.regex.Pattern

task createAbout(type:Exec) {
    workingDir '../ci/scripts'
    commandLine './create_about_data.sh'
}

task updateVersionCode {
    doLast {

        // Usage example:
        // set manual version:
        // gradlew updateVersionCode -P vVersion=123
        // or autoincrement from AndroidManifest
        // gradlew updateVersionCode

        def manifestFile = file("src/main/AndroidManifest.xml")
        def pattern = Pattern.compile("versionCode=\"(\\d+)\"")
        def manifestText = manifestFile.getText()
        def matcher = pattern.matcher(manifestText)
        matcher.find()
        def versionCode = Integer.parseInt(matcher.group(1))

        if(project.hasProperty("vVersion")) {
            versionCode = vVersion
        } else {
            ++versionCode
        }

        def manifestContent = matcher.replaceAll("versionCode=\"" + versionCode + "\"")
        println "Version Code: " + versionCode
        manifestFile.write(manifestContent)
    }
}

preBuild.dependsOn createAbout

task updateVersionName {
    doLast {

        // Usage example:
        // set manual version:
        // gradlew updateVersionName -P vName=1.0.0
        // or get the version from package.json:
        // gradlew updateVersionName

        def versionName

        if (project.hasProperty("vName")) {
            versionName = vName
        } else {
            def packageFile = file('../package.json')
            def json = new JsonSlurper().parseText(packageFile.text)
            versionName = json.version
        }

        def manifestFile = file("src/main/AndroidManifest.xml")
        def patternVersionNumber = Pattern.compile("versionName=\"(\\d+)\\.(\\d+)\\.(\\d+)(?:\\S*)\"")
        def manifestText = manifestFile.getText()
        def matcherVersionNumber = patternVersionNumber.matcher(manifestText)
        matcherVersionNumber.find()

        def manifestContent = matcherVersionNumber.replaceAll("versionName=\"" + versionName + "\"")
        manifestFile.write(manifestContent)

        println "Version name: " + versionName
    }
}
apply plugin: 'com.google.gms.google-services'