#!/bin/bash -e

# debirf module: network-driver-r8168

# Copyright (C) 2018, 2020 Gunter Miegel coinboot.io
#
# This file is part of Coinboot.
#
# Coinboot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Install the dkms source for the r8168 network driver
# The Gigabyte GA-H110-D3A comes with a Realtek RTL8111G ethernet controller
# which has no driver module in the linux-image-generic package.
# To get the driver module there are two ways:
# A: Use the linux-image-extra-package which comes along with the r8169 module
# and a lot of other modules and is quite big (34M).
# B: Use the DKMS package with the sources for the r8168 module.
# For now we go with B because the footprint and overhead seems to be smaller.

KERNEL_RELEASE=$DEBIRF_KERNEL
R8168_PACKAGE=r8168-dkms
R8168_PACKAGE_VERSION=8.049.02-1ubuntu1_all
R8168_PACKAGE_DEB="$R8168_PACKAGE"_"$R8168_PACKAGE_VERSION".deb
PACKAGES="linux-base linux-headers-$DEBIRF_KERNEL libelf-dev wget dkms linux-modules-5.11.0-46-generic"

# Update package list.
debirf_exec sudo apt-get update

# Proper chrooting is needed for building the module.
# Error with debirf fakeroot:
# '/usr/sbin/dkms: line 1904: /dev/fd/62: No such file or directory'
sudo mount -o bind /dev "$DEBIRF_ROOT"/dev
sudo mount -o bind /dev/pts "$DEBIRF_ROOT"/dev/pts
sudo mount -t proc /proc "$DEBIRF_ROOT/"proc

chroot "$DEBIRF_ROOT" sudo apt-get install --yes linux-base
chroot "$DEBIRF_ROOT" sudo apt-get install --yes $PACKAGES

debirf_exec sudo wget https://mirrors.edge.kernel.org/ubuntu/pool/universe/r/r8168/$R8168_PACKAGE_DEB -P /tmp

chroot "$DEBIRF_ROOT" dpkg -i /tmp/$R8168_PACKAGE_DEB
debirf_exec sudo rm -v /tmp/$R8168_PACKAGE_DEB

sudo umount "$DEBIRF_ROOT"/dev/pts
sudo umount "$DEBIRF_ROOT"/dev
sudo umount "$DEBIRF_ROOT"/proc

# Copy modules to /var/lib/modules - after we purges DKMS can not longer do this.
# Afterwards we have to rebuilt the dependency map with depmod.
# TODO: Put compiled module at a better place than the root of the module directory.
sudo find $DEBIRF_ROOT/var/lib/dkms -name r8168.ko -exec cp -v {} "$DEBIRF_ROOT"/lib/modules/"$KERNEL_RELEASE" \;

# Since Eoan linux-check-removal is triggered when removing the package
# with the currently running linux kernel and omits that operation.
# We just replace linux-chec-remove with a simple 'exit 0'.
echo 'exit 0' > "$DEBIRF_ROOT"/usr/bin/linux-check-removal

# As linux-modules-<kernel> gets purged as dependency we have to make a backup
# of the modules directory that we restore after apt-get purge.
sudo mv "$DEBIRF_ROOT"/lib/modules "$DEBIRF_ROOT"/tmp/

# Purge installed packages of this modules to free up the space.
debirf_exec apt-get purge $PACKAGES $R8168_PACKAGE --yes

sudo mv "$DEBIRF_ROOT"/tmp/modules "$DEBIRF_ROOT"/lib/
