From ca94a7ce5793013b2b132ceec7d9aaef1b48950b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 12 Aug 2024 18:31:58 +0200
Subject: [PATCH] Add some missing stuff.

---
 testing/setup_scripts/misc.sh               |  2 +-
 testing/setup_scripts/purge_nonrequireds.sh | 30 +++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100755 testing/setup_scripts/purge_nonrequireds.sh

diff --git a/testing/setup_scripts/misc.sh b/testing/setup_scripts/misc.sh
index 64a8916..f6d2367 100644
--- a/testing/setup_scripts/misc.sh
+++ b/testing/setup_scripts/misc.sh
@@ -14,7 +14,7 @@ expect_n_args() {
     explainer="$2"
     shift 2
     if [ "$#" -lt "${min_args}" ]; then
-        echo "Need at least ${1} arguments … ${explainer}"
+        echo "Need at least ${min_args} arguments … ${explainer}"
         false
     fi
 }
diff --git a/testing/setup_scripts/purge_nonrequireds.sh b/testing/setup_scripts/purge_nonrequireds.sh
new file mode 100755
index 0000000..48065fc
--- /dev/null
+++ b/testing/setup_scripts/purge_nonrequireds.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+# This script removes all Debian packages that are not of Priority
+# "required" or not depended on by packages of priority "required"
+# or not listed in the argument-selected files of apt-mark/.
+set -e
+. ./misc.sh
+
+# FIXME packages listed twice in the aptmark_dir get blacklisted?
+
+dpkg-query -Wf '${Package} ${Priority}\n' | grep ' required' | sed 's/ required//' > /tmp/list_white_unsorted
+for target in "$@"; do
+    path="${aptmark_dir}/${target}"
+    cat "${path}" | while read line; do
+        if [ ! $(echo "${line}" | cut -c1) = "#" ]; then
+            echo "${line}" >> /tmp/list_white_unsorted
+        fi
+    done
+done
+sort /tmp/list_white_unsorted > /tmp/list_white
+dpkg-query -Wf '${Package}\n' > /tmp/list_all_packages
+sort /tmp/list_all_packages > /tmp/foo
+mv /tmp/foo /tmp/list_all_packages
+comm -3 /tmp/list_all_packages /tmp/list_white > /tmp/list_black
+apt-mark auto `cat /tmp/list_black`
+DEBIAN_FRONTEND=noninteractive apt-get -y --purge autoremove
+rm /tmp/list_all_packages /tmp/list_white_unsorted /tmp/list_white /tmp/list_black
+
+# Somehow, auto-mounts get undone by all of this, so re-mount /etc/fstab.
+# TODO: Find out why.
+mount -a
-- 
2.30.2