/*
 * 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.
 */

project(':') {
  apply plugin: 'base'
  apply plugin: 'signing'
}

def isRelease = ext.release.toBoolean()

def releaseVersion = project.version
if (!isRelease) {
  releaseVersion += "-SNAPSHOT"
}

// Modify the gradle.properties to indicate whether this is a release.  This results in the
// source releases generating artifacts without -SNAPSHOT appended to the version when they are
// built.
task prepare_release_config(type: Copy) {
  from "$rootDir/gradle.properties"

  // There is a bug in Gradle on Windows that prevents copy tasks from copying into the same folder.
  // Therefore, we copy to a temporary dir, and use the copy method (which doesn't have this bug)
  // to copy back into rootDir so the Windows build works, even though this is ugly.
  // https://discuss.gradle.org/t/error-with-a-copy-task-on-windows/1803
  into temporaryDir
  rename { filename ->
    filename + ".release" }
  filter { line ->
    if (isRelease && line.startsWith("release=")) {
      "release=true"
    }
    else {
      line
    }
  }
  doLast {
    copy {
      from temporaryDir
      into "$rootDir"
      include 'gradle.properties.release'
    }
  }
}

task sourceRelease(type: Tar, dependsOn: prepare_release_config) {
  description = "Build a source release, specifically excluding the build directories and gradle wrapper files"
  compression = Compression.GZIP

  baseName "apache-datafu-sources-${releaseVersion}"

  from(project.rootDir) {
    // don't include gradle.properties because we use a modified version
    exclude '.java-version'
    exclude "gradle.properties"
    exclude 'build'
    exclude '**/build'
    exclude '.gradle'
    exclude '**/.gradle'
    exclude 'gradlew'
    exclude 'gradlew.bat'
    exclude 'gradle/wrapper/gradle-wrapper.jar'
    exclude 'gradle/wrapper/gradle-wrapper.properties'
    exclude 'site'
  }

  // rename gradle.properties.release to gradle.properties
  rename { filename ->
    if (filename.equals("gradle.properties.release")) {
      "gradle.properties"
    }
    else {
      filename
    }
  }

  into(baseName)

  // Set destination directory.
  destinationDir = file("${project.buildDir}/distribution/source")

  archiveName = "${baseName}.tgz"
  doLast {
    ant.checksum file:"$destinationDir/$archiveName", algorithm: "SHA-512", fileext: ".sha512"
  }
}

// Signing requires a user ~/.gradle/gradle.properties file with signing information.
// See: http://www.gradle.org/docs/current/userguide/signing_plugin.html
signing {
  // Sign the source archive if the proper configuration is in place to do so.  Otherwise
  // skip the signing process (it isn't required).  This archive can be manually signed if
  // needed.
  required false
  sign sourceRelease
}

task release(dependsOn: signSourceRelease)

// Publishing to Apache's Maven repository (Nexus). To install the archives in the
// local repository, run the 'install' task.
subprojects {
  apply plugin: 'nexus'

  nexus {
    attachSources = false
    attachTests = false
    attachJavadoc = false
    sign = true
    repositoryUrl = 'https://repository.apache.org/service/local/staging/deploy/maven2'
    snapshotRepositoryUrl = 'https://repository.apache.org/content/repositories/snapshots'
  }

  modifyPom {
    project {
      name 'Apache DataFu'
      description 'Librares that make easier to solve data problems using Hadoop and higher level languages based on it.'
      url 'https://datafu.apache.org/'

      scm {
        url 'https://git-wip-us.apache.org/repos/asf?p=datafu.git;a=tree'
        connection 'scm:http://git-wip-us.apache.org/repos/asf/datafu.git'
        developerConnection 'scm:https://git-wip-us.apache.org/repos/asf/datafu.git'
      }

      licenses {
        license {
          name 'The Apache Software License, Version 2.0'
          url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
        }
      }
    }
  }
}
