1#!/bin/sh
2#$Id$
3#Copyright (c) 2010-2015 Pierre Pronchery <khorben@defora.org>
4#
5#Redistribution and use in source and binary forms, with or without
6#modification, are permitted provided that the following conditions are met:
7#
8# * Redistributions of source code must retain the above copyright notice, this
9#   list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright notice,
11#   this list of conditions and the following disclaimer in the documentation
12#   and/or other materials provided with the distribution.
13#
14#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18#FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19#DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20#SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25
26
27#variables
28PREFIX="/usr/local"
29[ -f "../config.sh" ] && . "../config.sh"
30LOCALEDIR="$PREFIX/share/locale"
31POTFILES="POTFILES"
32PROGNAME="gettext.sh"
33#executables
34DEBUG="_debug"
35INSTALL="install -m 0644"
36MKDIR="mkdir -p"
37MSGFMT="msgfmt"
38MSGINIT="msginit"
39MSGMERGE="msgmerge"
40RM="rm -f"
41XGETTEXT="xgettext --force-po"
42
43
44#functions
45#debug
46_debug()
47{
48	echo "$@" 1>&3
49	"$@"
50}
51
52
53#error
54_error()
55{
56	echo "$PROGNAME: $@" 1>&2
57	return 2
58}
59
60
61#usage
62_usage()
63{
64	echo "Usage: $PROGNAME [-c|-i|-u][-P prefix] target..." 1>&2
65	return 1
66}
67
68
69#gettext_mo
70_gettext_mo()
71{
72	package="$1"
73	lang="$2"
74	potfile="$3"
75	pofile="$4"
76	mofile="$5"
77
78	_gettext_po "$package" "$lang" "$potfile" "$pofile"	|| return 1
79	$DEBUG $MSGFMT -c -v -o "$mofile" "$pofile"		|| return 1
80}
81
82
83#gettext_po
84_gettext_po()
85{
86	package="$1"
87	lang="$2"
88	potfile="$3"
89	pofile="$4"
90
91	if [ -f "$pofile" ]; then
92		$DEBUG $MSGMERGE -U "$pofile" "$potfile"	|| return 1
93	else
94		$DEBUG $MSGINIT -l "$lang" -o "$pofile" -i "$potfile" \
95								|| return 1
96	fi
97}
98
99
100#gettext_pot
101_gettext_pot()
102{
103	package="$1"
104	potfile="$2"
105
106	$DEBUG $XGETTEXT -d "$package" -o "$potfile" --keyword="_" \
107			--keyword="N_" -f "$POTFILES"		|| return 1
108}
109
110
111#main
112clean=0
113install=0
114uninstall=0
115while getopts "ciuP:" name; do
116	case "$name" in
117		c)
118			clean=1
119			;;
120		i)
121			uninstall=0
122			install=1
123			;;
124		u)
125			install=0
126			uninstall=1
127			;;
128		P)
129			PREFIX="$OPTARG"
130			;;
131		?)
132			_usage
133			exit $?
134			;;
135	esac
136done
137shift $(($OPTIND - 1))
138if [ $# -eq 0 ]; then
139	_usage
140	exit $?
141fi
142
143#check the variables
144if [ -z "$PACKAGE" ]; then
145	_error "The PACKAGE variable needs to be set"
146	exit $?
147fi
148
149LOCALEDIR="$PREFIX/share/locale"
150exec 3>&1
151while [ $# -gt 0 ]; do
152	target="$1"
153	source="${target#$OBJDIR}"
154	lang="${source%%.mo}"
155	lang="${lang%%.po}"
156	shift
157
158	#clean
159	[ "$clean" -ne 0 ] && continue
160
161	#uninstall
162	if [ "$uninstall" -eq 1 ]; then
163		$DEBUG $RM "$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
164								|| exit 2
165		continue
166	fi
167
168	#install
169	if [ "$install" -eq 1 ]; then
170		$DEBUG $MKDIR "$LOCALEDIR/$lang/LC_MESSAGES"	|| exit 2
171		$DEBUG $INSTALL "$target" \
172			"$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
173								|| exit 2
174		continue
175	fi
176
177	#create
178	case "$target" in
179		*.mo)
180			#XXX may not match
181			if [ -n "$OBJDIR" ]; then
182				potfile="$OBJDIR/$PACKAGE.pot"
183			else
184				potfile="$PACKAGE.pot"
185			fi
186			mofile="$target"
187			pofile="${source%%.mo}.po"
188			_gettext_mo "$PACKAGE" "$lang" "$potfile" "$pofile" \
189				"$mofile"			|| exit 2
190			;;
191		*.po)
192			#XXX may not match
193			if [ -n "$OBJDIR" ]; then
194				potfile="$OBJDIR/$PACKAGE.pot"
195			else
196				potfile="$PACKAGE.pot"
197			fi
198			pofile="$target"
199			_gettext_po "$PACKAGE" "$lang" "$potfile" "$pofile" \
200								|| exit 2
201			;;
202		*.pot)
203			package="${source%%.pot}"
204			potfile="$target"
205			_gettext_pot "$package" "$potfile"	|| exit 2
206			;;
207		*)
208			exit 2
209			;;
210	esac
211done
212