1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-28 15:56:33 +00:00
hosts/scripts/hosts-completion
2020-04-16 11:46:45 -07:00

200 lines
5.1 KiB
Bash
Executable File

#!/usr/bin/env bash
###############################################################################
# hosts / scripts / hosts-completion
#
# Install and uninstall completions for `hosts`.
#
# https://github.com/xwmx/hosts
###############################################################################
###############################################################################
# Strict Mode
#
# More Information:
# https://github.com/xwmx/bash-boilerplate#bash-strict-mode
###############################################################################
set -o nounset
set -o errexit
set -o errtrace
set -o pipefail
IFS=$'\n\t'
###############################################################################
export _MY_DIR=
_MY_DIR="$(cd "$(dirname "$0")"; pwd)"
if [[ -z "${_MY_DIR}" ]] || [[ ! -d "${_MY_DIR}" ]]
then
exit 1
fi
# Usage: _get_bash_completion_path
_get_bash_completion_path() {
local _bash_completion_path=
if [[ -n "${BASH_COMPLETION_COMPAT_DIR:-}" ]] &&
[[ -w "${BASH_COMPLETION_COMPAT_DIR:-}" ]]
then
_bash_completion_path="${BASH_COMPLETION_COMPAT_DIR}"
fi
if [[ -z "${_bash_completion_path:-}" ]]
then
local _maybe_path
_maybe_path="$(
pkg-config --variable=completionsdir bash-completion 2>/dev/null || true
)"
if [[ -n "${_maybe_path:-}" ]] &&
[[ -w "${_maybe_path:-}" ]]
then
_bash_completion_path="${_maybe_path}"
fi
fi
if [[ -z "${_bash_completion_path:-}" ]] &&
[[ -d "/usr/local/etc/bash_completion.d" ]] &&
[[ -w "/usr/local/etc/bash_completion.d" ]]
then
_bash_completion_path="/usr/local/etc/bash_completion.d"
fi
if [[ -z "${_bash_completion_path:-}" ]] &&
[[ -d "/etc/bash_completion.d" ]] &&
[[ -w "/etc/bash_completion.d" ]]
then
_bash_completion_path="/etc/bash_completion.d"
fi
printf "%s\\n" "${_bash_completion_path:-}"
}
# subcommands ################################################### subcommands #
# Usage: _check
_check() {
local _bash_completion_path=
_bash_completion_path="$(_get_bash_completion_path)"
local _exists=0
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]] &&
[[ -w "${_bash_completion_path}" ]] &&
[[ -e "${_bash_completion_path}/hosts" ]]
then
_exists=1
printf "Exists: %s\\n" "${_bash_completion_path}/hosts"
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]] &&
[[ -w "${_zsh_completion_path}" ]] &&
[[ -e "${_zsh_completion_path}/_hosts" ]]
then
_exists=1
printf "Exists: %s\\n" "${_zsh_completion_path}/_hosts"
fi
((_exists)) || return 1
}
# Usage: _install
_install() {
local _bash_completion_path=
_bash_completion_path="$(_get_bash_completion_path)"
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]] &&
[[ -w "${_bash_completion_path}" ]] &&
[[ ! -e "${_bash_completion_path}/hosts" ]]
then
cp \
"${_MY_DIR}/../etc/hosts-completion.bash" \
"${_bash_completion_path}/hosts"
printf "Completion installed: %s\\n" "${_bash_completion_path}/hosts"
# Cleanup old completion script.
if [[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
then
rm "${_bash_completion_path}/hosts-completion.bash"
fi
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]] &&
[[ -w "${_zsh_completion_path}" ]] &&
[[ ! -e "${_zsh_completion_path}/_hosts" ]]
then
cp \
"${_MY_DIR}/../etc/hosts-completion.zsh" \
"${_zsh_completion_path}/_hosts"
printf "Completion installed: %s\\n" "${_zsh_completion_path}/_hosts"
fi
}
# Usage: _uninstall
_uninstall() {
local _bash_completion_path=
_bash_completion_path="$(_get_bash_completion_path)"
if [[ -n "${_bash_completion_path:-}" ]] &&
[[ -d "${_bash_completion_path}" ]] &&
[[ -w "${_bash_completion_path}" ]] &&
[[ -e "${_bash_completion_path}/hosts" ]]
then
rm "${_bash_completion_path}/hosts"
printf "Completion removed: %s\\n" "${_bash_completion_path}/hosts"
# Cleanup old completion script.
if [[ -e "${_bash_completion_path}/hosts-completion.bash" ]]
then
rm "${_bash_completion_path}/hosts-completion.bash"
fi
fi
local _zsh_completion_path="/usr/local/share/zsh/site-functions"
if [[ -d "${_zsh_completion_path}" ]] &&
[[ -w "${_zsh_completion_path}" ]] &&
[[ -e "${_zsh_completion_path}/_hosts" ]]
then
rm "${_zsh_completion_path}/_hosts"
printf "Completion removed: %s\\n" "${_zsh_completion_path}/_hosts"
fi
}
# main ################################################################# main #
# Usage: _main <args>...
_main() {
_subcommand="${1:-}"
case "${_subcommand}" in
check)
_check
;;
install)
_install
;;
uninstall)
_uninstall
;;
*)
cat <<HEREDOC
hosts-completion
Usage:
hosts-completion [check | install | uninstall]
Description:
Manage completion scripts for \`hosts\`.
HEREDOC
;;
esac
} && _main "$@"