#!/bin/sh
#
# Add non-free firmware to the chroot used by LTSP to enable certain
# network cards (Intel, Broadcom, ...). Takes package name as param,
# or add all firmware packages if no package name is given.

set -e

# usage
if [ "$1" = "-h" ]
then
    cat <<EOF

Usage information:

(1) Run apt-get update and apt-cache search ^firmware-.* in the LTSP chroot.
    Example for the default diskless workstation chroot:
    debian-edu-ltsp-chroot -b /srv/ltsp/dlw apt update
    debian-edu-ltsp-chroot -b /srv/ltsp/dlw apt-cache search ^firmware-.*
(2) Decide which package has to be installed for the network card.
(3) Call this script with the package name as parameter.
    If no parameter is used, all firmware packages will be installed in the default
    LTSP chroot (/srv/ltsp/dlw) - not really recommended because the initrd gets bloated!

Please note:
Prepend BASE=<non default LTSP chroot> to the command in case such a chroot
should be modified.
Example:
BASE=/srv/ltsp/x2go-display-amd64 /usr/share/debian-edu-config/tools/ltsp-addfirmware firmware-linux

EOF
    exit 0
fi

BASE=${BASE:-/srv/ltsp/dlw}

if [ "$1" ] ; then
    debnames="$*"
else
    # Find files. This requires non-free to be enabled as APT repository.
    # Skip installer debs to avoid the b43 installers that conflict
    # with each other.
    # For bookworm, also exclude firmware-microbit-micropython{-dl} and
    # packages w/ EULA (firmware-{ivtv,ipw2x00}).
    # FIXME Review if this is still needed after bookworm.
    debnames="$(debian-edu-ltsp-chroot -b $BASE sh -c 'apt-cache search ^firmware-.* | grep -v installer | grep -v micropython | grep -v ivtv | grep -v ipw2x00 | cut -d" " -f1')"
fi

# do things in the default LTSP chroot.
debian-edu-ltsp-chroot -b $BASE apt-get update
debian-edu-ltsp-chroot -m -b $BASE apt-get -y -q install $debnames

# copy new initrd from chroot to the server tftpboot dir
ALL_IMAGES=1 ltsp kernel

cat <<EOF
Done - if no error about an unlocateable package was reported.
Booting a machine as thin client or diskless workstation should now be
possible.  If this is not the case, the installation of other or
additional firmware is likely to be required.

EOF
