plugins {
  id 'java'
  id 'com.google.cloud.tools.jib' version '3.4.5'
  id 'de.undercouch.download' version '4.0.0'
  id 'com.gorylenko.gradle-git-properties' version '2.2.0'
}

ext {
  // where to download the Stackdriver Debugger agent https://cloud.google.com/debugger/docs/setup/java
  stackdriverDebuggerAgentUrl = 'https://storage.googleapis.com/cloud-debugger/compute-java/debian-wheezy/cdbg_java_agent_gce.tar.gz'

  // where to place the Cloud Debugger agent in the container
  stackdriverDebuggerLocation = '/opt/cdbg'

  // location for jib extras, including the Java agent
  jibExtraDirectory = "${buildDir}/jib-agents"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
  mavenCentral()
}

dependencies {
  implementation 'com.sparkjava:spark-core:2.9.1'
  implementation 'org.slf4j:slf4j-simple:1.7.28'
}

// Download and extract the Cloud Debugger Java Agent
task downloadAgent(type: Download) {
  src stackdriverDebuggerAgentUrl
  dest "${buildDir}/cdbg_java_agent_gce.tar.gz"
}
task extractAgent(dependsOn: downloadAgent, type: Copy) {
  from tarTree(downloadAgent.dest)
  into "${jibExtraDirectory}/${stackdriverDebuggerLocation}"
}

jib {
  to {
    image = 'gcr.io/REPLACE-WITH-YOUR-GCP-PROJECT/image-built-with-jib'
  }
  extraDirectories.paths = [file(jibExtraDirectory)]
  container {
    ports = ['4567']
    jvmFlags = [
        '-agentpath:' + stackdriverDebuggerLocation + '/cdbg_java_agent.so=--logtostderr=1',
        '-Dcom.google.cdbg.module=' + project.name,
        '-Dcom.google.cdbg.version=' + version]
  }
}

tasks.jib.dependsOn extractAgent
tasks.jibDockerBuild.dependsOn extractAgent
tasks.jibBuildTar.dependsOn extractAgent
defaultTasks 'jib'
