#!/usr/bin/make -f
#
# debian/rules for Ubuntu linux-firmware-nonfree
#

firm_dir = $(CURDIR)/debian/linux-firmware-nonfree/lib/firmware

kernel_version := $(shell apt-cache depends linux-image-generic | sed -ne 's/^  Depends: linux-image-\([0-9]\)/\1/p')

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep
	dh_clean -i

	install -d $(firm_dir)

	#
	# Assume all files that start with a lower case character are firmware files.
	#
	for i in firmware/*/[[:lower:]]* firmware/[[:lower:]]*; do \
	  case $${i##*/} in \
	    *) test -f "$$i" && cp -v $$i "$(firm_dir)/${i##*/}";; \
	  esac; \
	done

	#
	# Of course b43 has to be special.
	#
	tar -C $(firm_dir) -xzf firmware/B43.tgz

binary-indep: install-indep debian/linux-firmware-nonfree.modaliases
	dh_testdir
	dh_testroot

	dh_installchangelogs -i
	dh_installdocs -i
	dh_modaliases -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary: binary-indep
build: build-arch

clean:
	dh_testdir
	dh_testroot
	dh_prep
	dh_clean

# run this target to update the Modaliases: package header, so that
# ubuntu-common-drivers/PackageKit can pick up the package automatically if a
# covered piece of hardware is found
debian/linux-firmware-nonfree.modaliases: moddir=/lib/modules/$(kernel_version)
debian/linux-firmware-nonfree.modaliases: install-indep
	test -d $(moddir)
	debian/gen-modaliases $(moddir) > $@

.PHONY: clean binary build binary-indep install-indep binary-arch build-arch build-indep
