/*
 * R3 Proprietary and Confidential
 *
 * Copyright (c) 2018 R3 Limited.  All rights reserved.
 *
 * The intellectual and technical concepts contained herein are proprietary to R3 and its suppliers and are protected by trade secret law.
 *
 * Distribution of this file or any portion thereof via any medium without the express permission of R3 is strictly prohibited.
 */

buildscript {
    // For sharing constants between builds
    Properties constants = new Properties()
    file("$projectDir/constants.properties").withInputStream { constants.load(it) }

    // Our version: bump this on release.
    ext.corda_release_version = "R3.CORDA-3.0-SNAPSHOT"    // "CORDA-3.0-SNAPSHOT" for Corda (Open Source)
    ext.corda_release_distribution = "com.r3.corda"        // "net.corda" for Corda (Open Source)

    // Increment this on any release that changes public APIs anywhere in the Corda platform
    ext.corda_platform_version = constants.getProperty("platformVersion")
    ext.gradle_plugins_version = constants.getProperty("gradlePluginsVersion")

    // Dependency versions. Can run 'gradle dependencyUpdates' to find new versions of things.
    //
    // TODO: Sort this alphabetically.
    ext.kotlin_version = constants.getProperty("kotlinVersion")
    // use our fork of quasar
    ext.quasar_group = 'com.github.corda.quasar'
    ext.quasar_version = '7629695563deae6cc95adcfbebcbc8322fd0241a'

    // gradle-capsule-plugin:1.0.2 contains capsule:1.0.1
    // TODO: Upgrade gradle-capsule-plugin to a version with capsule:1.0.3
    ext.capsule_version = '1.0.1'

    ext.asm_version = '0.5.3'

    /*
     * TODO Upgrade to version 2.4 for large message streaming support
     *
     * Due to a memory leak in the connection handling code in Artemis, we are
     * temporarily downgrading to version 2.2.0.
     *
     * The memory leak essentially triggers an out-of-memory exception within
     * less than 10 seconds and can take down a node if a non-TLS connection is
     * attempted against the P2P port.
     *
     * The issue has been reported to upstream:
     * https://issues.apache.org/jira/browse/ARTEMIS-1559
     */
    ext.artemis_version = '2.2.0'
    ext.jackson_version = '2.9.3'
    ext.jetty_version = '9.4.7.v20170914'
    ext.jersey_version = '2.25'
    ext.json_version = '20180130'
    ext.assertj_version = '3.8.0'
    ext.slf4j_version = '1.7.25'
    ext.log4j_version = '2.9.1'
    ext.bouncycastle_version = constants.getProperty("bouncycastleVersion")
    ext.guava_version = constants.getProperty("guavaVersion")
    ext.caffeine_version = constants.getProperty("caffeineVersion")
    ext.metrics_version = constants.getProperty("metricsVersion")
    ext.okhttp_version = '3.5.0'
    ext.netty_version = '4.1.9.Final'
    ext.typesafe_config_version = constants.getProperty("typesafeConfigVersion")
    ext.fileupload_version = '1.3.3'
    ext.junit_version = '4.12'
    ext.mockito_version = '2.10.0'
    ext.jopt_simple_version = '5.0.2'
    ext.jansi_version = '1.14'
    ext.hibernate_version = '5.2.6.Final'
    ext.h2_version = '1.4.194' // Update docs if renamed or removed.
    ext.postgresql_version = '42.1.4'
    ext.rxjava_version = '1.2.4'
    ext.dokka_version = '0.9.16-eap-2'
    ext.eddsa_version = '0.2.0'
    ext.dependency_checker_version = '3.1.0'
    ext.commons_collections_version = '4.1'
    ext.beanutils_version = '1.9.3'
    ext.crash_version = 'cadb53544fbb3c0fb901445da614998a6a419488'
    ext.jsr305_version = constants.getProperty("jsr305Version")
    ext.spring_jdbc_version ='5.0.0.RELEASE'
    ext.shiro_version = '1.4.0'
    ext.artifactory_plugin_version = constants.getProperty('artifactoryPluginVersion')
    ext.liquibase_version = '3.5.3'
    ext.shadow_version = '2.0.2'
    ext.hikari_version = '2.5.1'
    ext.artifactory_contextUrl = 'https://ci-artifactory.corda.r3cev.com/artifactory'
    ext.snake_yaml_version = constants.getProperty('snakeYamlVersion')
    ext.docker_compose_rule_version = '0.33.0'
    ext.selenium_version = '3.8.1'
    ext.ghostdriver_version = '2.1.0'
    ext.eaagentloader_version = '1.0.3'
    ext.curator_version = '4.0.0'
    ext.jsch_version = '0.1.54'

    // Update 121 is required for ObjectInputFilter and at time of writing 131 was latest:
    ext.java8_minUpdateVersion = '131'

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        // This repository is needed for Dokka until 0.9.16 is released.
        maven {
            url 'https://dl.bintray.com/kotlin/kotlin-eap/'
        }
        maven {
            url "$artifactory_contextUrl/corda-releases"
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
        classpath "net.corda.plugins:publish-utils:$gradle_plugins_version"
        classpath "net.corda.plugins:quasar-utils:$gradle_plugins_version"
        classpath "net.corda.plugins:cordformation:$gradle_plugins_version"
        classpath "net.corda.plugins:cordapp:$gradle_plugins_version"
        classpath "net.corda.plugins:api-scanner:$gradle_plugins_version"
        classpath 'com.github.ben-manes:gradle-versions-plugin:0.15.0'
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
        classpath "org.jetbrains.dokka:dokka-gradle-plugin:${dokka_version}"
        classpath "org.ajoberstar:grgit:1.1.0"
        classpath "net.i2p.crypto:eddsa:$eddsa_version" // Needed for ServiceIdentityGenerator in the build environment.
        classpath "org.owasp:dependency-check-gradle:${dependency_checker_version}"
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:$artifactory_plugin_version"
    }
}

plugins {
    // TODO The capsule plugin requires the newer DSL plugin block.It would be nice if we could unify all the plugins into one style,
    // but the DSL has some restrictions e.g can't be used on the allprojects section. So we should revisit this if there are improvements in Gradle.
    // Version 1.0.2 of this plugin uses capsule:1.0.1
    id "us.kirchmeier.capsule" version "1.0.2"
}

ext {
    corda_revision = org.ajoberstar.grgit.Grgit.open(file('.')).head().id
}

apply plugin: 'project-report'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'net.corda.plugins.publish-utils'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'

// We need the following three lines even though they're inside an allprojects {} block below because otherwise
// IntelliJ gets confused when importing the project and ends up erasing and recreating the .idea directory, along
// with the run configurations. It also doesn't realise that the project is a Java 8 project and misconfigures
// the resulting import. This fixes it.
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8


allprojects {
    apply plugin: 'kotlin'
    apply plugin: 'java'
    apply plugin: 'jacoco'
    apply plugin: 'org.owasp.dependencycheck'
    apply plugin: 'kotlin-allopen'

    allOpen {
        annotations(
                "javax.persistence.Entity",
                "javax.persistence.Embeddable",
                "javax.persistence.MappedSuperclass"
        )
    }

    dependencyCheck {
        suppressionFile = '.ci/dependency-checker/suppressedLibraries.xml'
        cveValidForHours = 1
        format = 'ALL'
    }
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-Xlint:-options" << "-parameters"
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            languageVersion = "1.1"
            apiVersion = "1.1"
            jvmTarget = "1.8"
            javaParameters = true   // Useful for reflection.
        }
    }

    tasks.withType(Jar) { task ->
        // Includes War and Ear
        manifest {
            attributes('Corda-Release-Version': corda_release_version)
            attributes('Corda-Platform-Version': corda_platform_version)
            attributes('Corda-Revision': corda_revision)
            attributes('Corda-Vendor': 'R3 Corda Edition')
            attributes('Automatic-Module-Name': "net.corda.${task.project.name.replaceAll('-', '.')}")
        }
    }

    tasks.withType(Test) {
        // Prevent the project from creating temporary files outside of the build directory.
        systemProperties['java.io.tmpdir'] = buildDir

        // Ensures that "net.corda.testing.amqp.enable" is passed correctly from Gradle command line
        // down to JVM executing unit test. It looks like we are running unit tests in the forked mode
        // and all the "-D" parameters passed to Gradle not making it to unit test level
        // TODO: Remove once we fully switched to AMQP
        final AMQP_ENABLE_PROP_NAME = "net.corda.testing.amqp.enable"
        systemProperty(AMQP_ENABLE_PROP_NAME, System.getProperty(AMQP_ENABLE_PROP_NAME))

         // relational database provider to be used by node
        final DATABASE_PROVIDER = "custom.databaseProvider"
        final DATASOURCE_URL = "corda.dataSourceProperties.dataSource.url"
        final DATASOURCE_CLASSNAME = "corda.dataSourceProperties.dataSourceClassName"
        final DATASOURCE_USER = "corda.dataSourceProperties.dataSource.user"
        final DATASOURCE_PASSWORD = "corda.dataSourceProperties.dataSource.password"

        // integration testing database configuration (to be used in conjunction with a DATABASE_PROVIDER)
        final TEST_DB_ADMIN_USER = "test.db.admin.user"
        final TEST_DB_ADMIN_PASSWORD = "test.db.admin.password"
        final TEST_DB_SCRIPT_DIR = "test.db.script.dir"

        [DATABASE_PROVIDER,DATASOURCE_URL, DATASOURCE_CLASSNAME, DATASOURCE_USER, DATASOURCE_PASSWORD,
          TEST_DB_ADMIN_USER, TEST_DB_ADMIN_PASSWORD, TEST_DB_SCRIPT_DIR].forEach {
            def property = System.getProperty(it)
            if (property != null) {
                systemProperty(it, property)
            }
        }

        if (System.getProperty("test.maxParallelForks") != null) {
            maxParallelForks = Integer.valueOf(System.getProperty("test.maxParallelForks"))
        }

        if (project.path.startsWith(':experimental') && System.getProperty("experimental.test.enable") == null) {
            enabled = false
        }
    }

    group 'com.r3.corda'
    version "$corda_release_version"

    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
        maven { url 'https://jitpack.io' }
        maven { url "$artifactory_contextUrl/corda-releases" } // cordform-common
        if (project.hasProperty("mavenOracleUsername") && project.hasProperty("mavenOraclePassword")) {
            maven {
                // For integrationTest task when running against Oracle database the JDBC driver is in Oracle Maven repository with login access only,
                // setup an account on https://login.oracle.com/oaam_server/login.do
                // provide credentials to Gradle task by -PmavenOracleUsername=... -PmavenOraclePassword=...
                url "https://www.oracle.com/content/secure/maven/content"
                credentials {
                    username = project.property("mavenOracleUsername")
                    password = project.property("mavenOraclePassword")
                }
            }
        }
    }

    configurations {
        compile {
            // We want to use SLF4J's version of these bindings: jcl-over-slf4j
            // Remove any transitive dependency on Apache's version.
            exclude group: 'commons-logging', module: 'commons-logging'
        }
        runtime {
            // We never want isolated.jar on classPath, since we want to test jar being dynamically loaded as an attachment
            exclude module: 'isolated'
        }
    }
}

// Check that we are running on a Java 8 JDK. The source/targetCompatibility values above aren't sufficient to
// guarantee this because those are properties checked by the Java plugin, but we're using Kotlin.
//
// We recommend a specific minor version (unfortunately, not checkable directly) because JavaFX adds APIs in
// minor releases, so we can't work with just any Java 8, it has to be a recent one.
if (!JavaVersion.current().java8Compatible)
    throw new GradleException("Corda requires Java 8, please upgrade to at least 1.8.0_$java8_minUpdateVersion")

repositories {
    mavenCentral()
    jcenter()
}

// Required for building out the fat JAR.
dependencies {
    compile project(':node')
    compile "com.google.guava:guava:$guava_version"

    // Set to corda compile to ensure it exists now deploy nodes no longer relies on build
    compile project(path: ":node:capsule", configuration: 'runtimeArtifacts')
    compile project(path: ":webserver:webcapsule", configuration: 'runtimeArtifacts')

    // For the buildCordappDependenciesJar task
    runtime project(':client:jfx')
    runtime project(':client:mock')
    runtime project(':client:rpc')
    runtime project(':core')
    runtime project(':confidential-identities')
    runtime project(':finance')
    runtime project(':webserver')
    testCompile project(':test-utils')
}

jar {
    // Prevent the root project from building an unwanted dummy CorDapp.
    enabled = false
}

task jacocoRootReport(type: org.gradle.testing.jacoco.tasks.JacocoReport) {
    dependsOn = subprojects.test
    additionalSourceDirs = files(subprojects.sourceSets.main.allSource.srcDirs)
    sourceDirectories = files(subprojects.sourceSets.main.allSource.srcDirs)
    classDirectories = files(subprojects.sourceSets.main.output)
    executionData = files(subprojects.jacocoTestReport.executionData)
    reports {
        html.enabled = true
        xml.enabled = true
        csv.enabled = false
    }
    onlyIf = {
        true
    }
    doFirst {
        executionData = files(executionData.findAll {
            it.exists()
        })
    }
}

tasks.withType(Test) {
    reports.html.destination = file("${reporting.baseDir}/${name}")
}

bintrayConfig {
    // user = System.getenv('CORDA_BINTRAY_USER')
    // key = System.getenv('CORDA_BINTRAY_KEY')
    repo = 'corda'
    org = 'r3'
    licenses = ['Apache-2.0']
    vcsUrl = 'https://github.com/corda/corda'
    projectUrl = 'https://github.com/corda/corda'
    gpgSign = true
    gpgPassphrase = System.getenv('CORDA_BINTRAY_GPG_PASSPHRASE')
    publications = ['corda-jfx', 'corda-mock', 'corda-rpc', 'corda-core', 'corda', 'corda-finance', 'corda-node', 'corda-node-api', 'corda-test-common', 'corda-test-utils', 'corda-jackson', 'corda-verifier', 'corda-webserver-impl', 'corda-webserver', 'corda-node-driver', 'corda-confidential-identities', 'doorman', 'doorman-hsm', 'corda-shell']
    license {
        name = 'Apache-2.0'
        url = 'https://www.apache.org/licenses/LICENSE-2.0'
        distribution = 'repo'
    }
    developer {
        id = 'R3'
        name = 'R3'
        email = 'dev@corda.net'
    }
}

// Build a ZIP of all JARs required to compile the Cordapp template
// Note: corda.jar is used at runtime so no runtime ZIP is necessary.
// Resulting ZIP can be found in "build/distributions"
task buildCordappDependenciesZip(type: Zip) {
    baseName 'corda-deps'
    from configurations.runtime
    from configurations.compile
    from configurations.testCompile
    from buildscript.configurations.classpath
    from 'node/capsule/NOTICE' // CDDL notice
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

artifactory {
    publish {
        contextUrl = artifactory_contextUrl
        repository {
            repoKey = 'enterprise-dev'
            username = 'teamcity'
            password = System.getenv('CORDA_ARTIFACTORY_PASSWORD')
        }

        defaults {
            // Root project applies the plugin (for this block) but does not need to be published
            if(project != rootProject) {
                publications(project.extensions.publish.name())
            }
        }
    }
}

task generateApi(type: net.corda.plugins.GenerateApi){
    baseName = "api-corda"
}

// This exists to reduce CI build time when the envvar is set (can save up to 40 minutes)
if(file('corda-docs-only-build').exists() || (System.getenv('CORDA_DOCS_ONLY_BUILD') != null)) {
    if(file('corda-docs-only-build').exists()) {
        logger.info("Tests are disabled due to presence of file 'corda-docs-only-build' in the project root")
    } else {
        logger.info("Tests are disabled due to the presence of envvar CORDA_DOCS_ONLY_BUILD")
    }

    allprojects {
        test {
            exclude '*/**'
        }

        it.afterEvaluate {
            if(it.tasks.findByName("integrationTest") != null) {
                integrationTest {
                    exclude '*/**'
                }
            }
        }

        it.afterEvaluate {
            if(it.tasks.findByName("smokeTest") != null) {
                smokeTest {
                    exclude '*/**'
                }
            }
        }
    }
}