# Build recipe for GNU Linux-libre headers.
#
# Copyright (c) 2015-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=linux-libre

# Set 'version' and 'release' for supported architectures
case "$(uname -m)" in
i?86)
    version=4.14.281
    release=1
    _gnu_suffix="gnu1"
    ;;
*)
    version=5.16.20
    release=2
    _gnu_suffix="gnu"
    ;;
esac

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

# Use a custom package name
pkgname=linux-libre-headers

tarname=${program}-${version}-${_gnu_suffix}.tar.lz

# Remote source(s)
fetch=https://linux-libre.fsfla.org/pub/linux-libre/releases/${version}-${_gnu_suffix}/$tarname

description="
The GNU Linux-libre headers (${arch}).

This package contains the Linux headers provided by the GNU Linux-libre
project to expose the kernel's API.
"

homepage=https://www.gnu.org/software/linux-libre
license="GPLv2 only"

# Limit parallel jobs for the compiler
jobs=1

# To match 'srcdir'
srcdir=linux-${version}

build()
{
    unpack "${tardir}/$tarname"
    unset -v _gnu_suffix

    cd "$srcdir"

    patch -p1 < "${worktree}/patches/linux-headers/revert-broken-uapi.patch"
    patch -p1 < "${worktree}/patches/linux-headers/0003-remove-inclusion-of-sysinfo.h-in-kernel.h.patch"

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

    make -j${jobs} mrproper
    make -j${jobs} headers || make -j${jobs} headers_check

    # Holy molly... the 'headers_install' target cannot be used
    # because it requires rsync(1), which is not available early.

    mkdir -p "${destdir}/usr/include"
    cp -Rp usr/include/* "${destdir}/usr/include/"

    # Remove cruft
    find "${destdir}/usr/include" -name '.*' -exec rm -rf {} +
    rm -f "${destdir}/usr/include/Makefile"
}

