import org.gradle.crypto.checksum.Checksum

/*
 * 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'
    alias(libs.plugins.nebula)
    alias(libs.plugins.checksum)
}

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

configurations {
    cppClient
    cppClientHeaders
}

dependencies {
    cppClient(project(path: ":platforms", configuration: "cppClient"))
    cppClientHeaders(project(path: ":platforms", configuration: "cppClientHeaders"))
}

def tokens = [
        PRODUCT_NAME    : "ignite3-cpp-client",
        LIB_DIR         : "/usr/lib",
        INCLUDE_DIR     : "/usr/include"
]

configurations {
    cppClientZip {
        canBeConsumed = true
        canBeResolved = false
    }
}

artifacts {
    cppClientZip(distZip)
}

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

            into('lib') {
                from configurations.cppClient
            }
            into('include') {
                from configurations.cppClientHeaders
            }
        }
    }
}

distZip {
    onlyIf {
        project.hasProperty('platforms.enable')
    }
}

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


ospackage {
    license "ASL 2.0"
    packageName tokens.PRODUCT_NAME
    packageGroup "Development/Libraries"
    url "https://ignite.apache.org"
    packageDescription "This package will install Apache Ignite 3 client library for C++"
    os LINUX
    release getCommitNum()

    into(tokens.LIB_DIR) {
        from configurations.cppClient
    }
    into(tokens.INCLUDE_DIR) {
        from configurations.cppClientHeaders
    }
}

buildRpm {
    onlyIf {
        project.hasProperty('platforms.enable')
    }
}

buildDeb {
    onlyIf {
        project.hasProperty('platforms.enable')
    }
}

// --- release setup --- //
def createChecksums = tasks.register('createChecksums', Checksum) {
    onlyIf {
        project.hasProperty('platforms.enable')
    }

    inputFiles.from distZip
    checksumAlgorithm = Checksum.Algorithm.SHA512
}

distZip.finalizedBy(createChecksums)
