summaryrefslogtreecommitdiff
path: root/chcleanup
blob: b1a02db5d93866ddbebfb920897e02edbeb31418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env 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
DRYRUN=${DRYRUN:-false}

source /etc/makepkg.conf
source /etc/libretools.conf
source ${HOME}/.makepkg.conf 2>/dev/null|| true

msg "Cleaning chroot..."

TMPDIR="$(mktemp -d /tmp/$(basename $0)-XXXXX)"
cleanup_log="${TMPDIR}"/libretools-cleanup.log

cp -a /var/lib/pacman/sync "${TMPDIR}/"
touch ${cleanup_log}

# If we're running makepkg
if [ -f PKGBUILD ]; then
    source PKGBUILD || true

    check=(${depends[@]} ${makedepends[@]} ${checkdepends[@]})

fi

# Get the full list of packages needed by dependencies, including the base system
sudo pacman -b "${TMPDIR}" \
            -Sp \
            --print-format "%n" \
            base base-devel sudo \
            ${CHROOTEXTRAPKG[@]} \
            ${check[@]} \
            >${cleanup_log}

# Diff installed packages against a clean chroot then remove leftovers
packages=($(comm -23 <(pacman -Qq | sort) \
                     <(sort -u ${cleanup_log})))

[ ${#packages[@]} -eq 0 ] && exit 0

msg2 "Removing %d packages" ${#packages[@]}

# Only remove leftovers, -Rcs removes too much
${DRYRUN} || sudo pacman --noconfirm -Rn ${packages[@]}
${DRYRUN} && echo ${packages[@]}

# Cleanup
${DRYRUN} || rm -fr ${TMPDIR}

exit $?