#!/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:
Execute: apt-get update && apt-cache search ^firmware-.*
Decide which package has to be installed for the network card.
Then call this script with the package name as parameter.  Or do not
use any parameter and get all firmware packages installed in the LTSP
chroot. Prepend arch=<non default arch> to the command if server arch and
LTSP chroot arch differ.

EOF
    exit 0
fi
if [ "$1" ] ; then
    debnames="$*"
else
    # Find files.  This require non-free to be enabled as APT
    # repository.
    # Skip installer debs, to avoid the b43 installers that conflict
    # with each other.  FIXME review if this is needed after squeeze.
    debnames="$(apt-cache search ^firmware-.* | grep -v installer | cut -d" " -f1)"
fi

# do things in the ltsp chroot for the default arch.
[ "$arch" ] || arch=$(dpkg --print-architecture)
TMP= TMPDIR= ltsp-chroot -a $arch apt-get update
TMP= TMPDIR= ltsp-chroot -d -a $arch apt-get -y -q install $debnames

# copy new initrd from chroot to the server tftpboot dir
ltsp-update-kernels

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
