# Build recipe for libucontext.
#
# Copyright (c) 2022 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=libucontext
version=1.2
release=1

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

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch=https://distfiles.dereferenced.org/libucontext/$tarname

description="
Ucontext function implementations.

libucontext is a library which provides the ucontext.h C API.  Unlike other
implementations, it faithfully follows the kernel process ABI when doing
context swaps.  Notably, when combined with gcompat, it provides a fully
compatible implementation of the ucontext functions that are ABI compatible
with Glibc.
"

homepage=https://github.com/kaniini/libucontext
license=ISC

# Source documentation
docs="LICENSE NEWS README.md VERSION"
docsdir="${docdir}/${program}-${version}"

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

    cd "$srcdir"

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

    # Set build architecture
    case $arch in
    arm*)
        libucontext_arch=arm
        ;;
    ppc64le*)
        libucontext_arch=ppc64
        ;;
    *)
        libucontext_arch="$(uname -m)"
        ;;
    esac

    make -j${jobs} \
     ARCH=$libucontext_arch \
     CFLAGS="$QICFLAGS -ggdb3 -O2 -Wall" \
     LDFLAGS="$(echo "$QILDFLAGS" | sed 's/-s//')" \
     LIBDIR=/usr/lib${libSuffix} \
     PKGCONFIGDIR=/usr/lib${libSuffix}/pkgconfig \
     all

    make -j${jobs} \
     ARCH=$libucontext_arch \
     CFLAGS="$QICFLAGS -ggdb3 -O2 -Wall" \
     LDFLAGS="$(echo "$QILDFLAGS" | sed 's/-s//')" \
     LIBDIR=/usr/lib${libSuffix} \
     PKGCONFIGDIR=/usr/lib${libSuffix}/pkgconfig \
     DESTDIR="$destdir" \
     install

    unset -v libucontext_arch

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

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

