#!/bin/sh -ef
# List and swap to alternatives

parser_definition() {
    setup REST help:usage -- "usage: ${0##*/} [-] [package file]"
    global_options
}

if [ -f ./cpt-lib ]; then . ./cpt-lib; else . cpt-lib; fi

# We don't need to be root in order to list alternatives, so skip privilege
# elevation if no arguments are passed to the script.
[ -z "$1" ] || [ -w "$CPT_ROOT/" ] || [ "$uid" = 0 ] || {
        as_root "$0" "$@"
        exit $?
}

case "$1" in
    -)
        while read -r pkg path; do
            pkg_swap "$pkg" "$path"
        done
        ;;
    '')
        # Go over each alternative and format the file name for listing.
        # (pkg_name>usr>bin>ls)
        set +f; for pkg in "$sys_db/../choices/"*; do
                    printf '%s\n' "${pkg##*/}"
        done | sed 's|>| /|; s|>|/|g; /\*/d'
        ;;
    *) pkg_swap "$@" ;;
esac