aboutsummaryrefslogtreecommitdiff
path: root/contrib/cpt-reset
blob: 4dfe52d11521978baefff69174bb4b758387159c (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
#!/bin/sh -e
# Remove all packages except for the base
#
# Disable word-splittng warnings as they're safe here.
# shellcheck disable=SC2046

[ "$1" ] && {
    printf 'usage: %s\n\nRemove all packages not defined in the base.\n' \
           "${0##*/}"
    exit 0
}

sys_db="$CPT_ROOT/var/db/cpt/installed"

# Get base packages and their dependencies.
[ -f "$CPT_ROOT/etc/cpt-base" ] && while read -r basepkg _; do
    case "$basepkg" in \#*) continue; esac
    base=" $basepkg $base"
    [ -f "$sys_db/$basepkg/depends" ] || continue
    while read -r dep make; do
        [ "$make" ] && continue
        base=" $dep $base"
    done < "$sys_db/$basepkg/depends"
done < "$CPT_ROOT/etc/cpt-base"

set --
cd "$sys_db"

for pkg in *; do
    case "$base" in *" $pkg "*) continue; esac
    set -- "$pkg" "$@"
done

[ "$1" ] && {
    printf 'WARNING: This will remove \033[1m%s\033[m package(s).\n' "$#"
    printf 'Base packages can be redefined in %s\n' "$CPT_ROOT/etc/cpt-base"
    printf 'Continue? [Enter/Ctrl+C]\n'
    read -r _ && CPT_FORCE=1 cpt-remove "$@"
}