/*
 * This file is part of LSPosed.
 *
 * LSPosed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LSPosed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LSPosed.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Copyright (C) 2020 EdXposed Contributors
 * Copyright (C) 2021 LSPosed Contributors
 */

apply plugin: 'com.android.library'

android {
    compileSdkVersion androidCompileSdkVersion.toInteger()

    defaultConfig {
        minSdkVersion androidMinSdkVersion.toInteger()
        targetSdkVersion androidTargetSdkVersion.toInteger()

        externalNativeBuild {
            cmake {
                abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
                cppFlags "-std=c++20"
                cFlags "-std=gnu99"
            }
        }
    }

    buildTypes {
        debug {
            externalNativeBuild {
                cmake {
                    cppFlags "-O0"
                    cFlags "-O0"
                }
            }
        }
        release {
            externalNativeBuild {
                cmake {
                    cppFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -s -Wno-unused-value"
                    cFlags "-fvisibility=hidden -fvisibility-inlines-hidden -Os -s -Wno-unused-value"
                }
            }
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

afterEvaluate {
    android.libraryVariants.all { variant ->
        def variantNameCapped = variant.name.capitalize()
        def variantNameLowered = variant.name.toLowerCase()

        task("copyKeySelector${variantNameCapped}LibraryToMagiskTemplate") {
            dependsOn tasks.getByName("assemble${variantNameCapped}")
            def libPathRelease = "${buildDir}/intermediates/cmake/${variantNameLowered}/obj"
            doLast {
                copy {
                    include "key_selector"
                    from "${libPathRelease}/armeabi-v7a"
                    into "${zipPathMagiskReleasePath}/system/bin"
                }
                copy {
                    include "key_selector"
                    from "${libPathRelease}/arm64-v8a"
                    into "${zipPathMagiskReleasePath}/system/bin64"
                }
                copy {
                    include "key_selector"
                    from "${libPathRelease}/x86"
                    into "${zipPathMagiskReleasePath}/system_x86/bin"
                }
                copy {
                    include "key_selector"
                    from "${libPathRelease}/x86_64"
                    into "${zipPathMagiskReleasePath}/system_x86/bin64"
                }
            }
        }

    }
}