Bug 5786 - Move AllowOnShelfHolds and OPACItemHolds system prefs to the Circulation...
[koha.git] / debian / koha-common.bash-completion
1 #!/bin/bash
2 #
3 # koha-common.bash-completion script for koha-* commands
4 #
5 # This file is part of Koha.
6 #
7 # Copyright 2013 Universidad Nacional de Cordoba
8 #                Tomas Cohen Arazi
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 3 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 _build_substract_switches()
23 {
24     local substract
25
26     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
27         if [[ ${COMP_WORDS[i]} == -* ]]; then
28             substract="$substract -e ${COMP_WORDS[i]}"
29         fi
30     done
31
32     echo "$substract"
33 }
34
35 _build_substract_instances()
36 {
37     local substract
38
39     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
40         substract="$substract -e ${COMP_WORDS[i]}"
41     done
42
43     echo "$substract"
44 }
45
46 _koha_list_cmd()
47 {
48     local filter=$1
49
50     local cur substract instancelist
51     _get_comp_words_by_ref cur
52
53     # Build a list of the already used words
54     substract=`_build_substract_instances`
55
56     if [[ "$substract" != "" ]]; then
57         instancelist=$( koha-list $filter | grep -v -x $substract )
58     else
59         instancelist=$( koha-list $filer )
60     fi
61
62     COMPREPLY=( $(compgen -W "$instancelist" -- $cur ) )
63 }
64
65 _koha_email_disable()
66 {
67     _koha_list_cmd "--email"
68     return 0
69 }
70 complete -F _koha_email_disable koha-email-disable
71
72 _koha_email_enable()
73 {
74     _koha_list_cmd "--noemail"
75     return 0
76 }
77 complete -F _koha_email_enable koha-email-enable
78
79 _koha_sip_enabled_instances()
80 {
81     _koha_list_cmd "--sip"
82     return 0
83 }
84
85 # koha-*-sip autocomplete with sip-enabled instances
86 complete -F _koha_sip_enabled_instances koha-start-sip
87 complete -F _koha_sip_enabled_instances koha-restart-sip
88 complete -F _koha_sip_enabled_instances koha-stop-sip
89
90 _koha_sip_disabled()
91 {
92     _koha_list_cmd "--nosip"
93     return 0
94 }
95
96 # koha-enable-sip autocompletes with sip-disabled instances
97 complete -F _koha_sip_disabled koha-enable-sip
98
99 _koha_disabled_instances()
100 {
101     _koha_list_cmd "--disabled"
102     return 0
103 }
104
105 _koha_enabled_instances()
106 {
107     _koha_list_cmd "--enabled"
108     return 0
109 }
110
111 # koha-enable autocompletes with disabled instances
112 complete -F _koha_disabled_instances koha-enable
113
114 # koha-disable autocompletes with enabled instances
115 complete -F _koha_enabled_instances koha-disable
116
117 # koha-*-zebra autocomplete with enabled instances
118 complete -F _koha_enabled_instances koha-start-zebra
119 complete -F _koha_enabled_instances koha-restart-zebra
120 complete -F _koha_enabled_instances koha-stop-zebra
121
122 _koha_list()
123 {
124     local cur opts substract
125
126     COMPREPLY=()
127     _get_comp_words_by_ref cur
128     opts="--enabled --disabled --email --noemail --sip --nosip --help -h"
129
130     # Build a list of the already used option switches
131     for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ )); do
132         if [[ ${COMP_WORDS[i]} == -* ]]; then
133             case ${COMP_WORDS[i]} in
134                 --disabled)
135                     substract="$substract -e --enabled"; ;;
136                 --enabled)
137                     substract="$substract -e --disabled"; ;;
138                 --email)
139                     substract="$substract -e --noemail"; ;;
140                 --noemail)
141                     substract="$substract -e --email"; ;;
142                 --sip)
143                     substract="$substract -e --nosip"; ;;
144                 --nosip)
145                     substract="$substract -e --sip"; ;;
146                 --help)
147                     substract="$substract -e -h"; ;;
148                 -h)
149                     substract="$substract -e --help"; ;;
150             esac
151             substract="$substract -e ${COMP_WORDS[i]}"
152         fi
153     done
154
155     if [[ "$substract" != "" ]]; then
156         opts=$( echo $opts | sed -e 's/ /\n/g'  | grep -v -x $substract )
157     fi
158
159     COMPREPLY=( $(compgen -W "$opts" -- $cur ) )
160
161     return 0
162 }
163 complete -F _koha_list koha-list
164
165 # Local variables:
166 # mode: shell-script
167 # sh-basic-offset: 4
168 # sh-indent-comment: t
169 # indent-tabs-mode: nil
170 # End:
171 # ex: ts=4 sw=4 et filetype=sh