# -*-eselect-*- vim: ft=eselect # Copyright 2005-2012 Gentoo Foundation # Distributed under the terms of the GNU GPL version 2 or later # $Id$ DESCRIPTION="Manage contributed bash-completion scripts" MAINTAINER="eselect@gentoo.org" find_targets() { local bcdir bc for bcdir in "${EROOT%/}/usr/share/bash-completion" ${ES_BASHCOMP_DIRS} do for bc in "${bcdir}"/*; do [[ -f ${bc} && ${bc} != *~ ]] && basename "${bc}" done done | sort -u } is_enabled() { local bcdir bcdir=${ROOT%/}/${HOME}/.bash_completion.d if [[ $1 == "--global" ]]; then bcdir=${EROOT%/}/etc/bash_completion.d shift fi [[ -e ${bcdir}/$1 ]] } ### list action ### describe_list() { echo "List available completions" } describe_list_options() { echo "--global : List globally enabled completions" } do_list() { local targets opts i targets=( $(find_targets) ) if [[ $1 == "--global" ]]; then opts="--global" shift fi for (( i = 0; i < ${#targets[@]}; i++ )); do is_enabled ${opts} "${targets[i]}" \ && targets[i]=$(highlight_marker "${targets[i]}") done write_list_start "Available completions:" write_numbered_list -m "(none found)" "${targets[@]}" } ### enable action ### describe_enable() { echo "Enable specified completion(s)" } describe_enable_parameters() { echo "" } describe_enable_options() { echo "--global : Enable for all users" echo " : Target name or number (from 'list' action)" } do_enable() { local bcdir bc mode="" file target targets bcdir=${ROOT%/}/${HOME}/.bash_completion.d if [[ $1 == "--global" ]]; then bcdir=${EROOT%/}/etc/bash_completion.d mode="-m 0755" shift fi [[ $# -eq 0 ]] && die -q "You didn't specify any completions to enable." # create directory if necessary if [[ ! -d ${bcdir} && -w $(dirname ${bcdir}) ]]; then mkdir ${mode} "${bcdir}" || die -q "Failed to create ${bcdir}" elif [[ ! -d ${bcdir} ]]; then die -q "You don't have permission to create ${bcdir}" fi # make sure we have proper permissions [[ -w ${bcdir} ]] \ || die -q "You don't have permission to write to ${bcdir}" targets=( $(find_targets) ) for bc in "$@"; do # ignore any unrecognized options [[ ${bc} == --* ]] && continue target=${bc} is_number "${target}" && target=${targets[target-1]} [[ -z ${target} ]] \ && die -q "Target \"${bc}\" doesn't appear to be valid!" bc=${target} # what form is the argument in? case "${bc}" in # absolute path /*) file=${ROOT%/}/${bc} ;; # relative path */*) file=${ROOT%/}/${PWD}/${bc} ;; # no path *) # assume /usr/share/bash-completion file=${EROOT%/}/usr/share/bash-completion/${bc} if [[ ! -f ${file} ]]; then for x in ${ES_BASHCOMP_DIRS}; do [[ -f ${x}/${bc} ]] && file=${x}/${bc} done fi ;; esac # does it exist? if [[ ! -f ${file} ]]; then write_error_msg "${file} doesn't exist" continue fi # already installed? if [[ -e ${bcdir}/$(basename ${bc}) ]]; then write_error_msg "$(basename ${bc}) is already installed" continue fi # finally, create the symlink ln -s "$(relative_name "${file}" "${bcdir}")" "${bcdir}/" \ || die -q "Failed to create symlink from ${file} to ${bcdir}" done } ### disable action ### describe_disable() { echo "Disable specified completion(s)" } describe_disable_parameters() { echo "" } describe_disable_options() { echo "--global : Disable for all users" echo " : Target name or number (from 'list' action)" } do_disable() { local bcdir bc targets target file bcdir=${ROOT%/}/${HOME}/.bash_completion.d if [[ $1 == "--global" ]]; then bcdir=${EROOT%/}/etc/bash_completion.d shift fi [[ $# -eq 0 ]] && die -q "You didn't specify any completions to disable" targets=( $(find_targets) ) for bc in "$@"; do # ignore any unrecognized options [[ ${bc} == --* ]] && continue target=${bc} is_number "${target}" && target=${targets[target-1]} [[ -z ${target} ]] \ && die -q "Target \"${bc}\" doesn't appear to be valid!" bc=${target} file=${bcdir}/${bc} # is it installed? if [[ ! -e ${file} ]]; then write_error_msg "${bc} is not installed" continue fi # remove it if we have permissions if [[ -w $(dirname ${file}) ]]; then rm "${file}" || die -q "Failed to remove ${file}" else die -q "You don't have permission to remove ${file}" fi done }