From 6a4730e7cba9709474a0bedb858a2938e3d3f77f Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 24 Oct 2024 21:39:34 +0200
Subject: [PATCH] Improve Raspi setup.

---
 many_releases/scripts/_misc.sh                | 29 ++++++++++++++-----
 {many_releases => testing}/aptmark/all        |  1 +
 testing/aptmark/raspi                         |  5 ++++
 .../etc/all/etc/locale.conf                   |  0
 testing/scripts/setup_raspi.sh                | 10 ++++---
 5 files changed, 34 insertions(+), 11 deletions(-)
 rename {many_releases => testing}/aptmark/all (99%)
 create mode 100644 testing/aptmark/raspi
 rename {many_releases => testing}/etc/all/etc/locale.conf (100%)

diff --git a/many_releases/scripts/_misc.sh b/many_releases/scripts/_misc.sh
index d1aa606..a1bf381 100755
--- a/many_releases/scripts/_misc.sh
+++ b/many_releases/scripts/_misc.sh
@@ -53,7 +53,7 @@ install_for_modules() {
     shift 1
     modules="$@"
     for module in "${modules}"; do
-        path="${aptmark_dir}/${module}"
+        path="${apt_mark_dir}/${module}"
         if [ ! -f "${path}" ]; then
             continue
         fi
@@ -67,15 +67,30 @@ install_for_modules() {
 }
 
 mark_nonrequireds_auto() {
-    path_list_unsorted="/tmp/list_unsorted"
-    path_list_all_packages="/tmp/list_all_packages"
-    path_list_white="/tmp/list_white"
-    path_list_black="/tmp/list_black"
+    path_list_unsorted='/tmp/list_unsorted'
+    path_list_all_packages='/tmp/list_all_packages'
+    path_list_white='/tmp/list_white'
+    path_list_black='/tmp/list_black'
     dpkg-query -Wf '${Package} ${Priority}\n' | grep ' required' | sed 's/ required//' > "${path_list_unsorted}"
     sort "${path_list_unsorted}" | uniq > "${path_list_white}"
     dpkg-query -Wf '${Package}\n' > "${path_list_unsorted}"
-    sort "${path_list_unsorted}" | uniq > "${path_list_all_packages}" 
+    sort "${path_list_unsorted}" | uniq > "${path_list_all_packages}"
     comm -3 "${path_list_all_packages}" "${path_list_white}" > "${path_list_black}"
     apt-mark auto `cat "${path_list_black}"`
-    rm "${path_list_unsorted}" "${path_list_all_packages}" "${path_list_white}" "${path_list_white}"
+    rm "${path_list_unsorted}" "${path_list_all_packages}" "${path_list_white}" "${path_list_black}"
+}
+
+upgrade_from_older_release() {
+    # Upgrade system to calling context's Debian release.
+    # Caution: Don't expect any customized /etc files to surivive this!
+    path_sources_list='/etc/apt/sources.list'
+    apt update
+    apt -y -o Dpkg::Options::='--force-confnew' upgrade
+    apt -y -o Dpkg::Options::='--force-confnew' full-upgrade
+    cp "../etc/all${path_sources_list}" "${path_sources_list}"
+    apt clean
+    apt update
+    apt -y -o Dpkg::Options::='--force-confnew' upgrade
+    apt -y -o Dpkg::Options::='--force-confnew' full-upgrade
+    apt -y autoremove
 }
diff --git a/many_releases/aptmark/all b/testing/aptmark/all
similarity index 99%
rename from many_releases/aptmark/all
rename to testing/aptmark/all
index 617b707..e0dc9ee 100644
--- a/many_releases/aptmark/all
+++ b/testing/aptmark/all
@@ -10,3 +10,4 @@ locales
 # extremely useful for basic network debugging; missed these more than once in an emergency
 netcat-traditional
 iputils-ping
+
diff --git a/testing/aptmark/raspi b/testing/aptmark/raspi
new file mode 100644
index 0000000..eab877a
--- /dev/null
+++ b/testing/aptmark/raspi
@@ -0,0 +1,5 @@
+# so we can still connect
+openssh-server
+# this gotta be good for _something_ …
+raspi-firmware
+
diff --git a/many_releases/etc/all/etc/locale.conf b/testing/etc/all/etc/locale.conf
similarity index 100%
rename from many_releases/etc/all/etc/locale.conf
rename to testing/etc/all/etc/locale.conf
diff --git a/testing/scripts/setup_raspi.sh b/testing/scripts/setup_raspi.sh
index 84c3f8a..e0966c6 100755
--- a/testing/scripts/setup_raspi.sh
+++ b/testing/scripts/setup_raspi.sh
@@ -13,18 +13,20 @@ cat "${FILENAME_PUBLIC_KEY}" >> ~/.ssh/authorized_keys
 rm "${FILENAME_PUBLIC_KEY}" 
 # service sshd restart
 
+upgrade_from_older_release
+
 # properly apt system
 for root in "${PATH_MANY}" '..'; do
-    copy_dirtree "${root}/${NAME_ETC_DIR}" "" all
+    copy_dirtree "${root}/${NAME_ETC_DIR}" "" all raspi
 done
 apt update
 mark_nonrequireds_auto
 for root in "${PATH_MANY}" '..'; do
-    install_for_modules "${root}/${NAME_APTMARK_DIR}" all
+    install_for_modules "${root}/${NAME_APTMARK_DIR}" all raspi
 done
 apt -y --purge autoremove
-apt -y -o Dpkg::Options::='--force-confnew' upgrade
-apt -y -o Dpkg::Options::='--force-confnew' full-upgrade
+
+# TODO test if isc-dhcp-client necessary (ifupdown certainly is)
 
 # Ensure our desired locale is available.
 # locale-gen
-- 
2.30.2