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

function move_item()
{
	local item=${1} src_dir=${2} dst_dir=${3}
	local src dst

	src=${src_dir}/${item}
	dst=${dst_dir}/${item}

	if [ -e "${src}".zst ] ; then
		src=${src}.zst
		dst=${dst}.zst
	fi

	mkdir -p "${dst%/*}"
	mv "${src}" "${dst}"
}

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

package=${1}
arch=${2:-none}

echo "Install firmware files for ${package} (arch=${arch}) ..."

staging_dir=debian/build/${package}

package_dir=debian/${package}
firmware_dir=${package_dir}/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}"

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

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

for f in debian/config/"${package}"/include.* \
         debian/config/"${package}"/"${arch}"/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} ; do
			# Move the item from the staging to the package directory
			item=${item#"${staging_dir}"/}
			move_item "${item}" "${staging_dir}" "${firmware_dir}"
		done
	done < <(sed -E '/^#|^$/d' "${f}")
done

# ----------------------------------------------------------------------------
# Resolve dangling symlinks

# Install missing link targets
while IFS= read -r item ; do
	item=$(realpath --relative-to="${firmware_dir}" "${item}")
	move_item "${item}" "${staging_dir}" "${firmware_dir}"
done < <(find "${firmware_dir}" -xtype l)

# Install missing links
while IFS= read -r item ; do
	move_item "${item}" "${staging_dir}" "${firmware_dir}"
done < <(cd "${staging_dir}" && find . -xtype l)

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

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

for f in debian/config/"${package}"/exclude.* \
         debian/config/"${package}"/"${arch}"/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} ; do
			# Remove the item and any link target from the package directory
			rm -rf "$(realpath "${item}")" "${item}"
			rm -rf "$(realpath "${item}".zst)" "${item}".zst
		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
