#!/bin/bash
# Builds cont8nuum-deps: an RPM with no files, only dependencies, so zypper
# knows what cont8nuum (installed by build.sh outside rpm) uses and never
# counts it as unneeded. It requires what runtime-deps.txt lists, plus the
# packages owning every library c8's installed programs link against.
#
#	packaging/make-deps-rpm.sh [--install]
#
# Run it after ./build.sh --install (it reads that install's file lists).
# --install installs the result with zypper.
set -eu

here=$(cd "$(dirname "$0")" && pwd)
root=$(dirname "$here")
out=$here/build
prefix=/usr/local

# Every ELF file the components installed into the prefix, and oo7 0.6
files=()
# (components sit one or two levels down: protocols, desktop/shell)
for log in "$root"/*/build-install/meson-logs/install-log.txt \
		"$root"/*/*/build-install/meson-logs/install-log.txt; do
	[ -f "$log" ] || continue
	while IFS= read -r f; do
		case $f in "#"*|"") continue ;; esac
		[ -f "$f" ] && head -c 4 "$f" 2>/dev/null | grep -q 'ELF' && files+=("$f")
	done < "$log"
done
for f in "$prefix"/libexec/oo7-daemon "$prefix"/libexec/oo7-portal; do
	[ -f "$f" ] && files+=("$f")
done
if [ ${#files[@]} -eq 0 ]; then
	echo "make-deps-rpm: nothing installed in $prefix; run ./build.sh --install first" >&2
	exit 1
fi

# The packages owning the libraries they load (our own, in the prefix,
# belong to no package and are left out)
libs=$(for f in "${files[@]}"; do
	ldd "$f" 2>/dev/null | awk '$2 == "=>" && $3 ~ /^\// {print $3}'
done | sort -u)
missing=$(for f in "${files[@]}"; do ldd "$f" 2>/dev/null | grep 'not found'; done | sort -u)
if [ -n "$missing" ]; then
	echo "make-deps-rpm: libraries not found (rebuild with ./build.sh --install):" >&2
	echo "$missing" >&2
	exit 1
fi
# (a library loaded from openSUSE's optimised x86-64-v3 copy counts as
# its base package: the copies are optional extras over it)
lib_pkgs=$(for l in $libs; do
	rpm -qf --qf '%{NAME}\n' "$(readlink -f "$l")" 2>/dev/null || true
done | grep -v 'is not owned' | sed 's/-x86-64-v[0-9]$//' | sort -u)

# The listed ones
deps=$(grep -E '^(Requires|Recommends): ' "$here/runtime-deps.txt")

version=0.1.$(date +%Y%m%d.%H%M)
mkdir -p "$out"/{SPECS,RPMS,BUILD,SOURCES,SRPMS}
spec=$out/SPECS/cont8nuum-deps.spec
{
	cat <<EOF
Name:           cont8nuum-deps
Version:        $version
Release:        0
Summary:        What the cont8nuum desktop needs installed
License:        CC0-1.0
BuildArch:      noarch

EOF
	# The dependency tags go before %description: after it, rpm takes
	# them for more of the description
	echo "# From packaging/runtime-deps.txt"
	echo "$deps"
	echo
	echo "# Libraries cont8nuum's programs in $prefix link against"
	for p in $lib_pkgs; do
		echo "Requires: $p"
	done
	cat <<'EOF'

%description
cont8nuum is built and installed with its own build.sh. This package
holds no files: it only says what cont8nuum uses, so the package manager
keeps those packages installed and never counts them as unneeded.

%prep
%build
%install
%files
EOF
} > "$spec"

rpmbuild --quiet -bb --define "_topdir $out" "$spec"
rpm_file=$(ls -t "$out"/RPMS/noarch/cont8nuum-deps-"$version"-*.noarch.rpm | head -1)

# What the RPM really carries must be what the spec says
want_req=$(( $(echo "$deps" | grep -c '^Requires') + $(echo "$lib_pkgs" | grep -c .) ))
want_rec=$(echo "$deps" | grep -c '^Recommends')
got_req=$(rpm -qp --requires "$rpm_file" | grep -vc '^rpmlib(')
got_rec=$(rpm -qp --recommends "$rpm_file" | grep -c .)
if [ "$got_req" -ne "$want_req" ] || [ "$got_rec" -ne "$want_rec" ]; then
	echo "make-deps-rpm: the RPM has $got_req requires and $got_rec recommends," \
		"the spec $want_req and $want_rec ($spec)" >&2
	exit 1
fi
echo "Built $rpm_file"
echo "  $(echo "$deps" | grep -c '^Requires') required and $(echo "$deps" | grep -c '^Recommends') recommended from runtime-deps.txt"
echo "  $(echo "$lib_pkgs" | grep -c .) library packages, from ${#files[@]} programs and libraries"

if [ "${1:-}" = "--install" ]; then
	# Without a terminal zypper can't ask and sudo needs an askpass, as
	# build.sh has it
	if [ -t 0 ]; then
		sudo zypper install --allow-unsigned-rpm "$rpm_file"
	else
		for a in "${SUDO_ASKPASS:-}" /usr/local/bin/c8-askpass \
				/usr/libexec/ssh/ksshaskpass /usr/bin/ksshaskpass \
				/usr/libexec/ssh/ssh-askpass; do
			if [ -n "$a" ] && [ -x "$a" ]; then
				export SUDO_ASKPASS="$a"
				break
			fi
		done
		sudo -A zypper --non-interactive install --allow-unsigned-rpm \
			"$rpm_file"
	fi
fi
