buildscript {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        classpath "org.bdware.bdcontract:simple-ypk-packer:${YPKPackerVersion}"
        classpath "org.bdware.bdcontract:ypk-deploy-tool:${DeployToolVersion}"
    }
}

plugins {
    id 'java'
    id 'java-library'
}
version = "1.4.0"
group = 'org.bdware.sc.controlproxy'
var moduleName = "control-proxy-backend"


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'
    api 'com.google.code.gson:gson:2.8.8'
    implementation "org.bdware.sc:cp:${CPVersion}"
    implementation "org.bdware.doip:doip-audit-tool:${AuditToolVersion}"
    implementation 'org.bdware.bdcontract:sdk-java:1.0.2'
    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.bdware.bdcontract:ypk-deploy-tool:${DeployToolVersion}"
}

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: [":${moduleName}:jar", ":${moduleName}:copyLibs"]) {
    from "./build/libs/$project.name-${project.version}.jar"
    into "./build/output"
    rename { String fileName -> "ControlProxy.jar" }
    doFirst {
        println "copyJar start"
    }

}
tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
tasks.processTestResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
task grepCP(dependsOn: [":${moduleName}:copyJar"]) {
    doLast {
        org.bdware.datanet.YPKPacker.grepJarByCPVersion("./${moduleName}/build/output/libs", org.bdware.datanet.CPVersion.cp_1_6_8)
        org.bdware.datanet.YPKPacker.grepJarByListFile("./${moduleName}/build/output/libs", "./grepcp.list")
    }
}

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

task buildControlProxyYPK(dependsOn: ["buildControlProxyZip", "grepCP"]) {
    doLast {
        org.bdware.datanet.YPKPacker.staticPack("./${moduleName}/build/ControlProxy.zip", "./${moduleName}/build/ShanxiControlProxy-${project.version}.ypk")
    }
}

task deployControlProxy(dependsOn: ["buildControlProxyYPK"]) {
    doLast {
        org.bdware.ypkdeploy.HTTPTool.deployWithYpk("./${moduleName}/deployconfig.json", "./${moduleName}/build/ShanxiControlProxy-${project.version}.ypk")
        //org.bdware.ypkdeploy.HTTPTool.batchRun("./backend/deployconfig.json", true)
    }
}

task putControlProxyPackage(type: Exec, dependsOn: ["buildControlProxyYPK"]) {
    workingDir "./build"
    commandLine "pwd"
    commandLine "curl", "--user", "${REPO_USRNAME}:${REPO_PASSWORD}", "--upload-file", "./ShanxiControlProxy-${project.version}.ypk", "${REPO_HOST}api/packages/iod/generic/ShanxiControlProxy/${project.version}/ShanxiControlProxy-${project.version}.ypk"
    doLast {
        println "Executed!"
    }
}