/*
* Copyright (c) 2021, Amazon.com, Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Amazon designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import groovy.text.GStringTemplateEngine

dependencies {
    compile project(path: ':openjdksrc', configuration: 'archives')
}

def tarName = "java-${version.major}-amazon-corretto"
def tarVersion = "${version.major}.${version.minor}.${version.security}+${version.build}"

def javaVersion = "${version.major}.${version.minor}.${version.security}"
def buildId = "${version.build}"
def releaseId = "${version.revision}"
/**
 * Apply version numbers to the RPM spec file template and copy
 * to the build root.
 */
task inflateRpmSpec {
    inputs.file 'java-amazon-corretto.spec.template'
    outputs.file "$buildDir/java-${version.major}-amazon-corretto.spec"
    doLast {
        def renderedTemplate = new GStringTemplateEngine()
                .createTemplate(file('java-amazon-corretto.spec.template'))
                .make(
                [
                    java_spec_version   : version.major,
                    java_major_version   : version.major,
                    java_security_version   : version.security,
                    java_version        : javaVersion,
                    build_id            : buildId,
                    release_id          : releaseId,
                    version_opt         : versionOpt,
                    debug_level         : correttoDebugLevel,
                    boot_jdk_major_version : version.major.toInteger() - 1
                ])
        outputs.files.singleFile.text = renderedTemplate
    }
}

task copySourceTar(type: Tar) {
    dependsOn project.configurations.compile, inflateRpmSpec
    compression Compression.GZIP
    archiveName project.configurations.compile.singleFile.name
    from("$buildDir") {
        include "java-${project.version.major}-amazon-corretto.spec"
        into 'rpm'
    }
    from(tarTree(project.configurations.compile.singleFile)) {
        into '/'
    }
}

task rpmBuild(type: Exec) {
  dependsOn copySourceTar
    workingDir "$buildDir"
    executable = '/usr/bin/rpmbuild'
    args  = ['-vv',
        '-bs',
        '--define',
        "dist .${project.findProperty('corretto.amzn_dist') ?: 'amzn2'}",
        '--define',
        "_topdir ${buildDir}/rpmbuild",
        '--define',
        "_sourcedir ${buildDir}/distributions",
        '--define',
        "_srcrpmdir ${buildDir}/distributions",
        "java-${version.major}-amazon-corretto.spec"]

}