buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.bdware.bdcontract:simple-ypk-packer:0.5.5"
        classpath "org.bdware.bdcontract:ypk-deploy-tool:0.7.1"
    }
}
plugins {
    id 'java'
    id 'java-library'
}

tasks.withType(JavaCompile) {
    options.compilerArgs << '-Xlint:none'
    options.compilerArgs << '-Xlint:deprecation' << "-Werror"
}

sourceSets {
    main {
        java {
            srcDirs 'src/main/java'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
    test {
        java {
            srcDir 'src/test/java'
        }
        resources {
            srcDir 'src/test/resources'
        }
    }
}

dependencies {
    api 'org.apache.logging.log4j:log4j-core:2.17.2'
    api 'org.apache.logging.log4j:log4j-api:2.17.2'
    implementation 'org.postgresql:postgresql:42.5.4'
    implementation 'org.bdware.doip:irp-sdk:1.1.3'

    implementation 'org.bdware.sc:cp:1.6.8'
    testImplementation 'junit:junit:4.13.2'
}

task copyLibs(type: Copy) {
    from configurations.runtimeClasspath
    into "./build/output/libs/"
}

task copyYJS(type: Copy) {
    from "./yjs"
    into "./build/output/"
}

task sourcesJar(type: Jar) {
    from sourceSets.main.allJava
    archiveClassifier = 'sources'
}
task copyAssets(type: Copy) {
    from "../front"
    into "./build/output/assets/"
}

task copyJar(type: Copy, dependsOn: [":backend:jar", ":backend:copyLibs"]) {
    from "./build/libs/backend.jar"
    into "./build/output"
    rename { String fileName -> "gdrouter.jar" }
    doFirst {
        println "copyJar start"
    }
}
def reltivePath = "./backend"
//reltivePath="."
def currVersion = "0.0.1"
task grepCP(dependsOn: ["copyJar"]) {
    doLast {
        org.bdware.datanet.YPKPacker.grepJarByCPVersion("${reltivePath}/build/output/libs", org.bdware.datanet.CPVersion.cp_1_6_8)
        org.bdware.datanet.YPKPacker.grepJarByListFile("${reltivePath}/build/output/libs", "${reltivePath}/grepcp.list")

    }
}
tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
tasks.processTestResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)

task buildZip(type: Zip, dependsOn: ["copyAssets", "copyJar", "copyYJS", "grepCP"]) {
    from './build/output/'
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    archiveFileName = 'gdrouter.zip'
    destinationDirectory = file('build/')
}

task buildYPK(dependsOn: ["buildZip"]) {
    doLast {
        org.bdware.datanet.YPKPacker.staticPack("${reltivePath}/build/gdrouter.zip", "${reltivePath}/build/gdrouter-${currVersion}.ypk")
    }
}
task deploy(dependsOn: ["buildYPK"]) {
    doLast {
        org.bdware.ypkdeploy.HTTPTool.deployWithYpk("${reltivePath}/debugconf.json", "${reltivePath}/build/gdrouter-${currVersion}.ypk")
    }
}