#!/bin/bash -eu
#
# Install firmware and license files into a package directory
#

if [ $# -ne 1 ] ; then
	echo "Usage: install-firmware <package>" >&2
	exit 2
fi

package=${1}

echo "Install firmware files for ${package} ..."

staging_dir=debian/build/${package}

package_dir=debian/${package}
firmware_dir=${package_dir}/usr/lib/firmware
doc_dir=${package_dir}/usr/share/doc/${package}
licenses_dir=${doc_dir}/licenses

rm -rf "${staging_dir}" "${package_dir}"
install -d "${staging_dir}" "${firmware_dir}" "${licenses_dir}"

# ----------------------------------------------------------------------------
# Copy firmware files and links to a staging directory

echo "Copy firmware files to ${staging_dir}"

ZSTD_CLEVEL=19 ./copy-firmware.sh --zstd "${staging_dir}"
debian/scripts/check-symlinks "${staging_dir}"

# ----------------------------------------------------------------------------
# Install included files

for f in debian/config/"${package}"/include.* ; do
	if ! [ -e "${f}" ] ; then
		continue
	fi
	echo "Include files from ${f}"
	while IFS= read -r items ; do
		# Don't quote 'items' to allow for globs
		# shellcheck disable=SC2086
		for item in "${staging_dir}"/${items}{,.zst} ; do
			if [ -e "${item}" ] || [ -L "${item}" ] ; then
				# Move the item from the staging to the package directory
				item_base=${item#"${staging_dir}"/}
				dest=${firmware_dir}/${item_base}
				echo "  ${item_base}"
				mkdir -p "${dest%/*}"
				mv "${item}" "${dest}"
			fi
		done
	done < <(sed -E '/^#|^$/d' "${f}")
done

# Sanity checks
debian/scripts/check-symlinks "${firmware_dir}"
debian/scripts/check-symlinks "${staging_dir}"

# ----------------------------------------------------------------------------
# Remove excluded files

for f in debian/config/"${package}"/exclude.* ; do
	if ! [ -e "${f}" ] ; then
		continue
	fi
	echo "Exclude files from ${f}"
	while IFS= read -r items ; do
		# Don't quote 'items' to allow for globs
		# shellcheck disable=SC2086
		for item in "${firmware_dir}"/${items}{,.zst} ; do
			if [ -e "${item}" ] || [ -L "${item}" ] ; then
				# Remove the item and any link target from the package directory
				item_base=${item#"${firmware_dir}"/}
				echo "  ${item_base}"
				rm -rf "${item}" "$(realpath "${item}")"
			fi
		done
	done < <(sed -E '/^#|^$/d' "${f}")
done

# Remove any dangling symlinks and empty directories
find "${firmware_dir}" -xtype l -delete
find "${firmware_dir}" -type d -empty -delete

# ----------------------------------------------------------------------------
# Install relevant licenses

echo "Install licenses"

install -m 0644 GPL-* "${licenses_dir}"

while IFS= read -r item ; do
	item=${item#./}
	item=${item%.zst}
	while IFS= read -r license ; do
		if ! [ -e "${licenses_dir}/${license}" ] ; then
			install -m 0644 "${license}" "${licenses_dir}"
		fi
	done < <(grep -A 1000 -h "^File:[ '\"]*${item}[ '\"]*$" WHENCE* | \
	         grep -m1 "^Licen[cs]e:" | tr " " "\n" | grep "LICEN[CS]E" || \
	         true)
done < <(cd "${firmware_dir}" && find . -type f)

# ----------------------------------------------------------------------------
# Install WHENCE files

echo "Install WHENCE files"

install -m 0644 WHENCE "${doc_dir}"/README
install -m 0644 WHENCE.ubuntu "${doc_dir}"/README.ubuntu
