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

if (!project.hasProperty('version') || project.version == 'unspecified') {
  try {
    exec {
      commandLine 'git', 'fetch', '-t', 'https://github.com/apache/incubator-gobblin.git', 'master'
    }
    def versionOut = new ByteArrayOutputStream()
    exec {
      commandLine 'git', 'describe', '--tags', '--always'
      standardOutput versionOut
    }
    def tagStr = versionOut.toString().trim()
    println 'Using latest tag for version: ' + tagStr
    if (tagStr.startsWith("gobblin_")) {
      project.version = tagStr.substring(8)
    }
    else {
      project.version = tagStr
    }
  }
  catch (Exception e) {
    logger.warn("Unable to determine version. Is this a git copy? Using 'unknown'.");
    project.version = 'unknown'
  }
}

if (!project.hasProperty('pegasusVersion')) {
  project.ext.pegasusVersion = '6.0.12'
}

if (project.hasProperty('jdkVersion')) {
  if (project.jdkVersion.equals(JavaVersion.VERSION_1_8.toString())) {
    ext.javaVersion = JavaVersion.VERSION_1_8
  } else {
    throw new RuntimeException("Unsupported JDK version: " + project.jdkVersion)
  }
}

// Hadoop validation
if (! hadoopVersion.startsWith('2.')) {
  throw new GradleScriptException("Only Hadoop 2.x is supported: " + hadoopVersion);
}

def gradleVersions = project.gradle.gradleVersion.tokenize(".")
ext.gradleVersionMajor = Integer.parseInt(gradleVersions[0])
ext.gradleVersionMinor = Integer.parseInt(gradleVersions[1])
println "Detected Gradle version major=" + gradleVersionMajor + " minor=" + gradleVersionMinor

ext.dropwizardMetricsVersion = '4.1.2'
ext.findBugsVersion = '3.0.0'
ext.googleVersion = '1.22.0'
ext.slf4jVersion = '1.7.21'
ext.log4jVersion = '1.2.17'
