#!/bin/sh -e
# Remove all packages except for the base
#
# Disable word-splittng warnings as they're safe here.
# shellcheck disable=SC2046

## SYNOPSIS:
## .Nm
## DESCRIPTION:
## .Nm
## removes all packages from the system that is not defined as a base package in
## .Pa /etc/cpt-base .

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

. cpt-lib
base=$(pkg_get_base nonl)

set --
cd "$sys_db"

set +f; for pkg in *; do
    contains "$base" "$pkg" || 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 "$@"
}