/*
 * Copyright 2017-2021 EPAM Systems, Inc. (https://www.epam.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import de.undercouch.gradle.tasks.download.Download

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id "de.undercouch.download" version "3.1.2"
}

apply plugin: "java"
apply plugin: "jacoco"
apply plugin: "org.springframework.boot"

repositories {
    mavenCentral()
}


dependencies {
    configurations {
        all*.exclude module : "spring-boot-starter-logging"
    }

    implementation project(":cloud-pipeline-common:model")

    compileOnly group: "org.projectlombok", name: "lombok", version: lombokVersion

    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: log4jVersion
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion
    implementation group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: log4jVersion

    // Spring
    compile group: "org.springframework.boot", name: "spring-boot-starter-web",         version: springBootVersion
    compile group: "org.springframework.boot", name: "spring-boot-starter-tomcat",      version: springBootVersion
    compile group: "org.springframework.boot", name: "spring-boot-starter-data-jpa",    version: springBootVersion
    compile group: "org.springframework.boot", name: "spring-boot-starter-security",    version: springBootVersion

    // DB
    compile group: "com.h2database",    name: "h2",     version: "1.4.197"
    compile group: "com.mchange",       name: "c3p0",   version: "0.9.5.2"

    // Swagger
    compile group: "io.springfox", name: "springfox-swagger2",      version: swaggerVersion
    compile group: "io.springfox", name: "springfox-swagger-ui",    version: swaggerVersion

    // Mapstruct
    compile group: "org.mapstruct", name: "mapstruct-jdk8", version: mapstructVersion
    compile group: "org.mapstruct", name: "mapstruct-processor", version: mapstructVersion

    // Security JWT
    compile group: "com.auth0", name: "java-jwt", version: "3.3.0"

    // Apache Commons
    compile group: "commons-validator",     name: "commons-validator",      version: "1.5.0"
    compile group: "org.apache.commons",    name: "commons-math3",          version: "3.6.1"
    compile group: "org.apache.commons",    name: "commons-text",           version: "1.4"
    compile group: "org.apache.commons", name: "commons-lang3", version: "3.8"
    compile group: "org.apache.commons", name: "commons-collections4", version: "4.0"
    compile group: "commons-io", name: "commons-io", version: "2.6"

    // Google Cloud Storage SDK
    compile group: "com.google.cloud", name: "google-cloud-storage", version: "1.34.0"

    // Thymeleaf for template
    compile group: "org.thymeleaf", name: "thymeleaf", version: "3.0.9.RELEASE"

    //Retrofit
    implementation group: "com.squareup.retrofit2", name: "retrofit",           version: retrofitVersion
    implementation group: "com.squareup.retrofit2", name: "converter-jackson",  version: retrofitVersion

    //XML parsing
    implementation group: "com.fasterxml.jackson.dataformat",   name: "jackson-dataformat-xml", version: "2.9.6"
    implementation group: "com.fasterxml.woodstox",             name: "woodstox-core",          version: "5.1.0"

    // Tests
    implementation  group: "org.springframework.boot",  name: "spring-boot-starter-test",   version: springBootVersion
    testCompile     group: "com.github.marschall",      name: "memoryfilesystem",           version: "1.2.1"
    testCompile group: "org.projectlombok", name: "lombok", version: lombokVersion
    testCompile group: "org.junit.jupiter", name: "junit-jupiter-api", version: junitVersion
    testRuntime group: "org.junit.jupiter", name: "junit-jupiter-engine", version: junitVersion
    testImplementation group: "org.hamcrest", name: "hamcrest-all", version: hamcrestVersion
    testImplementation group: "org.mockito", name: "mockito-core", version: mockitoVersion
    testImplementation group: "org.mockito", name: "mockito-junit-jupiter", version: mockitoVersion

}

bootJar {
    archiveName "data-transfer-service.jar"
    manifest {
        attributes(
                "Implementation-Version": "$version")
    }
}

test {
    useJUnitPlatform()

    reports {
        html.enabled = true
    }
}

jacoco {
    toolVersion = "0.7.9"
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
    }
}

def jreURL = "https://cloud-pipeline-oss-builds.s3.amazonaws.com/tools/java/"
def windowsJre = "OpenJDK8U-jre_x64_windows_hotspot_8u292b10.zip"
def windowsJrePath = "jdk8u292-b10-jre"
def linuxJre = "openjdk-1.8.0_222_linux-x64_bin.tar.gz"
def linuxJrePath = "openjdk-8u222-b10"

task customStartScriptsWin(type: CreateStartScripts) {
    applicationName = 'dts'
    mainClassName = 'org.springframework.boot.loader.JarLauncher'
    classpath = files('data-transfer-service.jar')
    defaultJvmOpts = ["-Xms512m", "-Xmx2g"]
    outputDir = file("$buildDir/scripts")
    doLast {
        windowsScript.text = windowsScript.text.replace("set JAVA_EXE=%JAVA_HOME%/bin/java.exe",
                "set JAVA_EXE=%APP_HOME%/jre/bin/java.exe")
    }
}

task downloadJreWin(type: Download) {
    dependsOn bootJar
    src jreURL + windowsJre
    dest "$buildDir/downloads/$windowsJre"
    overwrite true
}

task unpackJreWin(type: Copy) {
    dependsOn downloadJreWin
    from(zipTree("$buildDir/downloads/$windowsJre"))
    into("$buildDir/downloads")
}

task bundleWindows(type: Zip) {
    dependsOn unpackJreWin, customStartScriptsWin, bootJar
    from ("$buildDir/downloads/$windowsJrePath") {
        include '**/*'
        into('jre')
    }
    from("$buildDir/libs") {
        include '**/*.jar'
        into('lib')
    }
    from("$buildDir/scripts") {
        into('bin')
    }
    baseName = 'dts'
    archiveName = 'data-transfer-service-windows.zip'
}

task customStartScriptsLinux(type: CreateStartScripts) {
    applicationName = 'dts'
    mainClassName = 'org.springframework.boot.loader.JarLauncher'
    classpath = files('data-transfer-service.jar')
    defaultJvmOpts = ["-Xms512m", "-Xmx2g"]
    outputDir = file("$buildDir/scripts")
    doLast {
        unixScript.text = unixScript.text.replace("# Determine the Java command to use to start the JVM.",
                "export JAVA_HOME=\$APP_HOME/jre/")
    }
}

task downloadJreLinux(type: Download) {
    dependsOn bootJar
    src jreURL + linuxJre
    dest "$buildDir/downloads/$linuxJre"
    overwrite true
}

task unpackJreLinux(type: Copy) {
    dependsOn downloadJreLinux
    from(tarTree(resources.gzip("$buildDir/downloads/$linuxJre")))
    into("$buildDir/downloads")
}

task bundleLinux(type: Zip) {
    dependsOn unpackJreLinux, customStartScriptsLinux, bootJar
    from ("$buildDir/downloads/$linuxJrePath") {
        include '**/*'
        into('jre')
    }
    from("$buildDir/libs") {
        include '**/*.jar'
        into('lib')
    }
    from("$buildDir/scripts") {
        into('bin')
    }
    baseName = 'dts'
    archiveName = 'data-transfer-service-linux.zip'
}

