#!/bin/sh

# Whenever the kernel package is updated, also install the latest version 
# of the QCACLD WiFi driver for the QCA9377 SDIO chip in MNT Pocket Reform 
# with i.MX8MPlus Module.
#
# Otherwise people can end up with the qcacld2 driver not matching the
# kernel version, thus rendering Wi-Fi nonfunctional.
#
# We're shipping the driver as a binary because building it from source
# on the platform takes a long time and is quite inconvenient.
#
# Running apt inside a kernel hook can lead to a race condition when using
# dpkg to configure the kernel package:
#
# dpkg: error: dpkg frontend lock was locked by /usr/bin/apt process with pid 8727
# Note: removing the lock file is always wrong, can damage the locked area
# and the entire system. See <https://wiki.debian.org/Teams/Dpkg/FAQ#db-lock>.

update_package() {
  apt install -y reform-qcacld2
}

# This script lives in /etc and is as such a config file and will not get
# removed (unless with --purge) when the user uninstalls the reform-qcacld2
# package. To avoid re-installing reform-qcacld2 even though the user
# has removed it, check here if the kernel module provided by the package
# reform-qcacld2-*-mnt-reform-arm64 is actually installed. If not, we do
# not need to and should not proceed.

if ! test -d /opt/reform-qcacld2/; then
  echo "I: /opt/reform-qcacld2/ does not exist: not running reform-qcacld2 hook" >&2
  exit 0
fi

if test -n "$(find /opt/reform-qcacld2/ -maxdepth 0 -type d -empty 2>/dev/null)"; then
  echo "I: /opt/reform-qcacld2/ is empty: not running reform-qcacld2 hook" >&2
  exit 0
fi

if test -z "$(find /opt/reform-qcacld2/ -maxdepth 1 -name 'qcacld2-*.ko' -print -quit)"; then
  echo "I: No match for glob /opt/reform-qcacld2/qcacld2-*.ko: not running reform-qcacld2 hook" >&2
  exit 0
fi

# If this script is not run on a real system with a real init process
# but in a container-like chroot environment, then creating these
# background processes without waiting for their completion will leave
# zombie processes behind which hog resources like /dev/null and thus
# prevent the container software from cleanly un-mounting bind-mounts
# as they may have been created for /dev/null. We cannot reliably
# detect whether the container manager will automatically wait for and
# reap zombie processes, so as a hacky workaround, we just make this
# script a no-op if it is started inside a chroot environment
if ischroot; then
	echo "I: chroot environment detected: not attempting to install reform-qcacld2 in the background" >&2
	exit 0
fi

# The package reform-qcacld2-*-mnt-reform-arm64 (which reform-qcacld2) depends
# on depends on linux-image-*-mnt-reform-arm64. This means, that running this
# postinst hook on a system that has a kernel from debian stable installed will
# result in a kernel from the MNT repos getting installed and probably booted
# because the MNT kernel from unstable will probably be of a higher version
# than the version from stable or stable-backports.
if test -n "$(apt-get indextargets 'Component: main' 'Created-By: Packages' 'Repo-URI: https://reform.debian.net/debian/' 'Architecture: arm64' --format '$(FILENAME)')"; then
  echo "W: installing reform-qcacld2 on a system with packages from reform.d.n is not recommended as it will install a kernel from the MNT repositories. Remove reform-qcacld2 and install ezurio-qcacld-2.0-dkms instead." >&2
fi

echo "Starting background process to update reform-qcacld2 driver package (for Wi-Fi) to match kernel version."

# Background because we need to wait for the main running apt install process to finish

update_package &

