chiark / gitweb /
Add copyright notices to bash scripts
[fdroidserver.git] / completion / bash-completion
1 #!/bin/bash
2 #
3 # bash-completion - part of the FDroid server tools
4 # Commits updates to apps, allowing you to edit the commit messages
5 #
6 # Copyright (C) 2013 Daniel Martí <mvdan@mvdan.cc>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # 'fdroid' is aliased automatically, but aliases to it are not. For instance,
22 # to alias 'fd' to 'fdroid' and have competion available:
23 #
24 #       alias fd='fdroid'
25 #       complete -F _fdroid fd
26 #
27 # One can use completion on aliased subcommands as follows:
28 #
29 #       alias fbuild='fdroid build'
30 #       complete -F _fdroid_build fbuild
31
32 __fdroid_init() {
33         COMPREPLY=()
34         cur="${COMP_WORDS[COMP_CWORD]}"
35         prev="${COMP_WORDS[COMP_CWORD-1]}"
36
37         (( $# >= 1 )) && __complete_${1}
38 }
39
40 __package() {
41         files=( metadata/*.txt )
42         files=( ${files[@]#metadata/} )
43         files=${files[@]%.txt}
44         COMPREPLY=( $( compgen -W "$files" -- $cur ) )
45 }
46
47 __apk_package() {
48         files=( ${1}/*.apk )
49         [ -f "${files[0]}" ] || return
50
51         files=( ${files[@]#*/} )
52         files=${files[@]%_*}
53         COMPREPLY=( $( compgen -W "$files" -- $cur ) )
54 }
55
56 __apk_vercode() {
57         local p
58         p=${cur:0:-1}
59
60         files=( ${1}/${p}_*.apk )
61         [ -f "${files[0]}" ] || return
62
63         files=( ${files[@]#*_} )
64         files=${files[@]%.apk}
65         COMPREPLY=( $( compgen -P "${p}:" -W "$files" -- $cur ) )
66 }
67
68 __vercode() {
69         local p
70         p=${cur:0:-1}
71
72         COMPREPLY=( $( compgen -P "${p}:" -W "$( while read line; do
73                 if [[ "$line" == "Build Version:"* ]]
74                 then
75                         line="${line#*,}"
76                         printf "${line%%,*} "
77                 elif [[ "$line" == "Build:"* ]]
78                 then
79                         line="${line#*,}"
80                         printf "${line%%,*} "
81                 fi
82                 done < "metadata/${p}.txt" )" -- $cur ) )
83 }
84
85 __complete_options() {
86         case "${cur}" in
87                 --*)
88                         COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
89                         return 0;;
90                 *)
91                         COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
92                         return 0;;
93         esac
94 }
95
96 __complete_build() {
97         opts="-h -v -c -l -s -t -f"
98         lopts="--help --verbose --latest --server --resetserver --on-server
99  --force --all"
100         case "${cur}" in
101                 -*)
102                         __complete_options
103                         return 0;;
104                 *:)
105                         __vercode
106                         return 0;;
107                 *)
108                         __package
109                         return 0;;
110         esac
111 }
112
113 __complete_install() {
114         opts="-h -v"
115         lopts="--help --verbose --all"
116         case "${cur}" in
117                 -*)
118                         __complete_options
119                         return 0;;
120                 *:)
121                         __apk_vercode repo
122                         return 0;;
123                 *)
124                         __apk_package repo
125                         return 0;;
126         esac
127 }
128
129 __complete_update() {
130         opts="-h -c -v -q -b -i -I -e -w"
131         lopts="--help --createmeta --verbose --quiet --buildreport --interactive
132  --icons --editor --wiki --pretty --clean"
133         case "${prev}" in
134                 -e|--editor)
135                         _filedir
136                         return 0;;
137         esac
138         __complete_options
139 }
140
141 __complete_publish() {
142         opts="-h -v"
143         lopts="--help --verbose"
144         case "${cur}" in
145                 -*)
146                         __complete_options
147                         return 0;;
148                 *:)
149                         __apk_vercode unsigned
150                         return 0;;
151                 *)
152                         __apk_package unsigned
153                         return 0;;
154         esac
155 }
156
157 __complete_checkupdates() {
158         opts="-h -v"
159         lopts="--help --verbose --auto --autoonly --commit --gplay"
160         case "${cur}" in
161                 -*)
162                         __complete_options
163                         return 0;;
164                 *)
165                         __package
166                         return 0;;
167         esac
168 }
169
170 __complete_import() {
171         opts="-h -u -s -r"
172         lopts="--help --url --subdir --repo"
173         case "${prev}" in
174                 -u|--url|-r|--repo) return 0;;
175                 -s|--subdir)
176                         _filedir
177                         return 0;;
178         esac
179         __complete_options
180 }
181
182 __complete_rewritemeta() {
183         opts="-h -v"
184         lopts="--help --verbose"
185         case "${cur}" in
186                 -*)
187                         __complete_options
188                         return 0;;
189                 *)
190                         __package
191                         return 0;;
192         esac
193 }
194
195 __complete_scanner() {
196         opts="-h -v"
197         lopts="--help --verbose --nosvn"
198         case "${cur}" in
199                 -*)
200                         __complete_options
201                         return 0;;
202                 *:)
203                         __vercode
204                         return 0;;
205                 *)
206                         __package
207                         return 0;;
208         esac
209 }
210
211 __complete_verify() {
212         opts="-h -v -p"
213         lopts="--help --verbose"
214         case "${cur}" in
215                 -*)
216                         __complete_options
217                         return 0;;
218                 *:)
219                         __vercode
220                         return 0;;
221                 *)
222                         __package
223                         return 0;;
224         esac
225 }
226
227 __complete_stats() {
228         opts="-h -v -d"
229         lopts="--help --verbose --download"
230         __complete_options
231 }
232
233 __complete_server() {
234         opts="-h -v"
235         lopts="--help --verbose update"
236         __complete_options
237 }
238
239 __complete_init() {
240         opts="-h -v -d"
241         lopts="--help --verbose --keystore --distinguished-name --repo-keyalias"
242         __complete_options
243 }
244
245 _fdroid() {
246         local cmd cmds aliased
247         cmd=${COMP_WORDS[1]}
248         cmds=" build init install update publish checkupdates import
249  rewritemeta scanner verify stats server "
250         aliased=false
251
252         for c in $cmds; do eval "_fdroid_${c} () {
253                 local cur prev cmds opts lopts
254                 __fdroid_init ${c};
255         }"; done
256
257         [[ $cmds == *\ $cmd\ * ]] && _fdroid_${cmd} || {
258                 (($COMP_CWORD == 1)) && COMPREPLY=( $( compgen -W "${cmds}" -- $cmd ) )
259         }
260 }
261
262 _fd-commit() {
263         __package
264 }
265
266 complete -F _fdroid fdroid
267 complete -F _fd-commit fd-commit
268
269 return 0