aboutsummaryrefslogtreecommitdiffstats
path: root/mkhint.bash-completion
blob: b786bc3c2438ffe80a78b7a25ab08b2b8ead741f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# bash completion for mkhint
# Install: sudo cp mkhint.bash-completion /etc/bash-completion.d/mkhint

_mkhintfile_completions() {
    local cur prev repo_dir hint_dir
    _init_completion || return

    repo_dir="/var/lib/sbopkg/SBo-danix"
    hint_dir="/etc/slackrepo/SBo-danix/hintfiles"

    local all_flags="--version -v --hintfile -f --new -n --list -l --clean -c --check -C --delete -d --no-dl -N --help -h"

    case "$prev" in
        --new|-n)
            local -a words=()
            while IFS= read -r f; do
                words+=("$(basename "${f%.info}")")
            done < <(find "$repo_dir" -mindepth 2 -maxdepth 2 -name "*.info" 2>/dev/null)
            COMPREPLY=($(compgen -W "${words[*]}" -- "$cur"))
            ;;
        --hintfile|-f|--delete|-d|--check|-C)
            local -a words=()
            for f in "$hint_dir"/*.hint; do
                [[ -f "$f" ]] && words+=("$(basename "${f%.hint}")")
            done
            COMPREPLY=($(compgen -W "${words[*]}" -- "$cur"))
            ;;
        --version|-v)
            local pkg="" i
            for (( i=1; i<${#COMP_WORDS[@]}-1; i++ )); do
                if [[ "${COMP_WORDS[i]}" == "--hintfile" || "${COMP_WORDS[i]}" == "-f" ]]; then
                    pkg="${COMP_WORDS[i+1]}"
                    break
                fi
            done
            if [[ -n "$pkg" ]]; then
                local hint_file="$hint_dir/${pkg}.hint"
                if [[ -f "$hint_file" ]]; then
                    local ver
                    ver=$(grep -m1 '^VERSION=' "$hint_file" | cut -d= -f2 | tr -d '"')
                    [[ -n "$ver" ]] && COMPREPLY=($(compgen -W "$ver" -- "$cur"))
                fi
            fi
            ;;
        *)
            COMPREPLY=($(compgen -W "$all_flags" -- "$cur"))
            ;;
    esac
}

complete -F _mkhintfile_completions mkhint.sh mkhint