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

plugins {
    id 'nebula.ospackage' version 'latest.release'
}

dependencies {
    compile project(path: ':installers:linux:universal:tar', configuration: 'archives')
}

ext {
    switch (project.correttoArch) {
        case 'aarch64':
            arch_redline = 'AARCH64'
            break;
        case 'x86':
            arch_redline = "i386"
            break
        case 'x64':
            arch_redline = 'x86_64'
            break
    }
}

def jvmDir = '/usr/lib/jvm'
def jdkInstallationDirName = "java-${project.version.major}-amazon-corretto"
def jdkHome = "${jvmDir}/${jdkInstallationDirName}"
def jdkBinaryDir = "${buildRoot}/${project.correttoJdkArchiveName}"
def jdkPackageName = "java-${project.version.major}-amazon-corretto-devel"

ospackage {
    version project.version.upstream
    release project.version.revision

    url "${packageInfo.url}"
    vendor "${packageInfo.vendor}"
    packager "${packageInfo.packager}"
    license "${packageInfo.license}"
    buildHost "${packageInfo.buildHost}"
    user 'root'
    permissionGroup 'root'
    epoch 1
    arch arch_redline
    os LINUX
    type BINARY
}

/**
 * Uncompress and copy the universal Corretto artifact
 * tar for RPM packaging.
 */
task extractUniversalTar(type: Copy) {
    dependsOn project.configurations.compile
    from tarTree(project.configurations.compile.singleFile)
    into buildRoot
}

/**
 * Populate version numbers, java home and alternatives
 * priority to postin_javac.sh.template and preun_javac.sh.template.
 * Create script copies under build root scripts folder.
 */
task inflateRpmScriptTemplate(type: Copy) {
    dependsOn extractUniversalTar
    // Use the same priority as IcedTea JDK RPM distribution, based on java version
    def priority = String.format("1%2s%2s%3s", project.version.major, project.version.minor, project.version.security).replace(' ', '0')
    from('scripts') {
        include '**/*.template'
        rename { file -> file.replace('.template', '') }
        filter(org.apache.tools.ant.filters.ReplaceTokens,
                tokens: project.version + [alternatives_priority: priority])
    }
    into "${buildRoot}/scripts"
}

/**
 * Generate RPM for JDK, with package published under
 * distributions folder.
 */
task generateJdkRpm(type: Rpm) {
    description 'Create the RPM package for Corretto JDK'
    dependsOn inflateRpmScriptTemplate
    packageName jdkPackageName
    packageDescription packageInfo.description
    summary "Amazon Corretto ${project.version.major} development environment"
    packageGroup 'Development/Tools'
    // Remove after https://github.com/nebula-plugins/gradle-ospackage-plugin/issues/401 is merged and released
    sourcePackage "${jdkPackageName}-${project.version.major}.${project.version.minor}.${project.version.security}.${project.version.build}-${project.version.revision}.src.rpm"

    prefix(jdkHome)
    postInstall file("$buildRoot/scripts/postin_java.sh")
    postInstall file("$buildRoot/scripts/postin_javac.sh")
    preUninstall file("$buildRoot/scripts/preun_java.sh")
    preUninstall file("$buildRoot/scripts/preun_javac.sh")

    requires('zlib')

    provides(jdkPackageName, "${epoch}:${version}-${release}", EQUAL)
    provides("java-${project.version.major}-devel", "${epoch}:${version}", EQUAL)
    provides("java-${project.version.major}-openjdk-devel", "${epoch}:${version}", EQUAL)
    provides("java-${project.version.major}-openjdk-devel", "${epoch}:${version}-${release}", EQUAL)
    provides("java-sdk-${project.version.major}", "${epoch}:${version}", EQUAL)
    provides("java-sdk-${project.version.major}-openjdk", "${epoch}:${version}-${release}", EQUAL)
    provides("java-${project.version.major}", "${epoch}:${version}", EQUAL)
    provides("java-${project.version.major}-openjdk", "${epoch}:${version}", EQUAL)
    provides("jre", "${epoch}:${version}", EQUAL)
    provides("jre-${project.version.major}", "${epoch}:${version}", EQUAL)
    provides("jre-${project.version.major}-openjdk", "${epoch}:${version}", EQUAL)

    from(jdkBinaryDir) {
        include(project.configurationFiles)
        fileType CONFIG | NOREPLACE
        into jdkHome
    }

    from(jdkBinaryDir) {
        into jdkHome
        exclude 'legal'
        exclude(project.configurationFiles)
    }

    // Copy legal directory specifically to set permission correctly.
    // See https://github.com/corretto/corretto-11/issues/129
    from("${jdkBinaryDir}/legal") {
        into "${jdkHome}/legal"
        fileMode 0444
    }
}

artifacts {
    archives generateJdkRpm
}