/*
 * This file is part of AndProx, an application for using Proxmark3 on Android.
 *
 * Copyright 2016-2018 Michael Farrell <micolous+git@gmail.com>
 *
 * This program 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.
 *
 * This program 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
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Under section 7 of the GNU General Public License v3, the following additional
 * terms apply to this program:
 *
 *  (b) You must preserve reasonable legal notices and author attributions in
 *      the program.
 *  (c) You must not misrepresent the origin of this program, and need to mark
 *      modified versions in reasonable ways as different from the original
 *      version (such as changing the name and logos).
 *  (d) You may not use the names of licensors or authors for publicity
 *      purposes, without explicit written permission.
 */

apply plugin: 'com.android.library'

// This section defines a build for non-Android JNI, which is used by the unit tests. To build this,
// we're effectively building a standard, non-Android JNI version of our native libraries.
//
// This makes the build process more dependent on the host.
//
// It requires make and cmake in your path.
def nonAndroidJniDir = projectDir.absolutePath + '/.externalNativeBuild/cmake/debug/nonAndroidJni'

task createNonAndroidBuildDir() {
    def dir = new File(nonAndroidJniDir)
    if (!dir.exists()) {
        dir.mkdirs()
    }
}

task runNonAndroidCMake(type: Exec) {
    dependsOn createNonAndroidBuildDir
    workingDir nonAndroidJniDir
    commandLine android.sdkDirectory.absolutePath + '/cmake/3.6.4111459/bin/cmake'
    args (projectDir.absolutePath + '/src/main/cmake/mainline/')
}

task runNonAndroidMake(type: Exec) {
    dependsOn runNonAndroidCMake
    workingDir nonAndroidJniDir
    commandLine 'make'
}

dependencies {
    testImplementation 'org.mockito:mockito-core:2.18.3'
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.jetbrains:annotations:15.0'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'org.mockito:mockito-android:2.18.3'
    androidTestImplementation 'org.jetbrains:annotations:15.0'

    implementation 'commons-io:commons-io:2.5'
    implementation "com.android.support:support-annotations:$support_version"
    //implementation "com.android.support:support-compat:$support_version"
}

task copyScripts(type: Copy) {
    description = 'Copy scripts from proxmark3 client'
    from '../third_party/proxmark3/client/scripts'
    into 'build/assets/scripts'
    include '*.lua'
}

task copyLuaLibs(type: Copy) {
    description = 'Copy scripts from proxmark3 client'
    from '../third_party/proxmark3/client/lualibs'
    into 'build/assets/lualibs'
    include '*.lua'
}

task copyHardnestedTables(type: Copy) {
    description = 'Copy Hardnested Tables from proxmark3 client'
    from '../third_party/proxmark3/client/hardnested'
    into 'build/assets/hardnested'
    include '**/*.bin.z'
    include '**/*.bin'
}

task copyThirdPartyNotices(type: Copy) {
    from '../third_party/'
    into 'build/assets/third_party'
    eachFile { it.path = name }
    include 'NOTICE.proxmark3.txt'
    include 'NOTICE.jansson.txt' // from PM3
    include 'NOTICE.lua.txt' // from PM3
    include 'NOTICE.webrtc.txt'
    include 'NOTICE.zlib.txt' // from PM3
}

android {
    /*
     * In the context of NDK, the compile and target API levels are the MINIMUM API level.
     *
     * As a result, this library (natives) needs to shoot pretty low.
     *
     * We currently target API 21 as 64-bit architecture support. This is needed for performance of
     * maths-heavy parts of Proxmark3 (crypto), and also required by Google Play:
     * https://android-developers.googleblog.com/2017/12/improving-app-security-and-performance.html
     *
     * For more context, see the NDK platform documentation at:
     * https://android.googlesource.com/platform/ndk/+/master/docs/user/common_problems.md#target-api-set-higher-than-device-api
     *
     * The rest of AndProx can target higher API levels safely.
     */
    compileSdkVersion 21
    buildToolsVersion '28.0.3'

    defaultConfig {
        minSdkVersion 21
        // These warnings are not relevant, per above comment.
        //noinspection ExpiredTargetSdkVersion,OldTargetApi
        targetSdkVersion 21
        versionCode "git -C ${projectDir} rev-list --first-parent --count HEAD".execute().text.trim().toInteger()
        versionName "git -C ${projectDir} describe --always --tags --dirty".execute().text.trim().replaceAll("^v", "")
        ndk.abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_PLATFORM=android-21", "-DANDROID_TOOLCHAIN=clang"
            }
        }
    }

    preBuild.dependsOn copyScripts, copyLuaLibs, copyHardnestedTables, copyThirdPartyNotices

    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets', 'build/assets/']
        }
    }

    buildTypes {
        release {
            minifyEnabled true
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cmake/mainline/CMakeLists.txt"
        }
    }

    testOptions {
        unitTests {
            all {
                systemProperty 'java.library.path', nonAndroidJniDir
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

project.afterEvaluate {
    javaPreCompileDebug.dependsOn externalNativeBuildDebug
    if (org.gradle.internal.os.OperatingSystem.current().isUnix()) {
        javaPreCompileDebugUnitTest.dependsOn runNonAndroidMake
    }
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
