# Script to-be-sourced to enable ccm CLI tabcompletion
# (e.g. in case not all bashcompletion scripts in
# /etc/bash_completion.d are/can not be enabled by default).
#
# This loads quattor-ccm bash completion
#   - interactive session only
#   - any user with low enough UID (like admins)
#       - can be set via CCM_TABCOMPLETION_MINUID
#       - defaults to 9999
#
# Use following pan snippet to configure it via /etc/profile.d
# "/software/components/symlink/links" = append(dict(
#        "name", "/etc/profile.d/quattor-ccm.sh",
#        "target", "/usr/share/quattor/quattor-ccm-profile",
#        "replace", dict("all", "yes"),
#        ));
#

# source the tabcompletion script if variable is not yet defined
# (and current uid is lower than minuid)
function _quattor_load_bash_completion ()
{
    local fn variable

    fn=/etc/bash_completion.d/$1
    loaded=$2

    # do not load if already loaded
    if [[ -z "${!loaded}" && -f $fn ]]; then
        # these are not local, and unset at the end
        if [ -z "$_q_minuid" ]; then
            _q_minuid=${CCM_TABCOMPLETION_MINUID:-9999}
            _q_myid=`id -u 2>/dev/null`
        fi
        if [ ${_q_myid:-0} -lt $_q_minuid ]; then
            source $fn
        fi
    fi
}

if [[ $- == *i* ]]; then
    _quattor_load_bash_completion quattor-ccm _quattor_ccm_CLI_longoptions
    _quattor_load_bash_completion ncm-ncd.sh _quattor_ncm_ncd_longoptions
fi

unset _q_minuid _q_myid
unset -f _quattor_load_bash_completion
