Bug 11655: koha-translate --list no longer shows 'en'
[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             grep -v -x -e en
76     done
77 }
78
79 print_installed()
80 {
81     ls $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/prog/ | \
82         grep -v -e images -e itemtypeimg -x -e en
83 }
84
85 install_lang()
86 {
87     local lang=$1
88     local translate_opts=""
89
90     if [ "$verbose" = "yes" ]; then
91         translate_opts="--verbose"
92     fi
93
94     if [ "$lang" != "" ]; then
95
96         if [ "$lang" = "en" ]; then
97             die "Error: the default language (en) is already installed."
98         fi
99
100         if print_available | grep -q $lang; then
101             if print_installed | grep -q $lang; then
102                 die "Error: the selected language is already installed. Try --update if you want to re-install it."
103             else
104                 # Check po files are present
105                 check_lang_po_files $lang
106                 env PERL5LIB="$KOHA_LIB_DIR:$TRANSLATE_DIR" KOHA_CONF="$KOHA_CONF_FILE"\
107                     $PERL_CMD $TRANSLATE_DIR/translate install $translate_opts $lang
108             fi
109         else
110             die "Error: the selected language is not currently available."
111         fi
112
113     else
114         die "Error: no language code supplied."
115     fi
116 }
117
118 update_lang()
119 {
120     lang=$1
121
122     if [ "$lang" != "" ]; then
123
124         if [ "$lang" = "en" ]; then
125             die "Error: the default language (en) cannot be updated."
126         fi
127
128         if print_installed | grep -q $lang; then
129             # Check po files are present
130             check_lang_po_files $lang
131             remove_lang $lang
132             install_lang $lang
133         else
134             die "Error: the selected language is not currently installed. Try --install."
135         fi
136     else
137         die "Error: no language code supplied."
138     fi
139 }
140
141 remove_lang()
142 {
143     lang=$1
144
145     if [ "$lang" != "" ]; then
146
147         if [ "$lang" = "en" ]; then
148             die "Error: the default language (en) cannot be removed."
149         fi
150
151         if print_installed | grep -q $lang; then
152             rm -rf $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/prog/$lang
153             rm -rf $KOHA_INSTALL_DIR/opac/htdocs/opac-tmpl/ccsr/$lang
154             rm -rf $KOHA_INSTALL_DIR/intranet/htdocs/intranet-tmpl/prog/$lang
155         else
156             die "Error: the selected language is not already installed."
157         fi
158     else
159         die "Error: no language code supplied."
160     fi
161 }
162
163 check_lang_po_files()
164 {
165     lang=$1
166
167     po_files="$PO_DIR/$lang-i-opac-t-prog-v-3006000.po
168               $PO_DIR/$lang-opac-ccsr.po
169               $PO_DIR/$lang-i-staff-t-prog-v-3006000.po
170               $PO_DIR/$lang-pref.po"
171
172     if [ "$lang" != "" ]; then
173
174         for po_file in $po_files; do
175             if [ ! -f $po_file ]; then
176                 die "Error: $po_file not found."
177             fi
178         done
179     else
180         die "Error: no language code supplied."
181     fi
182 }
183
184 set_action()
185 {
186     if [ "$op" = "" ]; then
187         op=$1
188     else
189         die "Error: only one action can be specified."
190     fi
191 }
192
193 # Global PATH variables
194 KOHA_INSTALL_DIR="/usr/share/koha"
195 KOHA_LIB_DIR="/usr/share/koha/lib"
196 KOHA_CONF_FILE="/etc/koha/koha-conf-site.xml.in"
197 TRANSLATE_DIR="$KOHA_INSTALL_DIR/misc/translator"
198 PO_DIR="$TRANSLATE_DIR/po"
199 PERL_CMD=`which perl`
200
201 # Control variables
202 list_all=""
203 op=""
204 language=""
205 verbose="no"
206
207 # We accept at most 2 parameters
208 [ $# -ge 1 ] && [ $# -le 4 ] || ( usage ; die "Error: wrong parameters" )
209
210 # Read parameters
211 while [ $# -gt 0 ]; do
212
213     case "$1" in
214         -h|--help)
215             op="help"
216             break ;;
217         -c|--check)
218             set_action "check"
219             shift ;;
220         -i|--install)
221             set_action "install"
222             shift ;;
223         -u|--update)
224             set_action "update"
225             shift ;;
226         -r|--remove)
227             set_action "remove"
228             shift ;;
229         -l|--list)
230             set_action "list"
231             shift ;;
232         -a|--available)
233             list_all=1
234             shift ;;
235         -v|--verbose)
236             verbose="yes"
237             shift ;;
238         -*)
239             usage
240             die "Error: unknown parameter $1." ;;
241         *)
242             language=$1
243             shift ;;
244     esac
245
246 done
247
248 # Process the requested actions
249 case $op in
250     "help")
251         usage ;;
252     "list")
253         list $list_all ;;
254     "install")
255         install_lang $language ;;
256     "update")
257         update_lang $language ;;
258     "remove")
259         remove_lang $language ;;
260     "check")
261         check_lang_po_files $language ;;
262     *)
263         usage
264         die "Error: wrong parameters..." ;;
265 esac
266
267 exit 0