wmaker: remove useless null pointer check (Coverity #109612)
[wmaker-crm.git] / script / generate-po-from-template.sh
blob6e73ba35193f6b28825742fef46e9c7cfe832a30
1 #!/bin/sh
2 ###########################################################################
4 # Window Maker window manager
6 # Copyright (c) 2014-2015 Christophe CURIS
7 # Copyright (c) 2015 Window Maker Team
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 ###########################################################################
25 # generate-po-from-template.sh:
26 # update an existing <lang>.po file from the POT (gettext template for
27 # translations)
29 # The goal is to take the opportunity to do a little bit more than what
30 # msgmerge does, including:
31 # - copying the full template if the language file does not yet exist;
32 # - post-process a few fields that it does not change but we may wish to
33 # see updated (project name, version, ...)
35 ###########################################################################
37 # Please note that this script is writen in sh+awk on purpose: this script
38 # is gonna be run on the machine of the person who is trying to compile
39 # WindowMaker, and as such we cannot be sure to find any scripting language
40 # in a known version and that works (python/ruby/tcl/perl/php/you-name-it).
42 # So for portability, we stick to the same sh+awk constraint as Autotools
43 # to limit the problem, see for example:
44 # http://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.69/html_node/Portable-Shell.html
46 ###########################################################################
48 # Report an error on stderr and exit with status 1 to tell make could not work
49 arg_error() {
50 echo "$0: $@" >&2
51 exit 1
54 # print help and exit with success status
55 print_help() {
56 echo "$0: update language po file from xgettext template"
57 echo "Usage: $0 [options...] po_file"
58 echo "valid options are:"
59 echo " -b email : email address to place in 'Report-Msgid-Bugs-To'"
60 echo " -n name : name of the project, to place in 'Project-Id-Version'"
61 echo " -t file : template file to be used"
62 echo " -v version : version of the project, to place in 'Project-Id-Version'"
63 exit 0
66 # Extract command line arguments
67 while [ $# -gt 0 ]; do
68 case $1 in
69 -b)
70 shift
71 project_email="$1"
74 -h|-help|--help) print_help ;;
76 -n)
77 shift
78 project_name="$1"
81 -t)
82 shift
83 [ "x$template" = "x" ] || arg_error "template already set to \"$template\", can't use also \"$1\""
84 template="$1"
85 [ -r "$template" ] || arg_error "template file \"$1\" is not readable"
88 -v)
89 shift
90 project_version="$1"
93 -*) arg_error "unknow option '$1'" ;;
96 [ "x$lang_file" = "x" ] || arg_error "only 1 po file can be specified, not \"$lang_file\" and \"$1\""
97 lang_file="$1"
99 esac
100 shift
101 done
103 # Check consistency of command-line
104 [ "x$lang_file" != "x" ] || arg_error "no po file given"
105 [ "x$template" != "x" ] || arg_error "no template file given"
107 # Generate the <lang>.po using the usual method if possible
108 if [ -r "$lang_file" ] ; then
109 msgmerge --update --silent "$lang_file" "$template"
111 # Fuzzy matching is generally not great, so print a little message to make
112 # sure the user will think about taking care of it
113 grep ', fuzzy' "$lang_file" > /dev/null && \
114 echo "Warning: fuzzy matching was used in \"$lang_file\", please review"
115 else
116 # If the <lang>.po file does not exist, we create a dummy one now from the
117 # template, updating a few fields that 'msgmerge' will not do:
118 # - it won't touch 'Language', so let's handle it for the user;
119 # - it won't like 'CHARSET' in content-type, we place a safe 'UTF-8' as default;
120 echo "Warning: creating new \"$lang_file\""
121 lang="`echo "$lang_file" | sed -e 's,^.*/\([^/]*\)$,\1,' -e 's/\..*$//' `"
122 sed -e '/^"Language:/s,:.*\\n,: '"$lang"'\\n,' \
123 -e '/^"Content-Type:/s,CHARSET,UTF-8,' < "$template" > "$lang_file"
126 # We need to post-process the generated file because 'msgmerge' does not do
127 # everything, there are some field for which we can give a value to xgettext
128 # but msgmerge will not take the new value of the header on update
129 temp_file="`echo "$lang_file" | sed -e 's,^.*/\([^/]*\)$,\1,' -e 's/\.po$//' `.tmp"
131 # The 'PO-Revision-Date' is supposed to be automatically updated by the user's
132 # po edition tool, but in case it does not, we feel safer with at least the
133 # current date
134 cmd_update="/PO-Revision-Date:/s,:.*\\\\n,: `date +"%Y-%m-%d %H:%M%z" `\\\\n,"
136 # We update the 'Project-Id-Version', because for historical reasons the PO
137 # files did not have had a consistent name; it is also the opportunity to
138 # place the current version of the project in the field
139 if [ "x$project_name$project_version" != "x" ]; then
140 cmd_update="$cmd_update;/Project-Id-Version:/s,:.*\\\\n,: $project_name $project_version\\\\n,"
143 # We update the 'Report-Msgid-Bugs-To', because for historical reasons the PO
144 # files probably did not have this information; it is also an opportunity to be
145 # sure it is in line with project's latest value
146 if [ "x$project_email" != "x" ]; then
147 cmd_update="$cmd_update;/Report-Msgid-Bugs-To:/s,:.*\\\\n,: $project_email\\\\n,"
150 mv "$lang_file" "$temp_file"
151 sed -e "$cmd_update" < "$temp_file" > "$lang_file"
152 rm -f "$temp_file"