1#!/usr/local/bin/bash
2#
3# Copyright 2004, 2005 Zuza Software Foundation
4#
5# This file is part of The Translate Toolkit.
6#
7# The Translate Toolkit is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# translate is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, see <http://www.gnu.org/licenses/>.
19
20
21# pomigrate2 - migrates PO files from an old version using new POT files.
22
23
24function usage() {
25	echo "Usage `basename $0` [options] <product-old> <product-new> <product-templates>"
26        echo
27        echo "Options:"
28        echo "   -F|--use-fuzzy-matching - use fuzzy algorithms when merging to attempt to match strings"
29        echo "   -C|--use-compendium     - create and use a compendium built from the migrating files"
30        echo "   -C|--use-compendium=some-compendium.po"
31	echo "                           - use an external compendium during the migration"
32        echo "   --no-wrap               - do not wrap long lines"
33        echo "   --locale=lang           - set locale for newly born files"
34        echo "   -q|--quiet              - suppress most output"
35        echo "   -p|--pot2po             - use pot2po instead of msgmerge to migrate"
36	exit 1
37}
38
39option_no_fuzzy_matching="--no-fuzzy-matching"
40option_verbose="Y"
41
42while true
43do
44	case $1 in
45		-F|--use-fuzzy-matching)
46			option_no_fuzzy_matching=""
47			shift
48			;;
49		-C|--use-compendium)
50			option_use_compendium="--compendium"
51			shift
52			;;
53		-C=*|--use-compendium=*)
54			option_use_own_compendium="$option_use_own_compendium --compendium=`echo $1 | sed 's/\-C=\|--use-compendium=//'`"
55			option_pot2po_use_own_compendium="$option_pot2po_use_own_compendium --tm=`echo $1 | sed 's/\-C=\|--tm=//'`"
56			shift
57			;;
58		--no-wrap)
59			option_no_wrap="--no-wrap"
60			shift
61			;;
62		--locale=*)
63			option_locale=`echo $1 | sed 's/--locale=//'`
64			shift
65			;;
66		--locale)
67			shift
68                        if [ $# -lt 1 ]; then
69                            usage
70                        fi
71			option_locale=$1
72                        shift
73			;;
74		-q|--quiet)
75			option_verbose=""
76			option_verbose_msgmerge="--quiet"
77			shift
78			;;
79		-p|--pot2po)
80			option_pot2po="pot2po"
81			shift
82			;;
83		-*|--*)
84			usage
85			;;
86		*)
87			break
88			;;
89	esac
90done
91
92if [ $# -ne 3 ]; then
93	usage
94fi
95
96old=$1
97new=$2
98templates=$3
99
100echo "** Migrating files... **"
101pots=`cd $templates; find . -name "*.pot"`
102if [ "$pots" == "" ]; then
103	echo "No POT templates found in: $templates"
104	exit 1
105fi
106for pot in $pots
107do
108	filename=`basename $pot .pot`.po
109	directory=`dirname $pot`
110	mkdir -p $new/$directory
111	if [ -f $old/$directory/$filename ]; then
112		cp -p $old/$directory/$filename $new/$directory/$filename && [ $option_verbose ] && echo Copied $new/$directory/$filename
113	else
114		if [ `find $old -name "$filename" | wc -l` -ge 1 ]; then
115			msgcat $option_no_wrap -o $new/$directory/$filename `find $old -name "$filename" -printf "%p "` && [ $option_verbose ] && echo Migrated $new/$directory/$filename
116		else
117			if [ ! $option_verbose ]; then
118				msginit $option_no_wrap --locale=$option_locale --no-translator -i $templates/$pot -o $new/$directory/$filename 2>&1 | egrep -v "^Created"
119			else
120				msginit $option_no_wrap --locale=$option_locale --no-translator -i $templates/$pot -o $new/$directory/$filename
121			fi
122		fi
123	fi
124done
125
126if [ "$option_use_compendium" != "" ]; then
127	echo "** Creating compendium from old files... **"
128	compendium=`mktemp -t /tmp tmp.compendium.XXXXXXXXXX`
129	# Move and rename to work around inability of mktemp TEMPLATE to end on anything but X's
130        mv $compendium ${compendium}.po
131        compendium=${compendium}.po
132	`dirname $0`/pocompendium --correct $compendium -d $old
133	option_use_compendium=" --compendium=$compendium "
134	option_pot2po_use_compendium=" --tm=$compendium "
135fi
136
137echo -n "** Updating files against templates"
138[ $option_use_compendium ] && echo -n " using a compendium"
139echo " **"
140if [ ! $option_pot2po ]; then
141	for po in `cd $new ; find . -name "*.po" | sort`
142	do
143		[ $option_verbose ] && echo -n $new/$po
144		[ -f $templates/${po}t ] && msgmerge --previous $option_verbose_msgmerge $option_no_fuzzy_matching $option_no_wrap $option_use_compendium $option_use_own_compendium --backup=off --update $new/$po $templates/${po}t
145	done
146else
147	temp_pot2po_new=`mktemp -d -t /tmp tmp.XXXXXXXXXX`
148	temp_msgcat_new=`mktemp -d -t /tmp tmp.XXXXXXXXXX`
149	cp -rp $new/* $temp_pot2po_new
150	pot2po --errorlevel=traceback --progress=none $option_pot2po_use_compendium $option_pot2po_use_own_compendium -t $temp_pot2po_new $templates $temp_msgcat_new
151	for file in `cd $temp_msgcat_new; find . -name "*.po"`
152	do
153		mkdir -p $new/$(dirname $file)
154		msgcat -o $new/$file $temp_msgcat_new/$file 2> >(egrep -v "warning: internationali.ed messages should not contain the .* escape sequence" >&2)
155	done
156	rm -rf $temp_pot2po_new $temp_msgcat_new
157fi
158
159if [ "$option_use_compendium" != "" ]; then
160	rm $compendium
161fi
162