1# Bash completion support for oggz.
2# Conrad Parker, Aug 3 2008
3
4# Copyright (C) 2008 Annodex Association
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12#
13# - Redistributions in binary form must reproduce the above copyright
14# notice, this list of conditions and the following disclaimer in the
15# documentation and/or other materials provided with the distribution.
16#
17# - Neither the name of the Annodex Association nor the names of its
18# contributors may be used to endorse or promote products derived from
19# this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24# PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ASSOCIATION OR
25# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33OGGZ_COMMANDS="chop codecs comment diff dump help info known-codecs merge rip scan sort validate"
34
35__oggzcomp ()
36{
37    COMPREPLY=( $( compgen -W '$*' -- $cur ))
38
39    return
40}
41
42__oggz_commands ()
43{
44    __oggzcomp $OGGZ_COMMANDS
45}
46
47__oggz_known_codecs ()
48{
49    local cur=$1
50
51    cur_LOWERCASE=$(echo $cur | tr '[:upper:]' '[:lower:]')
52
53    KNOWN=$(oggz-known-codecs)
54    KNOWN_LOWERCASE=$(echo $KNOWN | tr '[:upper:]' '[:lower:]')
55
56    # If the user has typed nothing, or lowercase, complete on lowercased
57    # codec names
58    if [ -z $cur -o $cur = $cur_LOWERCASE ] ; then
59      __oggzcomp "$KNOWN_LOWERCASE"
60    else
61      __oggzcomp "$KNOWN"
62    fi
63}
64
65# See http://wiki.xiph.org/index.php/MIME_Types_and_File_Extensions
66_oggz_filedir ()
67{
68    _filedir '@(ogx|ogv|oga|ogg|spx|anx|axv|axa)'
69}
70
71_oggz_ctype_cmd ()
72{
73    local cur="${COMP_WORDS[COMP_CWORD]}"
74    local prev="${COMP_WORDS[COMP_CWORD-1]}"
75
76    local command=$1
77
78    case "$prev" in
79    -c|--content-type)
80        __oggz_known_codecs $cur
81        return ;;
82    esac
83
84    case "$cur" in
85    -*)
86        __oggzcomp $($command -?)
87        return ;;
88    *)  _oggz_filedir
89        return ;;
90    esac
91}
92
93_oggz_basic_cmd ()
94{
95    local cur="${COMP_WORDS[COMP_CWORD]}"
96
97    local command=$1
98
99    case "$cur" in
100    -*)
101        __oggzcomp $($command -?)
102        return ;;
103    *)  _oggz_filedir
104        return ;;
105    esac
106}
107
108_oggz_chop ()
109{
110    _oggz_basic_cmd oggz-chop
111}
112
113_oggz_comment ()
114{
115    _oggz_ctype_cmd oggz-comment
116}
117
118_oggz_diff ()
119{
120    _oggz_ctype_cmd oggz-diff
121}
122
123_oggz_dump ()
124{
125    _oggz_ctype_cmd oggz-dump
126}
127
128_oggz_info ()
129{
130    _oggz_basic_cmd oggz-info
131}
132
133_oggz_known_codecs ()
134{
135    _oggz_basic_cmd oggz-known-codecs
136}
137
138_oggz_merge ()
139{
140    _oggz_basic_cmd oggz-merge
141}
142
143_oggz_rip ()
144{
145    _oggz_ctype_cmd oggz-rip
146}
147
148_oggz_scan ()
149{
150    _oggz_basic_cmd oggz-scan
151}
152
153_oggz_sort ()
154{
155    _oggz_basic_cmd oggz-sort
156}
157
158_oggz_validate ()
159{
160    _oggz_basic_cmd oggz-validate
161}
162
163_oggz_codecs ()
164{
165    _oggz_basic_cmd oggz-codecs
166}
167
168_oggz_help ()
169{
170    __oggz_commands
171}
172
173_oggz ()
174{
175    local cur="${COMP_WORDS[COMP_CWORD]}"
176    local prev="${COMP_WORDS[COMP_CWORD-1]}"
177    local i c=1 command
178
179    while [ $c -lt $COMP_CWORD ]; do
180        i="${COMP_WORDS[c]}"
181        case "$i" in
182        --version|--help) ;;
183        *) command="$i"; break ;;
184        esac
185        c=$((++c))
186    done
187
188    if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
189        case "${COMP_WORDS[COMP_CWORD]}" in
190        --*) COMPREPLY=() ;;
191        *) __oggz_commands ;;
192        esac
193        return
194    fi
195
196    case "$command" in
197    chop)	_oggz_chop ;;
198    comment)	_oggz_comment ;;
199    diff)	_oggz_diff ;;
200    dump)	_oggz_dump ;;
201    help)	_oggz_help ;;
202    info)	_oggz_info ;;
203    known-codecs)	_oggz_known-codecs ;;
204    merge)	_oggz_merge ;;
205    rip)	_oggz_rip ;;
206    scan)	_oggz_scan ;;
207    sort)	_oggz_sort ;;
208    validate)	_oggz_validate ;;
209    codecs)	_oggz_codecs ;;
210    *)		COMPREPLY=() ;;
211    esac
212}
213
214# Completion for wrapper oggz tool
215complete -o filenames -F _oggz oggz
216
217# Completions for commands
218complete -o filenames -F _oggz_chop oggz-chop
219complete -o filenames -F _oggz_comment oggz-comment
220complete -o filenames -F _oggz_diff oggz-diff
221complete -o filenames -F _oggz_dump oggz-dump
222complete -o filenames -F _oggz_info oggz-info
223complete -o default -F _oggz_known-codecs oggz-known-codecs
224complete -o filenames -F _oggz_merge oggz-merge
225complete -o filenames -F _oggz_rip oggz-rip
226complete -o filenames -F _oggz_scan oggz-scan
227complete -o filenames -F _oggz_sort oggz-sort
228complete -o filenames -F _oggz_validate oggz-validate
229complete -o filenames -F _oggz_codecs oggz-codecs
230