1#!/bin/sh
2#$Id$
3#Copyright (c) 2010-2013 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. "../config.sh"
30LOCALEDIR="$PREFIX/share/locale"
31POTFILES="POTFILES"
32#executables
33DEBUG="_debug"
34INSTALL="install -m 0644"
35MKDIR="mkdir -p"
36MSGFMT="msgfmt"
37MSGINIT="msginit"
38MSGMERGE="msgmerge"
39RM="rm -f"
40XGETTEXT="xgettext --force-po"
41
42
43#functions
44#debug
45_debug()
46{
47	echo "$@" 1>&2
48	"$@"
49}
50
51
52#usage
53_usage()
54{
55	echo "Usage: gettext.sh [-c|-i|-u][-P prefix] target..." 1>&2
56	return 1
57}
58
59
60#gettext_mo
61_gettext_mo()
62{
63	package="$1"
64	lang="$2"
65
66	_gettext_po "$package" "$lang"				|| return 1
67	$DEBUG $MSGFMT -c -v -o "$lang.mo" "$lang.po"		|| return 1
68}
69
70
71#gettext_po
72_gettext_po()
73{
74	package="$1"
75	lang="$2"
76
77	if [ -f "$lang.po" ]; then
78		$DEBUG $MSGMERGE -U "$lang.po" "$package.pot"	|| return 1
79	else
80		$DEBUG $MSGINIT -l "$lang" -o "$lang.po" -i "$package.pot" \
81								|| return 1
82	fi
83}
84
85
86#gettext_pot
87_gettext_pot()
88{
89	package="$1"
90
91	$DEBUG $XGETTEXT -d "$package" -o "$package.pot" --keyword="_" \
92			--keyword="N_" -f "$POTFILES"		|| return 1
93}
94
95
96#main
97clean=0
98install=0
99uninstall=0
100while getopts "ciuP:" name; do
101	case "$name" in
102		c)
103			clean=1
104			;;
105		i)
106			uninstall=0
107			install=1
108			;;
109		u)
110			install=0
111			uninstall=1
112			;;
113		P)
114			PREFIX="$2"
115			;;
116		?)
117			_usage
118			exit $?
119			;;
120	esac
121done
122shift $(($OPTIND - 1))
123if [ $# -eq 0 ]; then
124	_usage
125	exit $?
126fi
127
128LOCALEDIR="$PREFIX/share/locale"
129while [ $# -gt 0 ]; do
130	target="$1"
131	lang="${target%%.mo}"
132	lang="${lang%%.po}"
133	shift
134
135	#clean
136	[ "$clean" -ne 0 ] && continue
137
138	#uninstall
139	if [ "$uninstall" -eq 1 ]; then
140		$DEBUG $RM "$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
141								|| exit 2
142		continue
143	fi
144
145	#install
146	if [ "$install" -eq 1 ]; then
147		$DEBUG $MKDIR "$LOCALEDIR/$lang/LC_MESSAGES"	|| exit 2
148		$DEBUG $INSTALL "$target" \
149			"$LOCALEDIR/$lang/LC_MESSAGES/$PACKAGE.mo" \
150								|| exit 2
151		continue
152	fi
153
154	#create
155	case "$target" in
156		*.mo)
157			_gettext_mo "$PACKAGE" "$lang"		|| exit 2
158			;;
159		*.po)
160			_gettext_po "$PACKAGE" "$lang"		|| exit 2
161			;;
162		*.pot)
163			_gettext_pot "${target%%.pot}"		|| exit 2
164			;;
165		*)
166			exit 2
167			;;
168	esac
169done
170