#!/bin/sh
# Builds cont8nuum's .deb packages for the Debian or Ubuntu this runs on,
# into OUTDIR (CI runs it in the distribution's image). The build and
# what goes in which package are stage.sh's; this packs them. Their
# library dependencies are dpkg-shlibdeps'; what the programs need
# besides (services, portals) is packaging/distro/DISTRO.txt.
#
# The version is stage.sh's, then -1+DISTRO:
# cont8nuum_0.0+git202612060046.3e29cef7b5ad-1+ubuntu26.04_amd64.deb
# says what it was built on. No tildes: artifact names can't have them.
#
#   packaging/deb/make-debs.sh OUTDIR
set -eu
mkdir -p "$1"
out=$(cd "$1" && pwd)
. "$(dirname "$0")/../stage.sh"
arch=$(dpkg --print-architecture)
version=$upstream-1+$(echo "$distro" | tr -d -)
echo "== cont8nuum $version for $distro ($arch)"

# Every name in the list must be something the archive has (the images
# drop apt's lists; Ubuntu's tarball fetched them first, Debian has none)
apt-get update -qq
missing=
for n in $(awk '$1 == "required" || $1 == "recommended" { print $2 }' "$list"); do
	apt-cache show "$n" >/dev/null 2>&1 || missing="$missing $n"
done
[ -z "$missing" ] || { echo "make-debs: not in $distro:$missing" >&2; exit 1; }

# dpkg-shlibdeps wants a debian/control to work in. Our own libraries
# (c8ui, the private ones) have no shlibs: they're found with -l and
# their packages are depended on by name instead.
mkdir -p "$work/src/debian"
{
	printf 'Source: cont8nuum\nMaintainer: lowtide <lowtide.sbs@posteo.pm>\n'
	for p in "$pkgs"/*; do
		printf '\nPackage: %s\nArchitecture: any\n' "$(basename "$p")"
	done
} > "$work/src/debian/control"
libpaths=
for d in $(find "$pkgs" -name 'lib*.so*' -printf '%h\n' | sort -u); do
	libpaths="$libpaths -l$d"
done
required=$(awk '$1 == "required" { printf "%s, ", $2 }' "$list")
recommended=$(awk '$1 == "recommended" { printf "%s, ", $2 }' "$list")

rm -f "$out"/cont8nuum*_"$version"_"$arch".deb
for p in "$pkgs"/*; do
	name=$(basename "$p")
	elfs=$(find "$p" -type f -exec sh -c \
		'head -c4 "$1" | grep -q "^.ELF"' _ {} \; -print)
	shdeps=
	if [ -n "$elfs" ]; then
		shdeps=$(cd "$work/src" && dpkg-shlibdeps -O --ignore-missing-info \
			$libpaths $elfs 2>"$work/shlibdeps.log" |
			sed -n 's/^shlibs:Depends=//p') || {
			cat "$work/shlibdeps.log" >&2; exit 1; }
	fi
	deps=$shdeps
	recs=
	case "$name" in
	cont8nuum) deps="$required$deps" recs=${recommended%, } ;;
	*) deps="cont8nuum (= $version)${deps:+, $deps}" ;;
	esac
	deps=${deps%, }
	mkdir -p "$p/DEBIAN"
	desc=$(describe "$name" | sed '2,$s/^/ /')
	{
		echo "Package: $name"
		echo "Version: $version"
		echo "Architecture: $arch"
		echo "Maintainer: lowtide <lowtide.sbs@posteo.pm>"
		echo "Installed-Size: $(du -sk --exclude=DEBIAN "$p" | cut -f1)"
		[ -z "$deps" ] || echo "Depends: $deps"
		[ -z "$recs" ] || echo "Recommends: $recs"
		echo "Section: x11"
		echo "Priority: optional"
		echo "Homepage: https://knot.lowtide.sbs/lowtide.sbs/cont8nuum"
		echo "Description: $desc"
	} > "$p/DEBIAN/control"
	# What's in /etc is the admin's once installed
	if [ -d "$p/etc" ]; then
		(cd "$p" && find etc -type f -printf '/%p\n') > "$p/DEBIAN/conffiles"
	fi
	# Libraries in the linker's own folders need its cache refreshed
	if find "$p/usr/$libdir" -maxdepth 1 -name 'lib*.so.*' \
			2>/dev/null | grep -q .; then
		echo "activate-noawait ldconfig" > "$p/DEBIAN/triggers"
	fi
	file=${name}_${version}_$arch.deb
	dpkg-deb --root-owner-group -Zxz --build "$p" "$out/$file" >/dev/null
	echo "== $file ($(du -h "$out/$file" | cut -f1)): ${deps:-no dependencies}"
done
