# Build recipe for linux-libre (generic).
#
# Copyright (c) 2017-2020 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
version=5.4.61
release=1

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

pkgname=kernel-generic

tarname=${program}-libre-${version}-gnu.tar.lz

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

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

description="
The Linux kernel image and modules.

This is a generic configuration for the Linux kernel
which intends to run well in all environments.
"

license="GPLv2 only"

build()
{
    # Preserve 'srcdir' in order to produce "buildtree-generic", later
    keep_srcdir=keep_srcdir

    unpack "${tardir}/$tarname"

    cd "$srcdir"

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

    # Support LZIP

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

    # Support Reiserfs4

    patch -p1 < "${worktree}/patches/kernel/reiser4-for-5.4.21.patch"

    make mrproper

    case $arch in
    amd64 | x32 )
        echo "Copying config-amd64_generic ..."
        cp -p "${worktree}/archive/kernel/config-amd64_generic" .config
        kernel_arch=x86_64
        ;;
    i586)
        echo "Copying config-i586_generic ..."
        cp -p "${worktree}/archive/kernel/config-i586_generic" .config
        kernel_arch=i386
        ;;
    armhf)
        echo "Copying config-c201-v12 ..."
        cp -p "${worktree}/archive/kernel/config-c201-v12" .config
        kernel_arch=arm
        ;;
    *)
        echo "${0}: No custom configuration file detected for your architecture." 1>&2
        exit 1;
        ;;
    esac

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

    make -j${jobs} ARCH=$kernel_arch bzImage
    make -j${jobs} ARCH=$kernel_arch modules
    make -j${jobs} ARCH=$kernel_arch \
     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-generic-${strver}"
    cp -p System.map "${destdir}/boot/System.map-generic-${strver}"

    if test -e "arch/${kernel_arch}/boot/bzImage"
    then
        cp -p "arch/${kernel_arch}/boot/bzImage" \
              "${destdir}/boot/vmlinuz-generic-${strver}"
    elif test -e "arch/${kernel_arch}/boot/zImage"
    then
        cp -p "arch/${kernel_arch}/boot/zImage" \
              "${destdir}/boot/vmlinuz-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-generic-${strver}" \\
          "\${rootdir}/boot/"

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

cp -pf -- "\$rootdir${packagedir}/${full_pkgname}/boot/vmlinuz-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 . /sbin/depmod -a $strver

EOF
}

