1# msiinfo bash completion
2
3__msiinfo_all_commands ()
4{
5        local i IFS=" "$'\n'
6        for i in $(msiinfo --help | grep "^  [^-]"  | cut -d' ' -f 3)
7        do
8                case $i in
9                *) echo $i;;
10                esac
11        done
12}
13__msiinfo_commands=
14__msiinfo_commands="$(__msiinfo_all_commands 2>/dev/null)"
15
16_msiinfo ()
17{
18    COMPREPLY=()
19
20    in_array()
21    {
22        local i
23        for i in $2; do
24            [[ $i = $1 ]] && return 0
25        done
26        return 1
27    }
28
29    local cur prev
30    # _get_comp_words_by_ref is in bash-completion >= 1.2, which EL-5 lacks.
31    if type _get_comp_words_by_ref &>/dev/null; then
32        _get_comp_words_by_ref cur prev
33    else
34        cur="${COMP_WORDS[COMP_CWORD]}"
35        prev="${COMP_WORDS[COMP_CWORD-1]}"
36    fi
37
38    # parse main options and get command
39
40    local options="--help --version"
41    local command=
42    local command_first=
43    local path=
44
45    local i w
46    for (( i = 0; i < ${#COMP_WORDS[*]} - 1; i++ )); do
47        w="${COMP_WORDS[$i]}"
48        # command
49        if in_array "$w" "$__msiinfo_commands"; then
50            command="$w"
51            command_first=$((i+1))
52            break
53        fi
54    done
55
56    # complete base options
57
58    if [[ -z $command ]]; then
59        if [[ $cur == -* ]]; then
60            COMPREPLY=( $(compgen -W "$options $options_value" -- "$cur") )
61            return 0
62        fi
63
64        case "$prev" in
65            *)
66                COMPREPLY=( $(compgen -W "$__msiinfo_commands" -- "$cur") )
67                ;;
68        esac
69
70        return 0
71    fi
72
73    local after= after_more=
74    case $command in
75        streams|tables|extract|export|suminfo)
76            after="msi"
77            ;;
78    esac
79
80    local all_options="--help $options"
81    local all_options_value=""
82
83    # count non-option parameters
84
85    local i w
86    local last_option=
87    local after_counter=0
88    local after_word=
89    for (( i = $command_first; i < ${#COMP_WORDS[*]} - 1; i++)); do
90        w="${COMP_WORDS[$i]}"
91        if [[ ${w:0:1} = - ]]; then
92            if in_array "$w" "$all_options"; then
93                last_option="$w"
94                continue
95            elif in_array "$w" "$all_options_value"; then
96                last_option="$w"
97                ((i++))
98                continue
99            fi
100        else
101            last_word="$w"
102        fi
103        in_array "$last_option" "$options_arches" || ((after_counter++))
104    done
105
106    local after_options=
107    if [[ $after_counter -eq 0 ]] || [[ $after_more = true ]]; then
108        case $after in
109                msi)    _filedir msi;;
110        esac
111    fi
112    if [[ $after_counter -eq 1 ]]; then
113        case $command in
114            extract)
115                after_options="$(msiinfo streams "$last_word")" ;;
116            export)
117                after_options="$(msiinfo tables "$last_word")" ;;
118        esac
119    fi
120
121    if [[ $cur != -* ]]; then
122        all_options=
123        all_options_value=
124    fi
125
126    COMPREPLY+=( $(compgen -W "$all_options $all_options_value $after_options" -- "$cur" ) )
127
128    return 0
129}
130
131_msibuild ()
132{
133        local cur="${COMP_WORDS[COMP_CWORD]}"
134        case "$cur" in
135        -*)
136                COMPREPLY=( $(compgen -W "-s -q -i -a" -- "$cur" ) )
137                return 0
138                ;;
139        esac
140}
141
142complete -o default -o nospace -F _msiinfo msiinfo
143complete -o default -o nospace -F _msibuild msibuild
144