plugins {
    id 'java'
    id 'java-library'
    id 'application'
    id 'idea'

    // Creates fat JAR
    id 'com.github.johnrengelman.shadow' version '7.1.2'

    // Downloads binary dependencies
    id 'de.undercouch.download' version '5.2.1'
}

def gradleDependencyVersion = '7.4'

wrapper {
    gradleVersion = gradleDependencyVersion
    distributionType = Wrapper.DistributionType.ALL
}

// Use the GGProvisioner main class when the JAR is invoked directly
mainClassName = 'com.awslabs.aws.greengrass.provisioner.AwsGreengrassProvisioner'

distZip.enabled = shadowDistZip.enabled = false
distTar.enabled = shadowDistTar.enabled = false

// Specify all of our dependency versions
def daggerVersion = '2.44'
def configVersion = '1.4.2'
def ztZipVersion = '1.15'
def jcommanderVersion = '1.82'
def slf4jVersion = '2.0.3'
def jcabiVersion = '0.22.0'
def jtarVersion = '2.3'
def gsonVersion = '2.9.1'
def dockerClientVersion = '8.16.0'
def awsSdk2Version = '2.17.291'
def mavenInvokerVersion = '3.2.0'
def commonsTextVersion = '1.10.0'
def commonsLangVersion = '3.12.0'
def commonsIoVersion = '2.11.0'
def junitVersion = '4.13.2'
def systemRulesVersion = '1.19.0'
def mockitoVersion = '4.8.0'
def vavrVersion = '0.10.4'
def httpClientVersion = '4.5.13'
def jodahFailsafeVersion = '2.4.4'
def testContainersVersion = '1.17.5'
def hamcrestVersion = '2.2'
def awsIotCoreWebsocketsVersion = '4.0.1'
def awaitilityVersion = '4.2.0'
def immutablesValueVersion = '2.9.2'
def awsLambdaJavaCoreVersion = '1.2.1'
def resultsIteratorForAwsJavaSdkVersion = '11.0.2'
def nomenEstOmenVersion = '2.1.0'
def jschVersion = '0.1.55'
def jetbrainsAnnotationVersion = '23.0.0'

def buildDirDist = "$buildDir/dist"
def buildDirFoundation = "$buildDir/foundation"

def greengrassVersion = '1.11.0'

task downloadGreengrassBinaries(type: Download) {
    // Download the Greengrass binaries into the dist directory so they can be packaged into the JAR
    src(
            ["https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-linux-aarch64-${greengrassVersion}.tar.gz",
             "https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-openwrt-aarch64-${greengrassVersion}.tar.gz",
             "https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-linux-armv7l-${greengrassVersion}.tar.gz",
             "https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-openwrt-armv7l-${greengrassVersion}.tar.gz",
             "https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-linux-armv6l-${greengrassVersion}.tar.gz",
             "https://d1onfpft10uf5o.cloudfront.net/greengrass-core/downloads/$greengrassVersion/greengrass-linux-x86-64-${greengrassVersion}.tar.gz"]
    )
    dest "$buildDirDist"
    overwrite false
}

task downloadAll {
    dependsOn downloadGreengrassBinaries
}

assemble.dependsOn downloadAll
check.dependsOn downloadAll

group = 'com.awslabs.aws.greengrass.provisioner.AwsGreengrassProvisioner'
version = '1.0-SNAPSHOT'

description = """"""

shadowJar {
    // Create a shadow JAR with all of the necessary dependencies
    archiveFileName = 'AwsGreengrassProvisioner.jar'

    // Include the "dist" files which are the SDKs and Greengrass binaries
    into('/dist') {
        from fileTree("$buildDirDist")
    }

    // Include the foundation for the Java functions
    into('/foundation') {
        from fileTree("$buildDirFoundation")
    }

    zip64 true
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    mavenCentral()

    maven { url "https://plugins.gradle.org/m2/" }

    // Required for Gradle tooling API
    maven { url "https://repo.gradle.org/gradle/libs-releases-local/" }

    // Jitpack for MQTT over WebSockets support
    maven { url 'https://jitpack.io' }
}

sourceSets {
    integrationTest {
        java {
            compileClasspath += sourceSets.main.output
            runtimeClasspath += sourceSets.main.output

            srcDir file('src/integration-test/java')
        }
    }
}

configurations {
    integrationTestImplementation.extendsFrom implementation
    integrationTestApi.extendsFrom api
}

task integrationTest(type: Test) {
    description = 'Runs the integration tests.'
    group = 'verification'
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    outputs.upToDateWhen { false }
    mustRunAfter test
}

dependencies {
    // Dagger code generation
    annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"

    // Immutables (requires annotation processing for code generation)
    annotationProcessor "org.immutables:value:$immutablesValueVersion"
    annotationProcessor "org.immutables:gson:$immutablesValueVersion"
    implementation "org.immutables:value:$immutablesValueVersion"
    implementation "org.immutables:gson:$immutablesValueVersion"

    // Dependency injection with Dagger
    implementation "com.google.dagger:dagger:$daggerVersion"

    implementation "com.typesafe:config:$configVersion"
    implementation "org.zeroturnaround:zt-zip:$ztZipVersion"
    implementation "com.beust:jcommander:$jcommanderVersion"
    implementation "org.slf4j:slf4j-log4j12:$slf4jVersion"
    implementation "com.jcabi:jcabi-log:$jcabiVersion"
    implementation "org.kamranzafar:jtar:$jtarVersion"
    implementation "com.google.code.gson:gson:$gsonVersion"
    implementation "com.spotify:docker-client:$dockerClientVersion"

    // For Docker-style names
    implementation "com.oblac:nomen-est-omen:$nomenEstOmenVersion"

    // For scp/ssh support
    implementation "com.jcraft:jsch:$jschVersion"

    implementation "software.amazon.awssdk:greengrass:$awsSdk2Version"
    implementation "software.amazon.awssdk:iam:$awsSdk2Version"
    implementation "software.amazon.awssdk:iot:$awsSdk2Version"
    implementation "software.amazon.awssdk:lambda:$awsSdk2Version"
    implementation "software.amazon.awssdk:cloudformation:$awsSdk2Version"
    implementation "software.amazon.awssdk:sts:$awsSdk2Version"
    implementation "software.amazon.awssdk:ecr:$awsSdk2Version"
    implementation "software.amazon.awssdk:ec2:$awsSdk2Version"
    implementation "software.amazon.awssdk:s3:$awsSdk2Version"
    implementation "software.amazon.awssdk:cloudwatchlogs:$awsSdk2Version"
    implementation "software.amazon.awssdk:secretsmanager:$awsSdk2Version"
    implementation "org.apache.maven.shared:maven-invoker:$mavenInvokerVersion"
    implementation "org.apache.commons:commons-text:$commonsTextVersion"
    implementation "org.apache.commons:commons-lang3:$commonsLangVersion"
    implementation "org.gradle:gradle-tooling-api:7.1.1"
    implementation "io.vavr:vavr:$vavrVersion"

    implementation "commons-io:commons-io:$commonsIoVersion"
    implementation "com.github.awslabs:results-iterator-for-aws-java-sdk:$resultsIteratorForAwsJavaSdkVersion"

    implementation "org.jetbrains:annotations:$jetbrainsAnnotationVersion"

    // Dependency added to fix - https://github.com/aws/aws-sdk-java-v2/issues/652
    implementation "org.apache.httpcomponents:httpclient:$httpClientVersion"

    // https://mvnrepository.com/artifact/net.jodah/failsafe
    implementation "net.jodah:failsafe:$jodahFailsafeVersion"

    implementation "org.testcontainers:testcontainers:$testContainersVersion"

    implementation "org.awaitility:awaitility:$awaitilityVersion"

    // To support AWS Lambda invocations
    implementation "com.amazonaws:aws-lambda-java-core:$awsLambdaJavaCoreVersion"

    testImplementation "junit:junit:$junitVersion"
    testImplementation "com.github.stefanbirkner:system-rules:$systemRulesVersion"
    testImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
    testImplementation "org.mockito:mockito-core:$mockitoVersion"

    integrationTestImplementation "junit:junit:$junitVersion"
    integrationTestImplementation "com.github.stefanbirkner:system-rules:$systemRulesVersion"
    integrationTestImplementation "org.hamcrest:hamcrest:$hamcrestVersion"
    integrationTestImplementation "org.mockito:mockito-core:$mockitoVersion"
    integrationTestImplementation "com.github.awslabs:aws-iot-core-websockets:$awsIotCoreWebsocketsVersion"
}
