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