Debian Codegraph @

scripts/poedit

raw
1 #!/bin/bash
2 # Copyright (C) 2000-2012 Marcin Owsiany <[email protected]>
3
4 #
5 # Set up temporary directory
6 #
7 POTMP=`mktemp -d -t poedit.XXXXXX` || exit 1
8
9 function usage()
10 {
11 echo "Usage: poedit [ -a ] [ -p ] [ -n ] <file.po>" >&2
12 exit 1
13 }
14
15 #
16 # Check what msgs to include
17 #
18 INCLUDE_ALL_MSGS=no
19 IGNORE_ENCODING=no
20 PRESERVE_WRAPPING=no
21
22 TEMP=`getopt -o anp -n 'poedit' -- "$@"`
23 if [ $? != 0 ] ; then usage ; fi
24 # Note the quotes around `$TEMP': they are essential!
25 eval set -- "$TEMP"
26
27 while true ; do
28 case "$1" in
29 -a)
30 INCLUDE_ALL_MSGS=yes
31 shift
32 ;;
33 -n)
34 IGNORE_ENCODING=yes
35 shift
36 ;;
37 -p)
38 PRESERVE_WRAPPING=yes
39 shift
40 ;;
41 --)
42 shift
43 break
44 ;;
45 *)
46 echo "Internal error!" >&2
47 exit 1
48 ;;
49 esac
50 done
51
52 POFILE="$1"
53 TMPFILE="$POTMP/`basename -- "$POFILE"`"
54 TMPFILE2="$TMPFILE.tmp"
55
56 #
57 # Some sanity checks
58 #
59 [ -z "$POFILE" ] && usage
60 if ! [ -w "$POFILE" ]; then
61 echo "$POFILE: not writable" >&2
62 exit 1
63 fi
64 if [ -n "$EDITOR" ]; then
65 EDITCMD="$EDITOR"
66 elif which sensible-editor >/dev/null 2>&1; then
67 EDITCMD="sensible-editor"
68 else
69 echo '$EDITOR is not set, and sensible-editor is not available' >&2
70 exit 1
71 fi
72
73 if [ $PRESERVE_WRAPPING = yes ]; then
74 potool="potool -p"
75 else
76 potool="potool"
77 fi
78
79 #
80 # Get file encoding
81 #
82 if [ $IGNORE_ENCODING = no ] ; then
83 input_encoding=$($potool -ft -fnth "$POFILE" | egrep -m 1 -i '^"Content-Type:' | perl -p -e 's,^.*charset=(.*?)( *;.*)?\\n"$,$1,')
84 if [ -z "$input_encoding" ] ; then
85 echo "Failed to retrieve encoding from the content-type header." 1>&2
86 echo "Make sure the file has a proper header before running $0" 1>&2
87 exit 1
88 fi
89 locale_encoding=$(locale charmap)
90 if [ -z "$locale_encoding" ] ; then
91 echo "Failed to retrieve the locale charmap. Something is very wrong." 1>&2
92 exit 1
93 fi
94 fi
95
96 #
97 # Filter the file
98 #
99
100 if [ "$INCLUDE_ALL_MSGS" = "yes" ]
101 then
102 $potool "$POFILE" -ft -fno > "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -ft -fno > \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
103 echo >> "$TMPFILE"
104 $potool "$POFILE" -fnt >> "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -fnt >> \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
105 echo >> "$TMPFILE"
106 $potool "$POFILE" -ft -fo >> "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -ft -fo >> \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
107 else
108 $potool "$POFILE" -fnth > "$TMPFILE" || { echo "Running '$potool \"$POFILE\" -fnth > \"$TMPFILE\"' failed with code $?" >&2; exit 1; }
109 fi
110
111 if [ $IGNORE_ENCODING = no ] ; then
112 #
113 # Recode the file so it is in the locale's encoding
114 #
115 iconv -f "$input_encoding" -t "$locale_encoding" < "$TMPFILE" > "$TMPFILE2"
116 if [ $? -ne 0 ]; then
117 echo "Recoding from [$input_encoding] to [$locale_encoding] failed." 1>&2
118 echo "Temp. file: $TMPFILE" 1>&2
119 exit 1
120 fi
121
122 #
123 # Fix the charset information
124 #
125 change-po-charset "$locale_encoding" "$TMPFILE2" > "$TMPFILE"
126 if [ $? -ne 0 ]; then
127 echo "Failed to substitute the encoding attribute in the header." 1>&2
128 echo "Make sure the file has a proper header before running $0" 1>&2
129 echo "Temp. file: $TMPFILE2" 1>&2
130 exit 1
131 fi
132 fi
133
134 #
135 # Run editor and update the file on success
136 #
137 $EDITCMD "$TMPFILE"
138 editor_ret="$?"
139
140 if [ $editor_ret -ne 0 ]; then
141 echo "$EDITCMD exited abnormally (code $editor_ret), not updating the po file" 1>&2
142 exit 1
143 fi
144
145 if [ $IGNORE_ENCODING = no ] ; then
146 #
147 # Change the charset information back
148 #
149 change-po-charset "$input_encoding" "$TMPFILE" > "$TMPFILE2"
150 if [ $? -ne 0 ]; then
151 echo "Failed to substitute the encoding attribute in the header." 1>&2
152 echo "Temp. file: " $TMPFILE 1>&2
153 exit 1
154 fi
155
156 #
157 # Recode the file back to the original encoding
158 #
159 iconv -f "$locale_encoding" -t "$input_encoding" < "$TMPFILE2" > "$TMPFILE"
160 if [ $? -ne 0 ]; then
161 echo "Recoding back from [$locale_encoding] to [$input_encoding] failed." 1>&2
162 echo "Temp. file: " $TMPFILE2 1>&2
163 exit 1
164 fi
165 fi
166
167 mv "$POFILE" "$POFILE~" || { echo "Failed to rename \"$POFILE\" to \"$POFILE~\""; exit 1; }
168 $potool "$POFILE~" "$TMPFILE" > "$POFILE"
169 if [ $? -eq 0 ]; then
170 printf "Before: %s/%s\n" `$potool -ft -s "$POFILE~"` `$potool -s "$POFILE~"`
171 printf "After: %s/%s\n" `$potool -ft -s "$POFILE"` `$potool -s "$POFILE"`
172 rm -f "$POFILE~" "$TMPFILE"
173 else
174 mv -f "$POFILE~" "$POFILE"
175 echo "Merging $POFILE with $TMPFILE failed" >&2
176 exit 1
177 fi
178
179 rm -Rf "$POTMP"