import org.gradle.internal.os.OperatingSystem;

apply plugin: 'com.android.library'
version "v0.3.1.6_beta-SNAPSHOT"
extensions["module_name"] = "EdXposed"
android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion

        externalNativeBuild {
            ndkBuild {
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
                arguments "NDK_PROJECT_PATH=jni/"
            }
        }
    }
    externalNativeBuild {
        ndkBuild {
            path 'jni/Android.mk'
        }
    }
}
afterEvaluate {

    android.libraryVariants.all { variant ->
        def nameCapped = variant.name.capitalize()
        def nameLowered = variant.name.toLowerCase()

        def zipTask = task("zip${nameCapped}", type: Exec, dependsOn: ":Bridge:makeAndCopy${nameCapped}") {
            workingDir '..'
            commandLine 'sh', 'build.sh',   \
                       project.name,   \
                       "${project.version}-${nameLowered}",   \
                       "${project.extensions['module_name']}"
        }

        def pushTask = task("push${nameCapped}", type: Exec) {
            workingDir 'release'
            def commands = ["adb", "push", "magisk-${project.extensions['module_name']}" +
                    "-${project.version}-${nameLowered}.zip", "/sdcard/"]
            if (OperatingSystem.current().isWindows()) {
                commandLine 'cmd', '/c', commands.join(" ")
            } else {
                commandLine commands
            }
        }

        pushTask.dependsOn(zipTask)
    }

}
