apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'maven-publish'


group = 'com.spacedrive.core'
version = '0.0.1'

buildscript {
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
  if (expoModulesCorePlugin.exists()) {
    apply from: expoModulesCorePlugin
    applyKotlinExpoModulesCorePlugin()
  }

  // Simple helper that allows the root project to override versions declared by this library.
  ext.safeExtGet = { prop, fallback ->
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
  }

  // Ensures backward compatibility
  ext.getKotlinVersion = {
    if (ext.has("kotlinVersion")) {
      ext.kotlinVersion()
    } else {
      ext.safeExtGet("kotlinVersion", "1.8.10")
    }
  }

  repositories {
    mavenCentral()
		maven {
				url "https://plugins.gradle.org/m2/"
		}
  }

  dependencies {
    classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${getKotlinVersion()}")
		classpath 'org.mozilla.rust-android-gradle:plugin:0.9.3'
  }
}

afterEvaluate {
  publishing {
    publications {
      release(MavenPublication) {
        from components.release
      }
    }
    repositories {
      maven {
        url = mavenLocal().url
      }
    }
  }
}

android {
  compileSdkVersion safeExtGet("compileSdkVersion", 33)

  compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
  }

  kotlinOptions {
    jvmTarget = JavaVersion.VERSION_11.majorVersion
  }

  namespace "com.spacedrive.core"
  defaultConfig {
    minSdkVersion safeExtGet("minSdkVersion", 21)
    targetSdkVersion safeExtGet("targetSdkVersion", 33)
    versionCode 1
    versionName "0.2.0"
  }
  lintOptions {
    abortOnError false
  }
  publishing {
    singleVariant("release") {
      withSourcesJar()
    }
  }
	sourceSets {
		main {
			jniLibs.srcDirs = ['../../../../../target/release']
		}
	}
}

repositories {
  mavenCentral()
}

dependencies {
  implementation project(':expo-modules-core')
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${getKotlinVersion()}"
}

apply plugin: 'org.mozilla.rust-android-gradle.rust-android'

def cargoTargets = ["x86_64"]

if (System.getenv('SPACEDRIVE_CI') != "1") {
    cargoTargets = ["arm", "arm64", "x86"]
}

cargo {
	  final String osName = System.getProperty("os.name").toLowerCase();

    module  = "./crate"
    libname = "sd_mobile_android"
    pythonCommand = 'python3'
    profile = 'release' // 'debug'
    targets = cargoTargets
    targetDirectory = "../../../../../target" // Monorepo moment
		exec { spec, toolchain ->

			def dir = "${android.ndkDirectory}/toolchains/llvm/prebuilt/${osName.contains("mac") ? "darwin" : osName}-x86_64/bin/llvm-ranlib"

			spec.environment("RANLIB_armv7-linux-androideabi", "${dir}")
			spec.environment("RANLIB_aarch64-linux-android", "${dir}")
			spec.environment("RANLIB_i686-linux-android", "${dir}")
			spec.environment("RANLIB_x86_64-linux-android", "${dir}")
		}
}

tasks.whenTaskAdded { task ->
	// Require cargo to be run before copying native libraries.
	if ((task.name == 'mergeDebugJniLibFolders' || task.name == 'mergeReleaseJniLibFolders')) {
			task.dependsOn 'cargoBuild'
	}
	// Require "clean builds" to avoid issues with build caches.
	if (task.name == 'assembleDebug' || task.name == 'assembleRelease')  {
			task.dependsOn 'clean'
	}
}
