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