/*
 * Copyright Thoughtworks, Inc.
 *
 * 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 com.thoughtworks.go.build.VerifyJarTask

plugins {
  id 'java-library'
}

description = 'GoCD Server Launcher'

configurations {
  extractedAtTopLevel { transitive = false }
  fatJarConfig
  packagingInLibDir.extendsFrom packagingOnly
  packagingInDefaultFilesDir.extendsFrom packagingOnly

  packagingInMainJarTopLevel {
    transitive = false
  }

  packagingInMainJarCommonJar {
    transitive = false
  }

  packagingInMainJarBaseJar {
    transitive = false
  }

  packagingInMainJarServerJar {
    transitive = false
  }

  pluginsZip.extendsFrom packagingOnly
}

dependencies {
  packagingInLibDir platform(project(':build-platform'))

  packagingInDefaultFilesDir project(path: ':agent-launcher', configuration: 'fatJarConfig')
  packagingInDefaultFilesDir project(path: ':agent', configuration: 'fatJarConfig')
  packagingInDefaultFilesDir project(path: ':server', configuration: 'warConfig')
  packagingInDefaultFilesDir project(path: ':tfs-impl:tfs-impl-14', configuration: 'fatJarConfig')

  packagingInLibDir project.deps.jetty
  packagingInLibDir project.deps.jettyJmx
  packagingInLibDir project.deps.jettyServlets
  packagingInLibDir project.deps.jettyUtil
  packagingInLibDir project.deps.jettyDeploy
  packagingInLibDir project.deps.jettyWebsocket

  packagingInLibDir(project(':plugin-infra:go-plugin-activator')) {
    transitive = false
  }
  packagingInLibDir project.deps.servletApi
  packagingInLibDir project.deps.commonsIO

  // Not needed for compilation, but we want to centralize all logging using logback
  packagingInLibDir project.deps.slf4jApi
  packagingInLibDir project.deps.slf4jJcl
  packagingInLibDir project.deps.slf4jJul
  packagingInLibDir project.deps.logback

  pluginsZip project(path: ':tw-go-plugins', configuration: 'pluginsZipConfig')

  packagingInMainJarTopLevel project(':jetty')
  packagingInMainJarTopLevel project(':app-server')
  packagingInMainJarCommonJar project(':common')
  packagingInMainJarBaseJar project(':base')
  packagingInMainJarServerJar project(':server')

  extractedAtTopLevel project(':jar-class-loader')
}

tasks.register('mainJar', Jar) {
  dependsOn configurations.packagingInMainJarBaseJar, configurations.packagingInMainJarCommonJar, configurations.packagingInMainJarTopLevel, configurations.packagingInMainJarServerJar
  archiveClassifier = 'main'

  from({ configurations.packagingInMainJarTopLevel.collect { it.isDirectory() ? it : zipTree(it) } }) {
    into('/')
  }

  from({ configurations.packagingInMainJarServerJar.collect { it.isDirectory() ? it : zipTree(it) } }) {
    include "**/GoLauncher*.class"
    include "**/GoServer*.class"

    into('/')
  }

  from({ configurations.packagingInMainJarCommonJar.collect { it.isDirectory() ? it : zipTree(it) } }) {
    include "**/SubprocessLogger*.class"
    include "**/CurrentProcess*.class"
    include "**/validators/*.class"

    into('/')
  }

  from({ configurations.packagingInMainJarBaseJar.collect { it.isDirectory() ? it : zipTree(it) } }) {
    include "**/LogHelper.class"
    include "**/LogConfigurator*.class"
    include "**/SystemEnvironment*.class"
    include "**/ConfigDirProvider.class"
    include "**/validators/*.class"
    include "**/SystemUtil.class"

    into('/')
  }

  from("${project(':server').projectDir}/src/main/resources/config/logback.xml") { into 'config' }
}

clean {
  delete 'defaultFiles'
}

def fatJar = tasks.register('fatJar', Jar) {
  finalizedBy 'verifyFatJar'
  dependsOn ':server:jar'
  dependsOn ':server:war'
  dependsOn configurations.packagingInDefaultFilesDir, configurations.pluginsZip,
    configurations.packagingInLibDir, mainJar, configurations.extractedAtTopLevel

  manifest {
    attributes 'Main-Class': 'com.thoughtworks.gocd.Boot'
    attributes 'GoCD-Main-Class': 'com.thoughtworks.go.server.util.GoLauncher'
  }

  from({ configurations.packagingInDefaultFilesDir }) {
    into "defaultFiles/"
    rename "(.*)-${project.version}.(.*)", '$1.$2'
  }

  from(project(':server').war.archiveFile) {
    into "defaultFiles/"
    rename "(.*)-${project.version}.(.*)", '$1.$2'
  }

  from("${project(':installers').projectDir}/go-server/release/cruise-config.xml") { into 'defaultFiles/config' }
  from("${project(':server').projectDir}/config/jetty.xml") { into 'defaultFiles/config' }
  from("${project(':server').projectDir}/config/go_update_server.pub") { into 'defaultFiles/config' }
  from("${project(':config:config-server').projectDir}/src/main/resources/cruise-config.xsd") { into 'defaultFiles/config' }
  from({ configurations.pluginsZip }) { into 'defaultFiles' rename "(.*)-${project.version}.zip", '$1.zip' }

  from({ configurations.packagingInLibDir }) {
    into 'lib/'
    rename "go-plugin-activator-${project.version}.jar", "go-plugin-activator.jar"
  }

  from(mainJar.archiveFile) {
    into('lib/')
  }

  from({ configurations.extractedAtTopLevel.collect { it.isDirectory() ? it : zipTree(it) } }) {
    into("/")
  }

  from("${rootProject.rootDir}/LICENSE") {
    into "/"
  }
}

tasks.register('verifyFatJar', VerifyJarTask) {
  jarTask = fatJar
  expectedJars = [
    "defaultFiles": [
      "agent-launcher.jar",
      "agent.jar",
      "plugins.zip",
      "tfs-impl-14.jar",
    ],
    "lib"         : [
      "commons-io-${project.versions.commonsIO}.jar",
      "go-plugin-activator.jar",
      "jakarta.servlet-api-${project.versions.servletApi}.jar",
      "jcl-over-slf4j-${project.versions.slf4jBom}.jar",
      "jetty-deploy-${project.versions.jetty}.jar",
      "jetty-http-${project.versions.jetty}.jar",
      "jetty-io-${project.versions.jetty}.jar",
      "jetty-jmx-${project.versions.jetty}.jar",
      "jetty-security-${project.versions.jetty}.jar",
      "jetty-server-${project.versions.jetty}.jar",
      "jetty-servlet-${project.versions.jetty}.jar",
      "jetty-servlets-${project.versions.jetty}.jar",
      "jetty-util-${project.versions.jetty}.jar",
      "jetty-webapp-${project.versions.jetty}.jar",
      "jetty-xml-${project.versions.jetty}.jar",
      "jul-to-slf4j-${project.versions.slf4jBom}.jar",
      "logback-classic-${project.versions.logback}.jar",
      "logback-core-${project.versions.logback}.jar",
      "server-launcher-${project.version}-main.jar",
      "slf4j-api-${project.versions.slf4jBom}.jar",
      "websocket-core-common-${project.versions.jetty}.jar",
      "websocket-core-server-${project.versions.jetty}.jar",
      "websocket-jetty-api-${project.versions.jetty}.jar",
      "websocket-jetty-common-${project.versions.jetty}.jar",
      "websocket-jetty-server-${project.versions.jetty}.jar",
      "websocket-servlet-${project.versions.jetty}.jar",
    ]
  ]
}

artifacts {
  fatJarConfig fatJar
}
