/*
 * MIT License
 *
 * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

plugins {
  id 'org.scm-manager.packaging'
  id 'org.scm-manager.license'
}

import org.gradle.util.VersionNumber
import groovy.json.JsonSlurper

configurations {
  server
  webapp
  packageYaml {
    canBeConsumed = true
  }
}

dependencies {
  server project(':scm-server')
  webapp project(path: ':scm-webapp', configuration: 'webapp')
}

task context(type: Copy) {
  VersionNumber version = VersionNumber.parse(project.version)
  destinationDir = project.buildDir
  into('docker') {
    into('etc') {
      from('src/main/fs/etc')
      expand([version: version])
    }
    into('opt') {
      from('src/main/fs/opt')
    }
    into('opt/scm-server/lib') {
      from project.configurations.server
    }
    into('opt/scm-server/var/webapp') {
      from project.configurations.webapp
      rename {
        'scm-webapp.war'
      }
    }
    from('.') {
      include 'Dockerfile'
    }
  }
}

task setupBuilder() {
  doLast {
    def inspect = exec {
      commandLine = ["docker", "buildx", "inspect", "scm-builder"]
      ignoreExitValue = true
    }
    if (inspect.exitValue != 0) {
      exec {
        commandLine = ["docker", "run", "--privileged", "--rm", "tonistiigi/binfmt", "--install", "arm64"]
      }
      exec {
        commandLine = ["docker", "buildx", "create", "--name", "scm-builder", "--driver", "docker-container", "--platform", "linux/arm/v7,linux/arm64/v8,linux/amd64"]
      }
      exec {
        commandLine = ["docker", "buildx", "inspect", "scm-builder"]
      }
    }
  }
}

task build(type: Exec) {
  commandLine = ["docker", "buildx", "bake", "--builder", "scm-builder", isSnapshot ? "dev": "prod"]
  environment "VERSION", dockerTag
  environment "COMMIT_SHA", revision
  environment "IMAGE", dockerRepository

  doLast {
    File file = new File(project.buildDir, 'docker.tag')
    file.text = dockerTag
  }
  dependsOn 'context', 'setupBuilder'
}

task pushImages(type: Exec) {
  commandLine = ["docker", "buildx", "bake", "--builder", "scm-builder",  isSnapshot ? "dev": "prod", "--push"]
  environment "VERSION", dockerTag
  environment "COMMIT_SHA", revision
  environment "IMAGE", dockerRepository

  dependsOn 'build'
}

task publish() {
  dependsOn 'pushImages'
}

task distribution(type: PackageYaml)  {
  type = 'docker'
  dependsOn build
}

artifacts {
  packageYaml(file('build/libs/package.yml')) {
    builtBy distribution
  }
}

license {
  header rootProject.file("LICENSE.txt")
  lineEnding = "\n"

  tasks {
    build {
      files.from("build.gradle", "Dockerfile")
    }
    main {
      files.from("src")
    }
  }
}

task check {
  dependsOn checkLicenses
}
