/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

plugins {
  id 'standard-subproject-configuration'
  id 'maven-publish'
}
evaluationDependsOn(':extensions:geode-modules')
evaluationDependsOn(':extensions:geode-modules-tomcat7')
evaluationDependsOn(':extensions:geode-modules-tomcat8')
evaluationDependsOn(':extensions:geode-modules-tomcat9')
evaluationDependsOn(':extensions:geode-modules-session')
evaluationDependsOn(':extensions:geode-modules-session-internal')


import org.apache.geode.gradle.plugins.DependencyConstraints
import org.apache.tools.ant.filters.ReplaceTokens

configurations {
  slf4jDeps
}

dependencies {
  slf4jDeps 'org.slf4j:slf4j-api:' + DependencyConstraints.get('slf4j-api.version')
  slf4jDeps 'org.slf4j:slf4j-jdk14:' + DependencyConstraints.get('slf4j-api.version')
}

jar.enabled = true

def moduleBaseName = "Apache_Geode_Modules"

def configureTcServerAssembly = {
  archiveBaseName = moduleBaseName
  classifier = "tcServer"

  // All client-server files
  into('geode-cs/lib') {
    from project(':extensions:geode-modules').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat7').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat8').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat9').tasks.named('jar')
    from configurations.slf4jDeps
  }

  into('geode-cs/bin') {
    from('release/scripts') {
      include '*'
    }
  }

  into('geode-cs/conf') {
    from('release/conf') {
      exclude 'cache-peer.xml'
    }
    from('release/tcserver/geode-cs') {
      include 'context-fragment.xml'
      include 'server-fragment.xml'
    }
  }

  into('geode-cs') {
    from('release/tcserver/geode-cs') {
      include 'configuration-prompts.properties'
    }
  }

  // Tomncat 7 specifics
  into('geode-cs-tomcat-7/conf') {
    from('release/tcserver/geode-cs-tomcat-7') {
      include 'context-fragment.xml'
    }
  }

  // All peer-to-peer files
  into('geode-p2p/lib') {
    from project(':extensions:geode-modules').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat7').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat8').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat9').tasks.named('jar')
    from configurations.slf4jDeps
  }

  into('geode-p2p/bin') {
    from('release/scripts') {
      include 'gemfire.*'
      include 'setenv.properties'
    }
  }

  into('geode-p2p/conf') {
    from('release/conf') {
      include 'cache-peer.xml'
    }
    from('release/tcserver/geode-p2p') {
      include 'context-fragment.xml'
      include 'server-fragment.xml'
    }
  }

  into('geode-p2p') {
    from('release/tcserver/geode-p2p') {
      include 'configuration-prompts.properties'
    }
  }

  // Tomncat 7 specifics
  into('geode-p2p-tomcat-7/conf') {
    from('release/tcserver/geode-p2p-tomcat-7') {
      include 'context-fragment.xml'
    }
  }
}

def configureTcServer30Assembly = {
  archiveBaseName = moduleBaseName
  classifier = "tcServer30"

  into('geode-cs-tomcat-8/conf') {
    from('release/tcserver/geode-cs-tomcat-8') {
      include 'context-fragment.xml'
    }
  }

  into('geode-cs-tomcat-85/conf') {
    from('release/tcserver/geode-cs-tomcat-85') {
      include 'context-fragment.xml'
    }
  }

  into('geode-cs-tomcat-9/conf') {
    from('release/tcserver/geode-cs-tomcat-9') {
      include 'context-fragment.xml'
    }
  }

  into('geode-p2p-tomcat-8/conf') {
    from('release/tcserver/geode-p2p-tomcat-8') {
      include 'context-fragment.xml'
    }
  }

  into('geode-p2p-tomcat-85/conf') {
    from('release/tcserver/geode-p2p-tomcat-85') {
      include 'context-fragment.xml'
    }
  }

  into('geode-p2p-tomcat-9/conf') {
    from('release/tcserver/geode-p2p-tomcat-9') {
      include 'context-fragment.xml'
    }
  }
}

tasks.register('distTomcat', Zip) {
  archiveBaseName = moduleBaseName
  classifier = "Tomcat"

  // All client-server files
  into('lib') {
    from project(':extensions:geode-modules').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat7').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat8').tasks.named('jar')
    from project(':extensions:geode-modules-tomcat9').tasks.named('jar')
    from configurations.slf4jDeps
  }

  into('bin') {
    from('release/scripts') {
      include '*'
      exclude 'setenv.properties'
    }
  }

  into('conf') {
    from('release/conf') {
      include '*'
    }
  }
}

tasks.register('distAppServer', Zip) {
  archiveBaseName = moduleBaseName
  classifier = "AppServer"

  into('lib') {
    from project(':extensions:geode-modules').tasks.named('jar')
    from project(':extensions:geode-modules-session').tasks.named('jar')
    from project(':extensions:geode-modules-session-internal').tasks.named('jar')
    from configurations.slf4jDeps
  }

  into('bin') {
    from('release/session/bin/') {
      include 'modify_war'

      filter(ReplaceTokens, tokens:['GEODE_VERSION': archiveVersion.get()])
      filter(ReplaceTokens, tokens:['SLF4J_VERSION': DependencyConstraints.get('slf4j-api.version')])
      filter(ReplaceTokens, tokens:['LOG4J_VERSION': DependencyConstraints.get('log4j.version')])
      filter(ReplaceTokens, tokens:['FASTUTIL_VERSION': DependencyConstraints.get('fastutil.version')])
      filter(ReplaceTokens, tokens:['ANTLR_VERSION': DependencyConstraints.get('antlr.version')])
      filter(ReplaceTokens, tokens:['MICROMETER_VERSION': DependencyConstraints.get('micrometer.version')])
      filter(ReplaceTokens, tokens:['TX_VERSION': DependencyConstraints.get('javax.transaction-api.version')])
      filter(ReplaceTokens, tokens:['JGROUPS_VERSION': DependencyConstraints.get('jgroups.version')])
      filter(ReplaceTokens, tokens:['JETTY_VERSION': DependencyConstraints.get('jetty.version')])
      filter(ReplaceTokens, tokens:['SHIRO_VERSION': DependencyConstraints.get('shiro.version')])
      filter(ReplaceTokens, tokens:['COMMONS_IO_VERSION': DependencyConstraints.get('commons-io.version')])
      filter(ReplaceTokens, tokens:['COMMONS_LANG_VERSION': DependencyConstraints.get('commons-lang3.version')])
      filter(ReplaceTokens, tokens:['COMMONS_VALIDATOR_VERSION': DependencyConstraints.get('commons-validator.version')])
    }
  }

  into('conf') {
    from('release/conf')
  }
}

tasks.register('distTcServer', Zip) {
  configure(configureTcServerAssembly)
}

tasks.register('distTcServer30', Zip) {
  configure(configureTcServerAssembly)
  configure(configureTcServer30Assembly)
}

task dist(type: Task, dependsOn: [
  'distTcServer',
  'distTcServer30',
  'distTomcat',
  'distAppServer'
])

build.dependsOn dist

sonarqube {
  skipProject = true
}
