]> TLD Linux GIT Repositories - packages/rpm-tld-macros.git/blob - find-lang.sh
fa120e8c695a52dad703b9354f8b6b37b03d6830
[packages/rpm-tld-macros.git] / find-lang.sh
1 #!/bin/sh
2
3 # find-lang - automagically generate list of language specific files
4 # for inclusion in an rpm spec file.
5 # This does assume that the *.mo files are under .../share/locale/...
6 # Run with no arguments gets a usage message.
7
8 # findlang is copyright (c) 1998 by W. L. Estes <wlestes@uncg.edu>
9
10 # Redistribution and use of this software are hereby permitted for any
11 # purpose as long as this notice and the above copyright notice remain
12 # in tact and are included with any redistribution of this file or any
13 # work based on this file.
14
15 # Changes:
16 #
17 # 2012-12-22 Elan Ruusamäe <glen@pld-linux.org>
18 #   * added --with-mate
19 # 2006-08-28 Elan Ruusamäe <glen@pld-linux.org>
20 #   * fixed --all-name which got broken with last change.
21 # 2006-08-09 Elan Ruusamäe <glen@pld-linux.org>
22 #   * huge performance boost for packages calling %find_lang multiple times (kde*i18n)
23 # 2001-01-08 Michał Kochanowicz <mkochano@pld.org.pl>
24 #   * --all-name support for KDE.
25 # 2000-11-28 Rafał Cygnarowski <pascalek@pld.org.pl>
26 #   * next simple rule for KDE
27 # 2000-11-12 Rafał Cygnarowski <pascalek@pld.org.pl>
28 #   * simple rules for KDE help files
29 # 2000-06-05 Michał Kochanowicz <mkochano@pld.org.pl>
30 #   * exact, not substring matching $NAME, i.e. find-lang top_dir NAME will
31 #     no longer find /usr/share/locale/pl/LC_MESSAGES/<anything>NAME.mo.
32 # 2000-04-17 Arkadiusz Miśkiewicz <misiek@pld.org.pl>
33 #   * exit 1 when no files found
34 # 1999-10-19 Artur Frysiak <wiget@pld.org.pl>
35 #   * added support for GNOME help files
36 #   * start support for KDE help files
37
38 PROG=${0##*/}
39 VERSION=1.41
40
41 usage () {
42 cat <<EOF
43 Usage: $PROG TOP_DIR PACKAGE_NAME [prefix]
44
45 where TOP_DIR is
46 the top of the tree containing the files to be processed--should be
47 \$RPM_BUILD_ROOT usually. TOP_DIR gets sed'd out of the output list.
48 PACKAGE_NAME is the %{name} of the package. This should also be
49 the basename of the .mo files.  the output is written to
50 PACKAGE_NAME.lang unless \$3 is given in which case output is written
51 to \$3.
52 Additional options:
53   --with-gnome          find GNOME help files
54   --with-mate           find MATE help files
55   --with-kde            find KDE help files
56   --with-omf            find OMF files
57   --with-qm                     find QT .qm files
58   --with-django         find translations in Django project
59   --with-dokuwiki       find translations in dokuwiki plugins/templates
60   --all-name            match all package/domain names
61   --without-mo          skip *.mo locale files
62   -o NAME                       output will be saved to NAME
63   -a NAME                       output will be appended to NAME
64 EOF
65 exit 1
66 }
67
68 if [ -z "$1" ]; then
69         usage
70 elif [ $1 = / ]; then
71         echo >&2 "$PROG: expects non-/ argument for '$1'"
72         exit 1
73 elif [ ! -d $1 ]; then
74         echo >&2 "$PROG: $1: No such directory"
75         exit 1
76 else
77         TOP_DIR="${1%/}"
78 fi
79 shift
80
81 if [ -z "$1" ]; then
82         usage
83 else
84         NAME=$1
85 fi
86 shift
87
88 GNOME='#'
89 MATE='#'
90 KDE='#'
91 OMF='#'
92 QM='#'
93 DJANGO='#'
94 DOKUWIKI=false
95 MO=''
96 OUTPUT=$NAME.lang
97 ALL_NAME='#'
98 NO_ALL_NAME=''
99 APPEND=''
100 while test $# -gt 0; do
101     case "$1" in
102         --with-dokuwiki)
103                 DOKUWIKI=true
104                 echo >&2 "$PROG: Enabling with Dokuwiki"
105                 shift
106                 ;;
107         --with-gnome)
108                 GNOME=''
109                 echo >&2 "$PROG: Enabling with GNOME"
110                 shift
111                 ;;
112         --with-mate)
113                 MATE=''
114                 echo >&2 "$PROG: Enabling with MATE"
115                 shift
116                 ;;
117         --with-kde)
118                 echo >&2 "$PROG: Enabling with KDE"
119                 KDE=''
120                 shift
121                 ;;
122         --with-omf)
123                 echo >&2 "$PROG: Enabling with OMF"
124                 OMF=''
125                 shift
126                 ;;
127         --with-qm)
128                 echo >&2 "$PROG: Enabling with Qt QM"
129                 QM=''
130                 shift
131                 ;;
132         --with-django)
133                 echo >&2 "$PROG: Enabling with Django"
134                 DJANGO=''
135                 shift
136                 ;;
137         --without-mo)
138                 echo >&2 "$PROG: Disabling .mo files"
139                 MO='#'
140                 shift
141                 ;;
142         --all-name)
143                 echo >&2 "$PROG: Enabling with all names"
144                 ALL_NAME=''
145                 NO_ALL_NAME='#'
146                 shift
147                 ;;
148         -o)
149                 shift
150                 OUTPUT=$1
151                 shift
152                 ;;
153         -a)
154                 shift
155                 OUTPUT=$1
156                 APPEND='>'
157                 shift
158                 ;;
159         *)
160                 OUTPUT=$1
161                 shift
162                 ;;
163     esac
164 done
165
166 if $DOKUWIKI; then
167         exec /usr/lib/rpm/dokuwiki-find-lang.sh "$TOP_DIR" "$NAME"
168         echo >&2 "$PROG: ERROR: Unable to execute dokuwiki-find-lang"
169         exit 2
170 fi
171
172 echo >&2 "$PROG/$VERSION: find-lang '$NAME' $APPEND> $OUTPUT"
173
174 MO_NAME=.$OUTPUT.tmp~
175 echo '%defattr(644,root,root,755)' > $MO_NAME
176
177 # .mo
178 if [ ! -f __find.files ] || [ "$TOP_DIR" -nt __find.files ]; then
179         find $TOP_DIR -xtype f -name '*.mo' | xargs -r file -L | \
180         sed -e '
181                 /, 1 message/d
182                 s/:.*//
183                 s:'"$TOP_DIR"'::' > __find.files
184 else
185         echo >&2 "$PROG: Using cached __find.files"
186 fi
187
188 # .omf
189 if [ ! -f __omf.files ] || [ "$TOP_DIR" -nt __omf.files ]; then
190         find $TOP_DIR -type f -name '*.omf' | \
191         sed -e '
192                 s:'"$TOP_DIR"'::' > __omf.files
193 else
194         echo >&2 "$PROG: Using cached __omf.files"
195 fi
196
197 # .qm
198 if [ ! -f __qm.files ] || [ "$TOP_DIR" -nt __qm.files ]; then
199         find $TOP_DIR -type f -name '*.qm' | \
200         sed -e '
201                 s:'"$TOP_DIR"'::' > __qm.files
202 else
203         echo >&2 "$PROG: Using cached __qm.files"
204 fi
205
206 # .mo
207 (
208         if [ "$ALL_NAME" ]; then
209                 grep -F $NAME __find.files
210         else
211                 cat __find.files
212         fi
213 ) | sed '
214 '"$ALL_NAME$MO"'s:\(.*/share/locale/\)\([^/@]\+\)\(@quot\|@boldquot\)\?\(@[^/]*\)\?\(/.*\.mo$\):%lang(\2\4) \1\2\3\4\5:
215 '"$NO_ALL_NAME$MO"'s:\(.*/share/locale/\)\([^/@]\+\)\(@quot\|@boldquot\)\?\(@[^/]*\)\?\(/.*/'"$NAME"'\.mo$\):%lang(\2\4) \1\2\3\4\5:
216 /^[^%]/d
217 s:%lang(C) ::' >> $MO_NAME
218
219 # .omf
220 (
221         if [ "$ALL_NAME" ]; then
222                 grep -F $NAME __omf.files
223         else
224                 cat __omf.files
225         fi
226 ) | sed '
227 '"$ALL_NAME$OMF"'s:\(.*/share/omf/[^/]\+/\)\(.*-\)\([^-]*\)\(\.omf\):%lang(\3) \1\2\3\4:
228 '"$NO_ALL_NAME$OMF"'s:\(.*/share/omf/'"$NAME"'/\)\(.*-\)\([^-]*\)\(\.omf\):%lang(\3) \1\2\3\4:
229 /^[^%]/d
230 s:%lang(C) ::' >> $MO_NAME
231
232 # .qm
233 (
234         if [ "$ALL_NAME" ]; then
235                 grep -F $NAME __qm.files
236         else
237                 cat __qm.files
238         fi
239 ) | sed '
240 '"$NO_ALL_NAME$QM"'s:\(.*/'"$NAME"'_\([a-zA-Z]\{2\}\([_@].*\)\?\)\.qm$\):%lang(\2) \1:
241 '"$NO_ALL_NAME$QM"'s:\(.*/share/locale/\)\([^/@]\+\)\(@quot\|@boldquot\)\?\(@[^/]*\)\?\(/.*/'"$NAME"'\.qm$\):%lang(\2\4) \1\2\3\4\5:
242 '"$ALL_NAME$QM"'s:\(.*/[^/_]\+_\([a-zA-Z]\{2\}[_@].*\)\.qm$\):%lang(\2) \1:
243 '"$ALL_NAME$QM"'s:\(.*/[^/_]\+_\([a-zA-Z]\{2\}\)\.qm$\):%lang(\2) \1:
244 '"$ALL_NAME$QM"'s:^\([^%].*/[^/]\+_\([a-zA-Z]\{2\}[_@].*\)\.qm$\):%lang(\2) \1:
245 '"$ALL_NAME$QM"'s:^\([^%].*/[^/]\+_\([a-zA-Z]\{2\}\)\.qm$\):%lang(\2) \1:
246 s:^[^%].*::
247 /^[^%]/d
248 s:%lang(C) ::' >> $MO_NAME
249
250 if [ ! -f __find.dirs ] || [ "$TOP_DIR" -nt __find.dirs ]; then
251         find $TOP_DIR -mindepth 1 -type d | sed 's:'"$TOP_DIR"'::' > __find.dirs
252 else
253         echo >&2 "$PROG: Using cached __find.dirs"
254 fi
255
256 # gnome
257 (
258         if [ "$ALL_NAME" ]; then
259                 grep -F $NAME __find.dirs
260         else
261                 cat __find.dirs
262         fi
263 ) | sed '
264 '"$NO_ALL_NAME$GNOME"'s:\(.*/share/help/\)\([^/]\+\)\(/'"$NAME"'\)$:%lang(\2) \1\2\3:
265 '"$NO_ALL_NAME$GNOME"'s:\(.*/gnome/help/'"$NAME"'$\):%dir \1:
266 '"$NO_ALL_NAME$GNOME"'s:\(.*/gnome/help/'"$NAME"'/\)\([^/]\+\)$:%lang(\2) \1\2:
267 '"$ALL_NAME$GNOME"'s:\(.*/share/help/\)\([^/]\+\)\(/[^/]\+\)$:%lang(\2) \1\2\3:
268 '"$ALL_NAME$GNOME"'s:\(.*/gnome/help/[^/]\+$\):%dir \1:
269 '"$ALL_NAME$GNOME"'s:\(.*/gnome/help/[^/]\+/\)\([^/]\+\)$:%lang(\2) \1\2:
270 /^[^%]/d
271 s:%lang(C) ::' >> $MO_NAME
272
273 # mate
274 (
275         if [ "$ALL_NAME" ]; then
276                 grep -F $NAME __find.dirs
277         else
278                 cat __find.dirs
279         fi
280 ) | sed '
281 '"$NO_ALL_NAME$MATE"'s:\(.*/share/help/\)\([^/]\+\)\(/'"$NAME"'\)$:%lang(\2) \1\2\3:
282 '"$NO_ALL_NAME$MATE"'s:\(.*/mate/help/'"$NAME"'$\):%dir \1:
283 '"$NO_ALL_NAME$MATE"'s:\(.*/mate/help/'"$NAME"'/\)\([^/]\+\)$:%lang(\2) \1\2:
284 '"$ALL_NAME$MATE"'s:\(.*/share/help/\)\([^/]\+\)\(/[^/]\+\)$:%lang(\2) \1\2\3:
285 '"$ALL_NAME$MATE"'s:\(.*/mate/help/[^/]\+$\):%dir \1:
286 '"$ALL_NAME$MATE"'s:\(.*/mate/help/[^/]\+/\)\([^/]\+\)$:%lang(\2) \1\2:
287 /^[^%]/d
288 s:%lang(C) ::' >> $MO_NAME
289
290 # kde
291 (
292         if [ "$ALL_NAME" ]; then
293                 grep -F $NAME __find.dirs
294         else
295                 cat __find.dirs
296         fi
297 ) | sed '
298 '"$NO_ALL_NAME$KDE"'s:\(.*/doc/kde/HTML/\)\([^/]\+\)\(/'"$NAME"'\)$:%lang(\2) \1\2\3:
299 '"$ALL_NAME$KDE"'s:\(.*/doc/kde/HTML/\)\([^/]\+\)\(/[^/]\+\)$:%lang(\2) \1\2\3:
300 /^[^%]/d
301 s:%lang(C) ::' >> $MO_NAME
302
303 # OMF
304 (
305         if [ "$ALL_NAME" ]; then
306                 grep -F $NAME __find.dirs
307         else
308                 cat __find.dirs
309         fi
310 ) | sed '
311 '"$NO_ALL_NAME$OMF"'s:\(.*/share/omf/'"$NAME"'$\):%dir \1:
312 '"$ALL_NAME$OMF"'s:\(.*/share/omf/[^/]\+$\):%dir \1:
313 /^[^%]/d
314 s:%lang(C) ::' >> $MO_NAME
315
316 # Django
317 cat __find.dirs | sed -r -e '
318 '"$DJANGO"'s:(.+/share/python.+/locale/)([^/@]+)(@quot|@boldquot)?(@[^/]*)?$:%lang(\2\4) \1\2\3\4:
319 /^[^%]/d
320 s:%lang(C) ::' >> $MO_NAME
321
322 if [ "$(grep -Ev '(^%defattr|^$)' $MO_NAME | wc -l)" -le 0 ]; then
323         echo >&2 "$PROG: Error: international files not found for '$NAME'!"
324         exit 1
325 fi
326
327 if [ "$APPEND" ]; then
328         cat $MO_NAME >> $OUTPUT
329         rm -f $MO_NAME
330 else
331         mv -f $MO_NAME $OUTPUT
332 fi