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

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

# enable non-free repository if any firmware / injected drivers are
# detected.
if [ -d /var/cache/firmware ]; then
	for deb in /var/cache/firmware/*.deb; do
		if [ -f "$deb" ]; then
			need_nonfree=1
		fi
	done
fi

if [ "$need_nonfree" ]; then
	db_set apt-setup/non-free true
fi
