buildscript {
  repositories {
    google()
    jcenter()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:4.1.0'
  }
}

allprojects {
    repositories {
        google()
        jcenter()
    }

    Properties props = new Properties()
    props.load(new FileInputStream(new File(project.rootDir, "retroarch.properties")))
    props.each { prop ->
        project.ext.set(prop.key, prop.value)
    }
}

apply plugin: 'com.android.application'

android {
  compileSdkVersion 29
  buildToolsVersion "29.0.3"
  ndkVersion "22.0.7026061"

  flavorDimensions "variant"

  defaultConfig {
    minSdkVersion 16
    externalNativeBuild {
      ndkBuild {
        arguments "-j${Runtime.runtime.availableProcessors()}"
      }
    }
    targetSdkVersion 28
    versionCode System.currentTimeSeconds().toInteger()
    versionName "${getManifestAttribute("versionName")}_GIT"
  }

  productFlavors {
    normal {
      resValue "string", "app_name", "RetroArch"
      buildConfigField "boolean", "PLAY_STORE_BUILD", "false"

      dimension "variant"
    }
    aarch64 {
      applicationIdSuffix '.aarch64'
      resValue "string", "app_name", "RetroArch (AArch64)"
      buildConfigField "boolean", "PLAY_STORE_BUILD", "false"

      dimension "variant"
      ndk {
        abiFilters 'arm64-v8a', 'x86_64'
      }
    }
    ra32 {
      applicationIdSuffix '.ra32'
      resValue "string", "app_name", "RetroArch (32-bit)"
      buildConfigField "boolean", "PLAY_STORE_BUILD", "false"

      dimension "variant"
      ndk {
        abiFilters 'armeabi-v7a', 'x86'
      }
    }
    playStoreNormal {
      minSdkVersion 21
      targetSdkVersion 29
      versionCode getPlayStoreVersionCode()
      versionName getPlayStoreVersionName()

      resValue "string", "app_name", "RetroArch"
      buildConfigField "boolean", "PLAY_STORE_BUILD", "true"

      dimension "variant"
    }
    playStorePlus {
      minSdkVersion 26
      targetSdkVersion 29
      versionCode getPlayStoreVersionCode()
      versionName getPlayStoreVersionName()

      applicationIdSuffix '.aarch64'
      resValue "string", "app_name", "RetroArch Plus"
      buildConfigField "boolean", "PLAY_STORE_BUILD", "true"

      dimension "variant"
    }
  }

  sourceSets {
    main {
      manifest.srcFile 'AndroidManifest.xml'
      assets.srcDirs = ['assets']
      java.srcDirs = ['src', '../phoenix-common/src']
      jniLibs.srcDir '../phoenix-common/libs'
      jni.srcDirs = []
      res.srcDirs = ['res', '../phoenix-common/res']
    }
    aarch64 {
      res.srcDirs = ['res', 'res64']
    }
  }

  externalNativeBuild {
    ndkBuild {
      path '../phoenix-common/jni/Android.mk'
    }
  }

  signingConfigs {
    if (project.hasProperty("RELEASE_STORE_FILE")) {
      release {
        storeFile file(RELEASE_STORE_FILE)
        storePassword RELEASE_STORE_PASSWORD
        keyAlias RELEASE_KEY_ALIAS
        keyPassword RELEASE_KEY_PASSWORD
      }
    }
  }

  buildTypes {
    if (project.hasProperty("RELEASE_STORE_FILE")) {
      release {
        signingConfig signingConfigs.release
      }
    }
    else {
      release {
        signingConfig signingConfigs.debug
      }
    }
  }
}

dependencies {
  implementation 'com.google.android.play:core:1.8.0'
}

def dynamicFeatures = file("dynamic_features.gradle")
if(dynamicFeatures.exists()) {
  apply from: "dynamic_features.gradle"
}

int getPlayStoreVersionCode() {
  def baseVersion = getManifestAttribute("versionCode").toInteger()
  def baseTime = 1609632000
  def halfWeek = 302400
  def increment = (System.currentTimeSeconds() - baseTime) / halfWeek

  return baseVersion + increment
}

String getPlayStoreVersionName() {
  def versionName = getManifestAttribute("versionName")
  return "$versionName (${new Date().format("yyyy-MM-dd")})"
}

String getManifestAttribute(String attribute) {
  def manifest = new XmlParser().parse("${project.projectDir}/AndroidManifest.xml")
  return manifest.attributes().find {
    ((String) it.key).contains(attribute)
  }.value
}

tasks.register("outputVersionCode") {
  doLast {
    println getPlayStoreVersionCode()
  }
}