plugins {
    id 'java'
    id 'application'
}

mainClassName = 'org.bdware.server.NodeCenterServer'

application {
    mainClass = mainClassName
    applicationDefaultJvmArgs = ['-Dfile.encoding=UTF-8']
}

dependencies {
    implementation project(":front-base")
    // https://mvnrepository.com/artifact/com.jianggujin/IKAnalyzer-lucene
    implementation 'com.jianggujin:IKAnalyzer-lucene:8.0.0'
    // https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common
    implementation 'org.apache.lucene:lucene-analyzers-common:8.10.1'
    implementation 'org.apache.lucene:lucene-analyzers-smartcn:8.10.1'
    // https://mvnrepository.com/artifact/org.apache.lucene/lucene-backward-codecs
    implementation 'org.apache.lucene:lucene-backward-codecs:8.10.1'
    // https://mvnrepository.com/artifact/org.apache.lucene/lucene-core
    implementation 'org.apache.lucene:lucene-core:8.10.1'
    // https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser
    implementation 'org.apache.lucene:lucene-queryparser:8.10.1'
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.jcraft:jsch:0.1.55'
}

jar {
    String libs = ''
    configurations.runtimeClasspath.each {
        libs = libs + " libs/" + it.name
    }

    manifest {
        attributes 'Manifest-Version': project.version
        attributes 'Main-Class': mainClassName
        attributes 'Class-Path': libs
    }
}

tasks.processResources.setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)

task prepareWebContent(type: Exec) {
    if (System.properties['os.name'].toString().toLowerCase().contains('windows')) {
        commandLine "rmdir", ".\\WebContent"
        commandLine "mklink", "/j", "..\\router-frontend\\src", ".\\WebContent"
    } else {
        commandLine "rm", "./WebContent"
        commandLine "ln", "-s", "../router-frontend/src", "./WebContent"
    }
}

task copyScript(type: Copy) {
    from("./script/") {
        include 'ncstart.sh'
        include 'ncstop.sh'
        include 'ncconfig.json.template'
        include 'updateCluster.sh'
        include 'log4j2.properties'
    }
    into "./build/output"
    println("copyScript done !")
}

task copySsl(type: Copy) {
    from './ssl'
    into './build/output/ssl'
}

task copyLibs(type: Copy, dependsOn: ["copyScript", 'copySsl']) {
    from configurations.runtimeClasspath
    into "./build/output/libs/"
}

//task copyJar(type: Exec, dependsOn: [":router-backend:jar", ":router-backend:copyLibs"]) {
//    println("copyJar start")
//    commandLine "cp", "./build/libs/$project.name-$version" + ".jar", "./build/output/bdcluster.jar"
//}

task copyJar(type: Copy, dependsOn: ["jar", "copyLibs"]) {
    from "./build/libs/$project.name-${project.version}.jar"
    into "./build/output"
    rename { String fileName -> "bdcluster.jar" }
    doFirst {
        println "copyJar start"
    }
}

task copyWebContent(type: Copy) {
    from("./WebContent") {
        exclude 'client/BaaSClient.html'
        exclude 'client/README.md'
        exclude 'client/.idea/'
        exclude 'client/.gitignore'
        exclude '.idea/'
    }
    into "./build/output/WebContent"
}

task buildBDServerClusterZip(type: Zip, dependsOn: ["copyWebContent", "copyScript", "copyJar"]) {
    from './build/output/'
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    archiveFileName = 'bdcluster-all.zip'
    destinationDirectory = file('build/')
}

task buildBDClusterZip(type: Zip, dependsOn: ["buildBDServerClusterZip"]) {
    from('./build/output/') {
        include("libs/*")
        include("bdcluster.jar")
    }
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    archiveFileName = 'bdcluster.zip'
    destinationDirectory = file('build/')
}

task buildWebContentZip(type: Zip, dependsOn: ["copyWebContent"]) {
    from "./build/output/WebContent"
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    archiveFileName = 'ClusterWebContent.zip'
    destinationDirectory = file('build/')
}

task buildBundle(dependsOn: ["buildBDClusterZip", "buildWebContentZip"]) {
    doLast {
        println "buildBundle in ./build/output/ successfully"
    }
}