# Build recipe for libaio.
#
# Copyright (c) 2018-2020 Matias Fonzo, <selk@dragora.org>.
# Copyright (c) 2022-2023 Matias Fonzo, <selk@dragora.org>.
#
# Licensed 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.

# Exit immediately on any error
set -e

program=libaio
version=0.3.113
release=2

# Define a category for the output of the package name
pkgcategory=libs

tarname=${program}_${version}.orig.tar.gz

# Remote source(s)
fetch=https://ftp.debian.org/debian/pool/main/liba/libaio/$tarname

description="
Kernel Asynchronous I/O (AIO) support for GNU/Linux.

AIO enables even a single application thread to overlap I/O operations
with other processing, by providing an interface for submitting one or
more I/O requests in one system call (io_submit()) without waiting for
completion, and a separate interface (io_getevents()) to reap
completed I/O operations associated with a given completion group.
"

homepage=https://lse.sourceforge.net/io/aio.html
license=LGPLv2+

# Source documentation
docs="COPYING ChangeLog TODO"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Apply patches from "Alpine Linux" in order to improve it
    # A huge thanks to the authors of the patches
    patch -Np1 -i "${worktree}/patches/libaio/libaio-cppflags.patch"
    patch -Np1 -i "${worktree}/patches/libaio/libaio-errno.patch"
    patch -Np1 -i "${worktree}/patches/libaio/test.patch"
    patch -Np1 -i "${worktree}/patches/libaio/test-poll.patch"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    make V=1 -j${jobs} \
     prefix=/usr \
     libdir=/usr/lib${libSuffix} \
     DESTDIR="$destdir" install

    strip --strip-unneeded "${destdir}/usr/lib${libSuffix}"/*.so || true
    strip --strip-debug "${destdir}/usr/lib${libSuffix}"/*.a || true

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

