apply plugin: 'com.android.library'

android {
    compileSdkVersion 34
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    signingConfigs {
        normSigning {
            storeFile     file("${System.properties['user.home']}${File.separator}.android${File.separator}debug.keystore")
            storePassword "android"
            keyAlias      "androiddebugkey"
            keyPassword   "android"
            storeType     "jks"
        }
    }

    defaultConfig {
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_PLATFORM=android-24", "-DCMAKE_MAKE_PROGRAM=/usr/local/bin/ninja", "-DANDROID_STL=c++_static", "-DANDROID_CPP_FEATURES=exceptions rtti", "-DANDROID_ARM_MODE=arm", "-DANDROID_ARM_NEON=TRUE"
                cFlags "-fsigned-char"
                cppFlags "-fsigned-char", "-std=c++14"
            }
        }
    }

    buildTypes {
         debug {
             initWith debug
             debuggable    true
             jniDebuggable true
             signingConfig signingConfigs.normSigning
         }
         release {
             initWith release
             debuggable    false
             jniDebuggable false
             signingConfig signingConfigs.normSigning
         }
    }

    flavorDimensions "default"
    productFlavors {
        debug_ {
            ndk {
                abiFilters "armeabi-v7a", "x86"
            }
            externalNativeBuild {
                cmake {
                    targets "norm", "mil_navy_nrl_norm"
                    arguments "-DBUILD_CONFIGURATION=DEBUG", "-DCMAKE_CXX_FLAGS_DEBUG=-O0", "-DCMAKE_C_FLAGS_DEBUG=-O0"
                }
            }

            dimension "default"
       }
        release_ {
            externalNativeBuild {
                cmake {
                    targets "norm", "mil_navy_nrl_norm"
                    arguments "-DBUILD_CONFIGURATION=RELEASE", "-DCMAKE_CXX_FLAGS_RELEASE=-O3", "-DCMAKE_C_FLAGS_RELEASE=-O3"
                }
            }

            dimension "default"
       }
    }

    variantFilter { variant ->
        def names = variant.flavors*.name
        if (names.contains ("debug_")
              && variant.buildType.name != "debug") {
            setIgnore(true)
        }
        if (names.contains ("release_")
              && variant.buildType.name != "release") {
            setIgnore(true)
        }
    }

repositories {
}

dependencies {
}


}

task copyReleaseJar(type: Copy) {
    println 'Unzipping release .aar and including .jar....'

    if (file("${project.rootDir}/lib/build/outputs/aar/lib-release_-release.aar").exists()) {
        from zipTree("${project.rootDir}/lib/build/outputs/aar/lib-release_-release.aar")
        into "${project.rootDir}/lib/build/outputs/aar/lib-release_-release"

        from "${System.env.HOME}/norm/lib/norm-1.0.0.jar"
        into "${project.rootDir}/lib/build/outputs/aar/lib-release_-release/"
    }
}

task copyDebugJar(type: Copy, dependsOn: copyReleaseJar) {
    println 'Unzipping debug .aar and including .jar....'

    if (file("${project.rootDir}/lib/build/outputs/aar/lib-debug_-debug.aar").exists()){
        from zipTree("${project.rootDir}/lib/build/outputs/aar/lib-debug_-debug.aar")
        into "${project.rootDir}/lib/build/outputs/aar/lib-debug_-debug"

        from "${System.env.HOME}/norm/lib/norm-1.0.0.jar"
        into "${project.rootDir}/lib/build/outputs/aar/lib-debug_-debug/"
    }
}

task zipReleaseAAR(type: Exec, dependsOn: copyDebugJar) {
    println 'Creating new release .aar..'
    ignoreExitValue true
    workingDir "${project.rootDir}/lib/build/outputs/aar/"
    executable 'jar'
    def releaseArgsList = ['cvf', 'lib-release_-release.aar', '-C', 'lib-release_-release/', '.']
    args releaseArgsList
}

task zipDebugAAR(type: Exec, dependsOn: zipReleaseAAR) {
    println 'Creating new debug .aar..'
    ignoreExitValue true
    workingDir "${project.rootDir}/lib/build/outputs/aar/"
    executable 'jar'
    def debugArgsList = ['cvf', 'lib-debug_-debug.aar', '-C', 'lib-debug_-debug/', '.']
    args debugArgsList
}

task deleteAARAssets(type: Delete, dependsOn: zipDebugAAR) {
    delete "${project.rootDir}/lib/build/outputs/aar/lib-release_-release"
    delete "${project.rootDir}/lib/build/outputs/aar/lib-debug_-debug"
}


this.build.finalizedBy(deleteAARAssets)

