summaryrefslogtreecommitdiff
path: root/chcleanup
diff options
context:
space:
mode:
Diffstat (limited to 'chcleanup')
-rwxr-xr-xchcleanup54
1 files changed, 51 insertions, 3 deletions
diff --git a/chcleanup b/chcleanup
index 7074b84..b5f631c 100755
--- a/chcleanup
+++ b/chcleanup
@@ -1,11 +1,59 @@
#!/bin/bash
+# (c) Nicolás Reynolds <fauno@parabola.nu>
+# Released under GPLv3
+#
+# Performs chroot cleanup smartly, it only removes the unneeded packages or
+# leaves you with a cleansystem
+#
+# See: HOOKPREBUILD
+
+set -e
[ ! -f /etc/libretools.d/cleansystem ] && exit 1
+[ ! -d "${DB:-/var/lib/libretools/clean}"/sync ] && exit 1
+
+source $(dirname $0)/libremessages
+source /etc/makepkg.conf
+source ${HOME}/.makepkg.conf 2>/dev/null|| true
+
+msg "Cleaning chroot..."
+
+cleanup_log=/tmp/libretools-cleanup.log
+touch ${cleanup_log}
+
+# If we're running makepkg
+if [ -f PKGBUILD ]; then
+ source PKGBUILD || true
+
+ check=(${depends[@]} ${makedepends[@]} ${checkdepends[@]})
+
+ if [ ${#check[@]} -ne 0 ]; then
+
+# Update the cleansystem database
+ sudo pacman -b "${BD:-/var/lib/libretools/clean}" -Sy
+# Get the full list of packages needed by dependencies
+ sudo pacman -b "${BD:-/var/lib/libretools/clean}" \
+ -Sp \
+ --print-format "%n" \
+ ${check[@]} \
+ >${cleanup_log}
+ fi
+fi
+
+# Diff installed packages against a clean chroot and needed packages,
+# then remove leftovers
+packages=($(comm -23 <(pacman -Qq | sort) \
+ <(cat /etc/libretools.d/cleansystem ${cleanup_log} | sort -u)
+ ))
+
+[ ${#packages[@]} -eq 0 ] && exit 0
-packages=($(comm -23 <(pacman -Qq | sort) <(sort /etc/libretools.d/cleansystem)))
+msg2 "Removing %d packages" ${#packages[@]}
-echo "Removing: ${packages[@]}"
+# Only remove leftovers, -Rcs removes too much
+sudo pacman --noconfirm -Rn ${packages[@]}
-sudo pacman --noconfirm -Rcs ${packages[@]}
+# Cleanup
+rm -f ${cleanup_log}
exit $?