Debian Codegraph @

po2debconf

raw
1 #! /bin/sh
2
3 # po2debconf - merge translations into Debconf templates file
4 # Copyright (C) 2002-2005 Denis Barbier <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or (at
9 # your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #
21 # This script is part of po-debconf
22
23 : ${PODEBCONF_LIB=/usr/share/intltool-debian}
24 : ${PODEBCONF_ENCODINGS=/usr/share/po-debconf/encodings}
25
26 # Prevent automatic conversion to UTF-8 by Perl
27 unset LANGUAGE LANG LC_ALL LC_CTYPE
28
29 # Default values
30 dftencoding=utf8
31 dftformat=2
32
33 help=
34 quiet=--quiet
35 origfile=
36 podir=
37 outfile=
38 encoding=$dftencoding
39 format=$dftformat
40
41 for option
42 do
43 if [ -n "$prev" ]; then
44 eval "$prev=\$option"
45 prev=
46 shift
47 continue
48 fi
49 optarg=`expr "x$option" : 'x[^=]*=\(.*\)'`
50 case $option in
51 -h | --h | --help )
52 help=1
53 shift ;;
54 -v | --v | --verbose )
55 quiet=
56 shift ;;
57 -o | --o | --output )
58 prev=outfile
59 shift ;;
60 -o=* | --o=* | --output=* )
61 outfile=$optarg
62 shift ;;
63 --podir )
64 prev=podir
65 shift ;;
66 --podir=* )
67 podir=$optarg
68 shift ;;
69 -e | --e | --encoding )
70 prev=encoding
71 shift ;;
72 --e=* | --encoding=* )
73 encoding=$optarg
74 shift ;;
75 -E | --E | --alt-encoding )
76 encoding=popular
77 shift ;;
78 -u | --u | --utf8 )
79 # Obsolete
80 encoding=utf8
81 shift ;;
82 -n | --n | --no-utf8 )
83 # Obsoleted
84 encoding=po
85 shift ;;
86 -O | --O | --old-format )
87 # Obsolete, use po/output file instead
88 format=1
89 shift ;;
90 -* ) echo "$0: unknown option: $option ...exiting" 1>&2
91 exit 1
92 ;;
93 * ) break ;;
94 esac
95 done
96
97 origfile=$1
98
99 fail=0
100 [ -n "$origfile" ] || fail=1
101 if [ "x$help" = x1 ] || [ "x$fail" = x1 ]; then
102 cat <<EOT 1>&2
103 Usage: po2debconf [options] master
104 Options:
105 -h, --help display this help message
106 -v, --verbose enable verbose mode
107 -o, --output=FILE specify output file (Default: stdout)
108 -e, --encoding=STRING convert encoding, STRING is chosen between
109 po: no conversion
110 utf8: convert to UTF-8
111 popular: change encoding according to file map found
112 in PODEBCONF_ENCODINGS environment variable
113 (Default, map is $PODEBCONF_ENCODINGS)
114 traditional: obsolete, replaced by popular
115 --podir=DIR specify PO output directory
116 (Default: <master directory>/po)
117 EOT
118 exit $fail
119 fi
120
121 [ -f "$origfile" ] || {
122 echo "ERROR: File $origfile does not exist ...exiting" 1>&2
123 exit 1
124 }
125 utf8=
126 case $encoding in
127 po | PO )
128 encoding=po ;;
129 pop* | POP* )
130 encoding=popular ;;
131 trad* | TRAD* )
132 encoding=popular ;;
133 utf8 | utf-8 | UTF8 | UTF-8 )
134 encoding=utf8 ;;
135 * ) echo "ERROR: Wrong --encoding argument, must be po, popular or utf8 ...exiting" 1>&2
136 exit 1 ;;
137 esac
138 [ -n "$podir" ] || podir=`dirname $origfile`/po
139 [ -d "$podir" ] || {
140 echo "ERROR: Directory $podir does not exist ...exiting" 1>&2
141 exit 1
142 }
143
144 # Override values when $podir/output file is found
145 if [ -f "$podir/output" ]; then
146 outputformat=`sed -e 1q "$podir/output" | awk '{printf "%s", $1}'`
147 [ -n "$outputformat" ] && format=$outputformat
148 outputencoding=`sed -e 1q "$podir/output" | awk '{printf "%s", $2}'`
149 [ -n "$outputencoding" ] && encoding=$outputencoding
150 fi
151
152 # Test validity of $encoding and $format values
153 case $encoding in
154 po | popular | utf8 )
155 # Do nothing
156 : ;;
157 * )
158 # Invalid value, set default encoding
159 echo "Warning:Invalid encoding: $encoding, set to '$dftencoding'" 1>&2
160 encoding=$dftencoding ;;
161 esac
162 case $format in
163 1 | 2 )
164 # Do nothing
165 :
166 ;;
167 * )
168 # Invalid value, set default encoding
169 echo "Warning:Invalid format: $format, set to '$dftformat'" 1>&2
170 format=$dftformat
171 ;;
172 esac
173
174 [ "$encoding" = popular ] || format=2
175 [ "$encoding" = utf8 ] && utf8="-u"
176
177 outdir=
178 fake=
179 is_tmp=
180 tmpfile=
181 cleanup()
182 {
183 rc=$?
184 [ -n "$outdir" ] && [ -d "$outdir" ] && {
185 rm -f "$outdir"/*.po
186 rmdir "$outdir"
187 }
188 [ -n "$fake" ] && [ -L "$podir/$fake.po" ] && rm -f "$podir/$fake.po"
189 [ -n "$is_tmp" ] && [ -f "$outfile" ] && rm -f "$outfile"
190 [ -n "$tmpfile" ] && [ -f "$tmpfile" ] && rm -f "$tmpfile"
191 exit $rc
192 }
193 trap 'cleanup' HUP INT QUIT BUS PIPE TERM
194
195 if [ "$encoding" = popular ]; then
196 [ -f "$PODEBCONF_ENCODINGS" ] || {
197 echo "ERROR: File $PODEBCONF_ENCODINGS does not exist ...exiting" 1>&2
198 exit 1
199 }
200 outdir=`mktemp -t -d po2debconf.XXXXXXXXXX` || {
201 echo "ERROR: Unable to create temporary directory ...exiting" 1>&2
202 exit 1
203 }
204 tmpfile=`mktemp -t po2debconf.XXXXXXXXXX` || {
205 echo "ERROR: Unable to create temporary file ...exiting" 1>&2
206 exit 1
207 }
208 for f in $podir/*.po
209 do
210 [ -f "$f" ] || continue
211 l=`echo $f | sed -e 's/.*\///' -e 's/\.po$//'`
212 encto=`grep "^$l[ ]" "$PODEBCONF_ENCODINGS" | sed -e "s/^$l[ ][ ]*//" -e 1q`
213 if [ -n "$encto" ]; then
214 [ -n "$quiet" ] || echo "Converting $f to $encto..." 1>&2
215 if msgconv -t "$encto" "$f" -o "$tmpfile" 2>/dev/null; then
216 mv "$tmpfile" "$outdir/$l.po"
217 else
218 echo "Warning: msgconv failed when converting file $f to $encto ... file skipped" >&2
219 fi
220 else
221 echo "Warning: Unknown default encoding for $l, get it from $f" 1>&2
222 cat "$f" > "$outdir/$l.po"
223 fi
224 done
225 podir=$outdir
226 rm -f "$tmpfile"
227 fi
228
229 if [ "x$outfile" = 'x-' ] || [ -z "$outfile" ]; then
230 outfile=`mktemp -t po2debconf.XXXXXXXXXX` || {
231 echo "ERROR: Unable to create temporary file ...exiting" 1>&2
232 exit 1
233 }
234 is_tmp=1
235 fi
236
237 # Helps no.po -> nb.po transition
238 if [ -r "$podir/no.po" ]; then
239 if [ -r "$podir/nb.po" ]; then
240 echo "Warning: Both no.po and nb.po files exist, please consider removing no.po" 1>&2
241 else
242 echo "Warning: no.po is obsolete and should be renamed into nb.po" 1>&2
243 fi
244 elif [ -r "$podir/nb.po" ]; then
245 # nb.po was found, copy it to no.po to provide both -nb and -no
246 # localized fields and thus ease no -> nb transition for Norwegian
247 # speaking people.
248 fake=no
249 ln -s nb.po "$podir/no.po"
250 fi
251
252 $PODEBCONF_LIB/intltool-merge $quiet --rfc822deb-style $utf8 $podir $origfile $outfile 1>&2 || exit 1
253
254 if [ $format -le 1 ]; then
255 tmpfile=`mktemp -t po2debconf.XXXXXXXXXX` || {
256 echo "ERROR: Unable to create temporary file ...exiting" 1>&2
257 exit 1
258 }
259 sed -e 's/^\([^ :]*\)\.[^ :]*:/\1:/' $outfile > $tmpfile && mv -f $tmpfile $outfile
260 fi
261
262 tmpfile=`mktemp -t po2debconf.XXXXXXXXXX` || {
263 echo "ERROR: Unable to create temporary file ...exiting" 1>&2
264 exit 1
265 }
266 sed -e 's/^DefaultChoice/Default/' $outfile > $tmpfile && mv -f $tmpfile $outfile
267
268 [ -n "$is_tmp" ] && cat "$outfile"
269
270 # Set $? to 0
271 :
272
273 cleanup
274