#!/bin/sh
# Execute a command inside the alternatives system

usage() {
    printf '\033[1;33m-> \033[m%s\n' "usage: ${0##*/} [pkg] [command]"
    exit "$1"
}

case "$1" in ''|--help|-h) usage 0; esac
[ "$2" ] || usage 1

pkg=$1

[ "${2##/*}" ] && command=">usr>bin>$2" ||
    command="$(printf '%s' "$2" | sed 's#/#>#g')"

[ -h "${file:=/var/db/cpt/choices/$pkg$command}" ] && {
    printf 'ERROR: %s\n' "$file is a symlink"
    exit 1
}

shift 2
exec "/var/db/cpt/choices/$pkg$command" "$@"