--- /dev/null
+. lib/apt_digested.sh
+. lib/prefixed_msg.sh
+
+ensure_packages_of_tags() {
+prefixed_msg_init 'ensure_packages_of_tags'
+
+_APT_ARG="-q -q -o 'Dpkg::Options::=--force-confnew' install"
+for _TAG in $@; do
+ prefixed_msg_no_nl "For tag '${_TAG}', "
+ if [ "$(echo ${_TAG} | cut -d':' -f1)" = 'keep_if_installed' ]; then
+ _PACKAGE=$(echo ${_TAG} | cut -d':' -f2)
+ printf 'checking if installed … '
+ if dpkg-query -Wf '${Package}\n' | grep '^'"${_PACKAGE}"'$' > /dev/null; then
+ echo 'yup, keeping!'
+ apt_digested "${_APT_ARG} ${_PACKAGE}"
+ else
+ echo 'nope, nothing to keep.'
+ fi
+ continue
+ fi
+ _PATH_APTMARK_TAG="../aptmark/${_TAG}"
+ if [ ! -f "${_PATH_APTMARK_TAG}" ]; then
+ echo 'no file, ignoring.'
+ continue
+ fi
+ _PACKAGES=$(cat "${_PATH_APTMARK_TAG}" | sed -E 's/#.*//g' | sed -z 's/\n/ /g' | sed 's/ */ /g' | cut -c 2-)
+ if [ -z "${_PACKAGES}" ]; then
+ echo 'nothing to install.'
+ else
+ echo "ensuring installation of: ${_PACKAGES}"
+ apt_digested "${_APT_ARG} ${_PACKAGES}"
+ fi
+done
+prefixed_msg_exit
+}
--- /dev/null
+#!/bin/sh
+set -e
+cd $(dirname "$0")
+. lib/apt_digested.sh
+. lib/ensure_packages_of_tags.sh
+. lib/prefixed_msg.sh
+
+prefixed_msg_init
+_INSTALL_TAGS="$@"
+prefixed_msg "narrow system to what is required explicitly by Debian and by us, as per these INSTALL_TAGS: ${_INSTALL_TAGS}"
+
+_TOK_REQ='required'
+_PATH_LIST_PREFIX=/tmp/list_
+_PATH_LIST_UNSORTED="${_PATH_LIST_PREFIX}unsorted"
+_PATH_LIST_WHITE="${_PATH_LIST_PREFIX}white"
+_PATH_LIST_ALL_PACKAGES="${_PATH_LIST_PREFIX}all_packages"
+_PATH_LIST_BLACK="${_PATH_LIST_PREFIX}black"
+
+prefixed_msg_no_nl "collect packages deemed '${_TOK_REQ}' by Debian: "
+dpkg-query -Wf '${Package} ${Priority}\n' | grep " ${_TOK_REQ}" | cut -d' ' -f1 > "${_PATH_LIST_UNSORTED}"
+sort "${_PATH_LIST_UNSORTED}" > "${_PATH_LIST_WHITE}"
+echo $(cat "${_PATH_LIST_WHITE}")
+
+prefixed_msg 'collect installed packages outside this selection …'
+dpkg-query -Wf '${Package}\n' > "${_PATH_LIST_UNSORTED}"
+sort "${_PATH_LIST_UNSORTED}" > "${_PATH_LIST_ALL_PACKAGES}"
+comm -3 "${_PATH_LIST_ALL_PACKAGES}" "${_PATH_LIST_WHITE}" > "${_PATH_LIST_BLACK}"
+
+prefixed_msg 'apt-mark auto from diff …'
+apt-mark auto `cat "${_PATH_LIST_BLACK}"` > /dev/null
+rm "${_PATH_LIST_UNSORTED}" "${_PATH_LIST_ALL_PACKAGES}" "${_PATH_LIST_WHITE}" "${_PATH_LIST_BLACK}"
+
+prefixed_msg 'install or mark as manually installed packages from our own selections …'
+ensure_packages_of_tags ${_INSTALL_TAGS}
+
+prefixed_msg 'run autopurge to get rid of all unwanted packages …'
+apt_digested '-q -q autopurge'
+
+prefixed_msg_exit