Bug 10896: Add a --verbose option to koha-translate
[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
21 set -e
22
23 usage()
24 {
25     local scriptname=$(basename $0)
26
27     cat <<EOF
28 $scriptname
29
30 This script lets you manage your Koha templates translations.
31
32 Usage:
33 $scriptname --list|-l [--available|-a]
34 $scriptname --check|-c language_code
35 $scriptname --install|-i language_code
36 $scriptname --update|-u language_code
37 $scriptname --remove|-r language_code
38 $scriptname --help|-h
39
40     -l | --list           List the installed or available (combined with -a)
41                           language translations
42     -a | --available      Used in conjunction with -l to show all languages
43     -c | --check          Check that the language .PO files are present
44     -i | --install        Install the specified language translations
45     -u | --update         Update the specified language translations
46     -r | --remove         Remove the specified language translations
47     -v | --verbose        Be more verbose on the translation process
48     -h | --help           Display this help message
49
50 EOF
51 }
52
53 die()
54 {
55     echo "$@" 1>&2
56     exit 1
57 }
58
59 list()
60 {
61     all=$1
62
63     if [ "$all" != "" ]; then
64         print_available
65     else
66         print_installed
67     fi
68 }
69
70 print_available()
71 {
72     # Loop over only one opac theme
73     for i in $( ls $PO_DIR | grep opac-t-prog ); do
74         echo `basename $i -i-opac-t-prog-v-3006000.po`
75     done
76 }
77
78 print_installed()
79 {
80     ls $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/prog/ | \
81         grep -v -e images -e itemtypeimg
82 }
83
84 install_lang()
85 {
86     local lang=$1
87     local translate_opts=""
88
89     if [ "$verbose" = "yes" ]; then
90         translate_opts="--verbose"
91     fi
92
93     if [ "$lang" != "" ]; then
94
95         if [ "$lang" = "en" ]; then
96             die "Error: the default language (en) is already installed."
97         fi
98
99         if print_available | grep -q $lang; then
100             if print_installed | grep -q $lang; then
101                 die "Error: the selected language is already installed. Try --update if you want to re-install it."
102             else
103                 # Check po files are present
104                 check_lang_po_files $lang
105                 env PERL5LIB="$KOHA_LIB_DIR:$TRANSLATE_DIR" KOHA_CONF="$KOHA_CONF_FILE"\
106                     $PERL_CMD $TRANSLATE_DIR/translate install $translate_opts $lang
107             fi
108         else
109             die "Error: the selected language is not currently available."
110         fi
111
112     else
113         die "Error: no language code supplied."
114     fi
115 }
116
117 update_lang()
118 {
119     lang=$1
120
121     if [ "$lang" != "" ]; then
122
123         if [ "$lang" = "en" ]; then
124             die "Error: the default language (en) cannot be updated."
125         fi
126
127         if print_installed | grep -q $lang; then
128             # Check po files are present
129             check_lang_po_files $lang
130             remove_lang $lang
131             install_lang $lang
132         else
133             die "Error: the selected language is not currently installed. Try --install."
134         fi
135     else
136         die "Error: no language code supplied."
137     fi
138 }
139
140 remove_lang()
141 {
142     lang=$1
143
144     if [ "$lang" != "" ]; then
145
146         if [ "$lang" = "en" ]; then
147             die "Error: the default language (en) cannot be removed."
148         fi
149
150         if print_installed | grep -q $lang; then
151             rm -rf $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/prog/$lang
152             rm -rf $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/ccsr/$lang
153             rm -rf $KOHA_INSTALL_DIR/intranet/htdocs/intranet-tmpl/prog/$lang
154         else
155             die "Error: the selected language is not already installed."
156         fi
157     else
158         die "Error: no language code supplied."
159     fi
160 }
161
162 check_lang_po_files()
163 {
164     lang=$1
165
166     po_files="$PO_DIR/$lang-i-opac-t-prog-v-3006000.po
167               $PO_DIR/$lang-opac-ccsr.po
168               $PO_DIR/$lang-i-staff-t-prog-v-3006000.po
169               $PO_DIR/$lang-pref.po"
170
171     if [ "$lang" != "" ]; then
172
173         for po_file in $po_files; do
174             if [ ! -f $po_file ]; then
175                 die "Error: $po_file not found."
176             fi
177         done
178     else
179         die "Error: no language code supplied."
180     fi
181 }
182
183 set_action()
184 {
185     if [ "$op" = "" ]; then
186         op=$1
187     else
188         die "Error: only one action can be specified."
189     fi
190 }
191
192 # Global PATH variables
193 KOHA_INSTALL_DIR="/usr/share/koha"
194 KOHA_LIB_DIR="/usr/share/koha/lib"
195 KOHA_CONF_FILE="/etc/koha/koha-conf-site.xml.in"
196 TRANSLATE_DIR="$KOHA_INSTALL_DIR/misc/translator"
197 PO_DIR="$TRANSLATE_DIR/po"
198 PERL_CMD=`which perl`
199
200 # Control variables
201 list_all=""
202 op=""
203 language=""
204 verbose="no"
205
206 # We accept at most 2 parameters
207 [ $# -ge 1 ] && [ $# -le 4 ] || ( usage ; die "Error: wrong parameters" )
208
209 # Read parameters
210 while [ $# -gt 0 ]; do
211
212     case "$1" in
213         -h|--help)
214             op="help"
215             break ;;
216         -c|--check)
217             set_action "check"
218             shift ;;
219         -i|--install)
220             set_action "install"
221             shift ;;
222         -u|--update)
223             set_action "update"
224             shift ;;
225         -r|--remove)
226             set_action "remove"
227             shift ;;
228         -l|--list)
229             set_action "list"
230             shift ;;
231         -a|--available)
232             list_all=1
233             shift ;;
234         -v|--verbose)
235             verbose="yes"
236             shift ;;
237         -*)
238             usage
239             die "Error: unknown parameter $1." ;;
240         *)
241             language=$1
242             shift ;;
243     esac
244
245 done
246
247 # Process the requested actions
248 case $op in
249     "help")
250         usage ;;
251     "list")
252         list $list_all ;;
253     "install")
254         install_lang $language ;;
255     "update")
256         update_lang $language ;;
257     "remove")
258         remove_lang $language ;;
259     "check")
260         check_lang_po_files $language ;;
261     *)
262         usage
263         die "Error: wrong parameters..." ;;
264 esac
265
266 exit 0