2014-05-22 19:52:55 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2014-05-29 16:45:24 +00:00
|
|
|
## prevent sourcing
|
|
|
|
if [[ ${BASH_SOURCE[0]} != $0 ]]; then
|
|
|
|
echo >&2 "error: \`bpkg' cannot be sourced"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
## bpkg version
|
2014-09-11 18:30:45 +00:00
|
|
|
VERSION="0.2.6"
|
2014-05-22 19:52:55 +00:00
|
|
|
|
|
|
|
## output error to stderr
|
|
|
|
error () {
|
|
|
|
printf >&2 "error: %s\n" "${@}"
|
|
|
|
}
|
|
|
|
|
|
|
|
## output usage
|
|
|
|
usage () {
|
2014-05-25 06:31:04 +00:00
|
|
|
echo ""
|
|
|
|
echo " usage: bpkg [-hV] <command> [args]"
|
|
|
|
echo ""
|
2014-05-22 19:52:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 21:19:13 +00:00
|
|
|
## commands
|
|
|
|
commands () {
|
|
|
|
{
|
|
|
|
declare -a local cmds=( $(
|
|
|
|
bpkg-suggest 'bpkg-' |
|
2014-05-26 04:36:49 +00:00
|
|
|
tail -n+2 |
|
2014-06-03 03:51:58 +00:00
|
|
|
xargs basename -a |
|
2014-05-26 04:36:49 +00:00
|
|
|
sort -u |
|
|
|
|
sed 's/bpkg-//g' |
|
2014-05-25 21:19:13 +00:00
|
|
|
tr '\n' ' '
|
|
|
|
) )
|
|
|
|
|
|
|
|
echo "${cmds[@]}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 19:52:55 +00:00
|
|
|
## feature tests
|
|
|
|
features () {
|
2014-05-25 21:19:13 +00:00
|
|
|
declare -a local features=(bpkg-json bpkg-suggest)
|
2014-05-24 19:29:07 +00:00
|
|
|
for ((i = 0; i < ${#features[@]}; ++i)); do
|
|
|
|
local f="${features[$i]}"
|
|
|
|
if ! type "${f}" > /dev/null 2>&1; then
|
|
|
|
error "Missing "${f}" dependency"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
2014-05-22 19:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bpkg () {
|
|
|
|
local arg="$1"
|
|
|
|
local cmd=""
|
|
|
|
shift
|
|
|
|
|
2014-05-24 19:29:07 +00:00
|
|
|
## test for required features
|
|
|
|
features || return $?
|
|
|
|
|
2014-05-22 19:52:55 +00:00
|
|
|
case "${arg}" in
|
|
|
|
|
|
|
|
## flags
|
|
|
|
-V|--version)
|
|
|
|
echo "${VERSION}"
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
-h|--help)
|
|
|
|
usage
|
2014-05-25 21:19:13 +00:00
|
|
|
echo
|
|
|
|
echo "Here are some commands available in your path:"
|
|
|
|
echo
|
|
|
|
local cmds=($(commands))
|
|
|
|
for cmd in ${cmds[@]}; do
|
|
|
|
echo " ${cmd}"
|
|
|
|
done
|
2014-05-22 19:52:55 +00:00
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
2014-05-25 21:19:13 +00:00
|
|
|
if [ -z "${arg}" ]; then
|
|
|
|
usage
|
|
|
|
return 1
|
|
|
|
fi
|
2014-05-22 19:52:55 +00:00
|
|
|
cmd="bpkg-${arg}"
|
|
|
|
if type -f "${cmd}" > /dev/null 2>&1; then
|
|
|
|
"${cmd}" "${@}"
|
|
|
|
return $?
|
2014-05-25 21:19:13 +00:00
|
|
|
else
|
|
|
|
echo >&2 "error: \`${arg}' is not a bpkg command."
|
|
|
|
{
|
2014-05-26 04:36:49 +00:00
|
|
|
declare -a local res=($(commands))
|
2014-05-25 21:19:13 +00:00
|
|
|
|
|
|
|
if [ ! -z "${res}" ]; then
|
|
|
|
echo
|
|
|
|
echo >&2 "Did you mean one of these?"
|
2014-10-07 19:36:53 +00:00
|
|
|
found=0
|
|
|
|
for r in "${res[@]}"; do
|
2014-05-26 04:36:49 +00:00
|
|
|
if [[ "$r" == *"${arg}"* ]]; then
|
|
|
|
echo " $ bpkg ${r}"
|
2014-10-07 19:36:53 +00:00
|
|
|
found=1
|
2014-05-26 04:36:49 +00:00
|
|
|
fi
|
2014-05-25 21:19:13 +00:00
|
|
|
done
|
2014-10-07 19:36:53 +00:00
|
|
|
if [ "$found" == "0" ]; then
|
|
|
|
for r in "${res[@]}"; do
|
|
|
|
echo " $ bpkg ${r}"
|
|
|
|
done
|
|
|
|
fi
|
2014-05-25 21:19:13 +00:00
|
|
|
return 1
|
|
|
|
else
|
|
|
|
usage
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
2014-05-22 19:52:55 +00:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
2014-05-25 06:31:04 +00:00
|
|
|
usage
|
2014-05-26 04:36:49 +00:00
|
|
|
return 1
|
2014-05-22 19:52:55 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 16:45:24 +00:00
|
|
|
bpkg "${@}"
|
|
|
|
exit $?
|