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

plugins {
    id 'distribution'
    id 'signing'
    id 'java'
    alias(libs.plugins.nebula)
    alias(libs.plugins.checksum)
}

apply from: "$rootDir/buildscripts/utils.gradle"

import org.gradle.crypto.checksum.Checksum

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}


configurations {
    javaClient
}

def igniteJavaClient = project(':ignite-client')

dependencies {
    javaClient(igniteJavaClient)
}

def createChecksums = tasks.register('createChecksums', Checksum) {
    inputFiles.from distZip
    checksumAlgorithm = Checksum.Algorithm.SHA512
}

def tokens = [
        PRODUCT_NAME        : 'ignite3-java-client',
        LIB_JAR             : "${igniteJavaClient.name}-${igniteJavaClient.version}.jar".toString(),
]


// ZIP packaging
distributions {
    main {
        distributionBaseName = tokens.PRODUCT_NAME
        contents {
            into('') {
                from "$rootDir/LICENSE"
                from "$rootDir/NOTICE"
            }
            into('lib') {
                from configurations.javaClient
            }
        }
    }
}

// --- ZIP packaging --- //
configurations {
    javaClientZip {
        canBeConsumed = true
        canBeResolved = false
    }
}

artifacts {
    javaClientZip(distZip)
}

// Explicitly create task so that the resulting artifact is not added to the configuration
def signJavaClientZip = tasks.register('signJavaClientZip', Sign) {
    sign configurations.javaClientZip
}

// --- DEB/RPM packaging --- //
def linuxTokens = tokens + [
        LIB_DIR             : '/usr/lib/ignite3-java-client'
]

ospackage {
    license "ASL 2.0"
    packageName linuxTokens.PRODUCT_NAME
    packageGroup "Development/Libraries"
    url "https://ignite.apache.org"
    packageDescription "This package will install Apache Ignite Java Client"
    os LINUX
    user 'root'
    release getCommitNum()

    into(linuxTokens.LIB_DIR) {
        from configurations.javaClient
    }
}

buildDeb {
    signingKeyId = project.findProperty("signing.keyId")
    signingKeyPassphrase = project.findProperty("signing.password")
    signingKeyRingFile = project.hasProperty("signing.secretKeyRingFile") ? file(project.property("signing.secretKeyRingFile")) : null
}


// --- release setup --- //
configurations {
    javaClientRelease {
        canBeConsumed = true
        canBeResolved = false
    }
}

if (project.hasProperty('prepareRelease')) {
    artifacts {
        javaClientRelease(file("$buildDir/distributions")) {
            builtBy signJavaClientZip, buildDeb, buildRpm
        }
        javaClientRelease(file("$buildDir/checksums")) {
            builtBy createChecksums
        }
    }
}
