Bug 30511: Don't lock up entire database while running koha-dump
[koha.git] / debian / scripts / koha-translate
1 #!/bin/sh
2 #
3 # koha-translate -- Manage Koha translations.
4 # Copyright 2013 Tomás Cohen Arazi
5 #                Universidad Nacional de Córdoba
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # Read configuration variable file if it is present
21 [ -r /etc/default/koha-common ] && . /etc/default/koha-common
22
23 set -e
24
25 # include helper functions
26 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
27     . "/usr/share/koha/bin/koha-functions.sh"
28 else
29     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
30     exit 1
31 fi
32
33 usage()
34 {
35     local scriptname=$(basename $0)
36
37     cat <<EOF
38 $scriptname
39
40 This script lets you manage your Koha templates translations.
41
42 Usage:
43 $scriptname --list|-l [--available|-a]  [-d|--dev instance]
44 $scriptname --check|-c language_code]   [-d|--dev instance]
45 $scriptname --install|-i language_code] [-d|--dev instance]
46 $scriptname --update|-u language_code]  [-d|--dev instance]
47 $scriptname --remove|-r language_code]  [-d|--dev instance]
48 $scriptname --help|-h
49
50     -l | --list           List the installed or available (combined with -a)
51                           language translations
52     -a | --available      Used in conjunction with -l to show all languages
53     -c | --check          Check that the language .PO files are present
54     -i | --install        Install the specified language translations
55     -u | --update         Update the specified language translations
56     -r | --remove         Remove the specified language translations
57     -v | --verbose        Be more verbose on the translation process
58     -h | --help           Display this help message
59     -d | --dev            Limit actions to a specific dev instance
60
61 EOF
62 }
63
64 list()
65 {
66     all=$1
67
68     if [ "$all" != "" ]; then
69         print_available
70     else
71         print_installed
72     fi
73 }
74
75 print_available()
76 {
77     # Loop over only one opac theme
78     for i in $( ls $PO_DIR | grep opac-bootstrap ); do
79         echo `basename $i -opac-bootstrap.po` | \
80             grep -v -x -e en
81     done
82 }
83
84 print_installed()
85 {
86     ( ls -1 $KOHA_HOME/$OPAC_TMPL/bootstrap/ ; \
87         ls -1 $KOHA_HOME/$OPAC_TMPL/prog/ 2> /dev/null ) | \
88         sort | uniq | \
89         grep -v -e images -e itemtypeimg -x -e en -e css -e js -e less -e lib
90 }
91
92 install_lang()
93 {
94     local lang=$1
95     local translate_opts=""
96
97     if [ "$verbose" = "yes" ]; then
98         translate_opts="--verbose"
99     fi
100
101     if [ "$lang" != "" ]; then
102
103         if [ "$lang" = "en" ]; then
104             die "Error: the default language (en) is already installed."
105         fi
106
107         if print_available | grep -q $lang; then
108             if print_installed | grep -q $lang; then
109                 die "Error: the selected language is already installed. Try --update if you want to re-install it."
110             else
111                 # Check po files are present
112                 check_lang_po_files $lang
113                 env PERL5LIB="$PERL5LIB:$TRANSLATE_DIR" \
114                     KOHA_CONF="$KOHA_CONF" \
115                     $PERL_CMD $TRANSLATE_DIR/translate install $translate_opts $lang
116             fi
117         else
118             die "Error: the selected language is not currently available."
119         fi
120
121     else
122         die "Error: no language code supplied."
123     fi
124 }
125
126 update_lang()
127 {
128     lang=$1
129
130     if [ "$lang" != "" ]; then
131
132         if [ "$lang" = "en" ]; then
133             die "Error: the default language (en) cannot be updated."
134         fi
135
136         if print_installed | grep -q $lang; then
137             # Check po files are present
138             check_lang_po_files $lang
139             remove_lang $lang
140             install_lang $lang
141         else
142             die "Error: the selected language is not currently installed. Try --install."
143         fi
144     else
145         die "Error: no language code supplied."
146     fi
147 }
148
149 remove_lang()
150 {
151     lang=$1
152
153     if [ "$lang" != "" ]; then
154
155         if [ "$lang" = "en" ]; then
156             die "Error: the default language (en) cannot be removed."
157         fi
158
159         if print_installed | grep -q $lang; then
160             rm -rf $KOHA_HOME/$OPAC_TMPL/bootstrap/$lang
161             rm -rf $KOHA_HOME/$INTRANET_TMPL/prog/$lang
162         else
163             die "Error: the selected language is not installed."
164         fi
165     else
166         die "Error: no language code supplied."
167     fi
168 }
169
170 check_lang_po_files()
171 {
172     lang=$1
173
174     po_files="$PO_DIR/$lang-marc-MARC21.po
175               $PO_DIR/$lang-marc-UNIMARC.po
176               $PO_DIR/$lang-opac-bootstrap.po
177               $PO_DIR/$lang-pref.po
178               $PO_DIR/$lang-staff-prog.po"
179
180     if [ "$lang" != "" ]; then
181
182         for po_file in $po_files; do
183             if [ ! -f $po_file ]; then
184                 die "Error: $po_file not found."
185             fi
186         done
187     else
188         die "Error: no language code supplied."
189     fi
190 }
191
192 set_action()
193 {
194     if [ "$op" = "" ]; then
195         op=$1
196     else
197         die "Error: only one action can be specified."
198     fi
199 }
200
201 set_dev()
202 {
203     if is_instance $1; then
204         dev=$1
205     else
206         die "Error: Invalid instance name $1"
207     fi
208 }
209
210 check_koha_conf()
211 {
212     if [ "$dev" != "" ]; then
213         KOHA_CONF=/etc/koha/sites/$dev/koha-conf.xml
214     elif [ -z $KOHA_CONF ]; then
215         KOHA_CONF=/etc/koha/koha-conf-site.xml.in
216     fi
217 }
218
219 init_template_paths()
220 {
221     # Template paths
222     if [ "$dev" = "" ]; then
223         OPAC_TMPL=opac/htdocs/opac-tmpl
224         INTRANET_TMPL=intranet/htdocs/intranet-tmpl
225     else
226         OPAC_TMPL=koha-tmpl/opac-tmpl
227         INTRANET_TMPL=koha-tmpl/intranet-tmpl
228     fi
229     TRANSLATE_DIR="$KOHA_HOME/misc/translator"
230     PO_DIR="$TRANSLATE_DIR/po"
231 }
232
233 flush_cache()
234 {
235     if [ "$dev" = "" ]; then
236         koha-foreach --enabled "$KOHA_HOME/bin/clear_cache.pl"
237     else
238         koha-shell $dev -c "$KOHA_HOME/misc/bin/clear_cache.pl"
239     fi
240 }
241
242 # Control variables
243 list_all=""
244 op=""
245 language=""
246 verbose="no"
247 dev=""
248
249 # We accept at most 4 parameters
250 [ $# -ge 1 ] && [ $# -le 4 ] || ( usage ; die "Error: wrong parameters" )
251
252 # Read parameters
253 while [ $# -gt 0 ]; do
254
255     case "$1" in
256         -h|--help)
257             op="help"
258             break ;;
259         -c|--check)
260             set_action "check"
261             shift ;;
262         -i|--install)
263             set_action "install"
264             shift ;;
265         -u|--update)
266             set_action "update"
267             shift ;;
268         -r|--remove)
269             set_action "remove"
270             shift ;;
271         -l|--list)
272             set_action "list"
273             shift ;;
274         -a|--available)
275             list_all=1
276             shift ;;
277         -v|--verbose)
278             verbose="yes"
279             shift ;;
280         -d|--dev)
281             if [ $# -lt 2 ]; then
282                 die "Error: dev parameter without instance"
283             fi
284             shift
285             set_dev $1
286             shift ;;
287         -*)
288             usage
289             die "Error: unknown parameter $1." ;;
290         *)
291             language=$1
292             shift ;;
293     esac
294
295 done
296
297 if [ "$dev" != "" ]; then adjust_paths_dev_install $dev; fi
298 check_koha_conf
299 init_template_paths
300 PERL_CMD=`which perl`
301
302 # Process the requested actions
303 case $op in
304     "help")
305         usage ;;
306     "list")
307         list $list_all ;;
308     "install")
309         install_lang $language
310         flush_cache
311         ;;
312     "update")
313         update_lang $language ;;
314     "remove")
315         remove_lang $language
316         flush_cache
317         ;;
318     "check")
319         check_lang_po_files $language ;;
320     *)
321         usage
322         die "Error: wrong parameters..." ;;
323 esac
324
325 exit 0