# Build instructions for GNU Linux-libre (i586+, generic).
#
# 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=linux-libre
version=4.14.281
release=1

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

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

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

homepage=https://www.gnu.org/software/linux-libre

description="
The GNU Linux-libre kernel (image and modules for ${arch}).

GNU Linux-libre is a project to maintain and publish 100% Free distributions of
Linux, suitable for use in Free System Distributions, removing software that is
included without source code, with obfuscated or obscured source code, under
non-Free Software licenses, that do not permit you to change the software so
that it does what you wish, and that induces or requires you to install
additional pieces of non-Free Software.

This is a custom configuration for Dragora, which is for uni-processors, is
intended to be or run on Intel 586 family (MMX) machines or higher.
"

license="GPLv2 only"

# Refresh 'srcdir'
srcdir=linux-${version}

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

    cd "$srcdir"

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

    # Support LZIP (Thanks to the friend of the house "Antonio Diaz Diaz")

    patch -p1 < "${worktree}/patches/kernel/linux-4.14.40_lzip-3.diff"

    # Support Reiserfs4 (Thanks to "Edward Shishkin")
    #
    # TODO: Needs a re-adaptation
    #patch -p1 < "${worktree}/patches/kernel/reiser4-for-4.14.1.patch"

    make mrproper

    echo "Copying config-i586_generic ..."
    cp -p "${worktree}/archive/kernel/config-i586_generic" .config

    # An environment variable for config purposes
    if test ! -z "$PRESERVE"
    then
        echo "PRESERVE has been set."
        exit 99;
    fi

    make -j${jobs} ARCH=i386 bzImage
    make -j${jobs} ARCH=i386 modules
    make -j${jobs} ARCH=i386 \
     INSTALL_MOD_PATH="$destdir" modules_install

    # To extract the kernel string version
    strver="$(ls -d "${destdir}"/lib/modules/*)"
    strver="${strver##*/}"

    # Install kernel images
    mkdir -p "${destdir}/boot"

    cp -p .config "${destdir}/boot/config-${arch}_generic-${strver}"
    cp -p System.map "${destdir}/boot/System.map-${arch}_generic-${strver}"

    if test -e "arch/i386/boot/bzImage"
    then
        cp -p "arch/i386/boot/bzImage" \
              "${destdir}/boot/vmlinuz-${arch}_generic-${strver}"
    elif test -e "arch/i386/boot/zImage"
    then
        cp -p "arch/i386/boot/zImage" \
              "${destdir}/boot/vmlinuz-${arch}_generic-${strver}"
    else
        echo "${0}: No kernel image found: bzImage or zImage" 1>&2
        exit 1;
    fi

    # Exclude files from the Graft installation,
    # this will be copied via post-intall, since
    # /boot could be a separated partition
    touch "${destdir}/boot/.nograft"

    # Insert post-install script manually

    # This must be recreated on post-installation
    rm -f "${destdir}"/lib/modules/${strver}/build \
          "${destdir}"/lib/modules/${strver}/source

    mkdir -p "${destdir}/var/lib/qi"
    cat << EOF > "${destdir}/var/lib/qi/${full_pkgname}.sh"

# Kernel string version
strver=$strver

# Copy kernel image from 'packagedir' to 'rootdir/boot'

mkdir -p -- "\${rootdir}/boot"

cp -pf -- "\$rootdir${packagedir}/${full_pkgname}/boot/System.map-${arch}_generic-${strver}" \\
          "\${rootdir}/boot/"

cp -pf -- "\$rootdir${packagedir}/${full_pkgname}/boot/config-${arch}_generic-${strver}" \\
          "\${rootdir}/boot/"

cp -pf -- "\$rootdir${packagedir}/${full_pkgname}/boot/vmlinuz-${arch}_generic-${strver}" \\
          "\${rootdir}/boot/"

# Make generic symlinks for this kernel version

srcdir="${srcdir##*/}"

(
    cd lib/modules/${strver} || exit 1
    rm -f build source
    ln -sf /usr/src/${srcdir} build
    ln -sf /usr/src/${srcdir} source
)

# Generate or update the module dependency list
chroot "\$rootdir" /sbin/depmod -a $strver

EOF

    # Preserve 'srcdir' in order to produce "kernel/buildtree-generic"
    keep_srcdir=keep_srcdir

    # Save 'version' and 'release' number to compose
    # variable values for "kernel/buildtree-generic"
    kernel_generic_version=$version
    kernel_generic_release=$release

    export keep_srcdir kernel_generic_version kernel_generic_release
}

