#!/bin/sh
set -e
. /usr/share/debconf/confmodule

log () {
	logger -t install-firmware "$@"
}

# For those who don't want to load any firmware, even if available on
# installation images (#1029848):
db_get hw-detect/firmware-lookup
firmware_lookup="$RET"
if [ "$firmware_lookup" = "never" ]; then
	log "firmware lookup disabled (=$firmware_lookup), exiting"
	exit 0
fi

# Clean any files that were present because they were embedded in the initrd by
# debian-cd (e.g. for speech synthesis support), and let the modalias-based
# logic down below pick the relevant package(s) instead. Otherwise, they would
# be picked up by the loose files copy step below.
#
# This uses deb filenames as we expect everything to be in sync (no version
# mismatch) on installation images, but we could use package names instead.
embedded_src=/usr/lib/firmware/.embedded-firmware-debs
embedded_debs=$(cat $embedded_src || true)
for fw_dir in /firmware /cdrom/firmware; do
	contents="$fw_dir/Contents-firmware"
	if [ -f "$contents" ]; then
		for deb in $embedded_debs; do
			log "looking to clean loose files belonging to $deb (according to $contents)"
			# Alternatively, "\$2 ~ /${package}_/ { print \$1 }" if
			# using package names instead of deb filenames:
			files=$(awk "\$2 == \"$deb\" { print \$1 }" "$contents")
			# shellcheck disable=SC2086
			rm -f $files
			log " ... removed up to $(echo "$files"|wc -w) files"
		done
		# Don't keep any empty directories:
		find /usr/lib/firmware -type d -depth -exec rmdir -p {} ';' 2>/dev/null || true
	fi
done

# copy any loose firmware files to /target (incl. subdirs)
if [ -d /usr/lib/firmware ]; then
	for f in /usr/lib/firmware/*; do
		if [ -e "$f" ]; then
			mkdir -p /target/usr/lib/firmware/
			cp -a "$f" /target/usr/lib/firmware/
		fi
	done
fi

# queue firmware packages based on modalias info (#989863):
for fw_dir in /firmware /cdrom/firmware; do
	if [ -d "$fw_dir/dep11" ]; then
		# Query only once:
		udevadm info --export-db | sed -n 's/.* MODALIAS=\(.*\)/\1/p' > /tmp/modalias.cache
		for patterns_file in $(ls "$fw_dir/dep11"/*.patterns 2>/dev/null); do
			if log-output -t install-firmware grep -f "$patterns_file" /tmp/modalias.cache; then
				package=$(basename "$patterns_file" .patterns)
				component=$(cat "${patterns_file%%.patterns}.component")
				log "detected the need for $package ($component) via modalias"
				find $fw_dir -name "${package}_*.deb" | while read deb; do
					mkdir -p /var/cache/firmware
					cp "$deb" /var/cache/firmware
					log "... added $deb to the firmware cache"
					echo $component >> /var/cache/firmware/components
					echo "$package $component modalias" >> /var/log/firmware-summary
				done
			fi
		done
		rm -f /tmp/modalias.cache
	fi
done

# Kali: we don't have DEP-11 at the moment. Let's double-check.
has_dep11=0
for fw_dir in /firmware /cdrom/firmware; do
	if ls "$fw_dir/dep11"/*.patterns 2>/dev/null >/dev/null; then
		has_dep11=1
		break
	fi
done
if [ $has_dep11 -eq 1 ]; then
	echo "Kali: DEP-11 patterns found"
else
	echo "Kali: DEP-11 patterns not found"
fi

# Kali: poor-man workaround in case we don't have DEP-11.
# We do our best to detect if there's a need for graphics firmware.
if [ $has_dep11 -eq 0 ] && command -v lspci >/dev/null; then
	echo "Kali: detecting graphics controller via lspci"
	packages=$(lspci -m | while read -r line; do
		eval set -- $line
		controller=$2
		echo "$controller" | grep -q -E '(3D|Display|VGA) .*controller' || continue
		shift 2
		log "... found $controller: $@"
		if echo "$1" | grep -q -i 'advanced micro devices'; then
			echo "firmware-amd-graphics"
		elif echo "$1" | grep -q -i 'intel corporation'; then
			echo "firmware-intel-graphics"
		elif echo "$1" | grep -q -i 'nvidia'; then
			echo "firmware-nvidia-graphics"
		fi
	done | sort -u)
	for fw_dir in /firmware /cdrom/firmware; do
		if [ -d "$fw_dir/dep11" ]; then
			for package in $packages; do
				find $fw_dir -name "${package}_*.deb" | while read deb; do
					mkdir -p /var/cache/firmware
					cp "$deb" /var/cache/firmware
					log "... added $deb to the firmware cache"
					# hardcode component, we know it's non-free-firmware
					component=non-free-firmware
					echo $component >> /var/cache/firmware/components
					echo "$package $component kali-lspci" >> /var/log/firmware-summary
				done
			done
		fi
	done
fi

# Check whether microcode packages are desirable, based on CPU vendor.
# Only detect and queue installation: they aren't needed in the
# installer context, and they cannot be deployed via `dpkg -i` by the
# install-firmware hook due their dependencies; let the finish-install
# hook handle them instead. Note the component hardcoding.
printf "GenuineIntel intel-microcode\nAuthenticAMD amd64-microcode\n" | while read vendor pkg; do
	if grep -qs "^vendor_id.*$vendor$" /proc/cpuinfo; then
		log "queuing $pkg installation ($vendor)"
		echo $pkg >> /tmp/microcode.list
		mkdir -p /var/cache/firmware
		echo non-free-firmware >> /var/cache/firmware/components
		echo "$pkg non-free-firmware cpu" >> /var/log/firmware-summary
	fi
done

# enable components based on firmware packages that were installed:
if [ -d /var/cache/firmware ]; then
	for component in $(sort -u /var/cache/firmware/components); do
		# Pulling firmware packages from main is unlikely, but if that
		# happens, we don't need to tweak apt-setup… and local packages
		# can be added to installation images without archive support:
		if [ "$component" = main ] || [ "$component" = local ]; then
			continue
		fi
		log "pre-enabling $component component for apt-setup"
		db_set "apt-setup/$component" true
	done
fi
