xref: /freebsd/tools/tools/locale/tools/finalize (revision f56f82e0)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5# This is a helper script for the Makefile in the parent directory.
6# When the localization definitions are generated in the draft area,
7# this script will copy base ones that others symlink to, and rearrange
8# the generate makefile to pull the LOCALES first.
9#
10
11set -e
12
13usage ()
14{
15	echo "finalize <type>' to package standard localization"
16	echo "type must be one of { monetdef, msgdef, numericdef, timedef, colldef, ctypedef }"
17	exit 1
18}
19
20[ $# -ne 1 ] && usage
21[ $1 = "monetdef" -o $1 = "msgdef" -o $1 = "colldef" -o \
22  $1 = "numericdef" -o $1 = "timedef" -o $1 = "ctypedef" ] || usage
23
24self=$(realpath $0)
25base=$(dirname ${self})
26old=${base}/../${1}.draft
27new=${base}/../${1}
28TEMP=/tmp/${1}.locales
29TEMP2=/tmp/${1}.hashes
30TEMP3=/tmp/${1}.symlinks
31TEMP4=/tmp/${1}.mapped
32FULLMAP=/tmp/utf8-map
33FULLEXTRACT=/tmp/extracted-names
34AWKCMD="/## PLACEHOLDER/ { \
35	  while ( getline line < \"${TEMP}\" ) {print line} } \
36	/## SYMPAIRS/ { \
37	  while ( getline line < \"${TEMP3}\" ) {print line} } \
38	/## LOCALES_MAPPED/ { \
39	  while ( getline line < \"${TEMP4}\" ) {print line} } \
40	!/## / { print \$0 }"
41
42# Rename the sources with 3 components name into the POSIX version of the name using @modifier
43cd $old
44pwd
45for i in *_*_*.*.src; do
46	if [ "$i" = "*_*_*.*.src" ]; then
47		break
48	fi
49	oldname=${i%.*}
50	nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
51	mv -f ${oldname}.src ${nname}.src
52	sed -i '' -e "s/${oldname}/${nname}/g" Makefile
53	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${oldname}/${nname}/g")
54done
55
56# For variable without @modifier ambiguity do not keep the @modifier
57for i in *@*.src; do
58	if [ "$i" = "*@*.src" ]; then
59		break
60	fi
61	oldname=${i%.*}
62	shortname=${oldname%@*}
63	if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
64		mv -f $i ${shortname}.src
65		sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
66		COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${oldname}/${shortname}/g")
67	fi
68done
69
70# Rename the modifiers into non abbreviated version
71for i in *@Latn.src; do
72	if [ "$i" = "*@Latn.src" ]; then
73		break
74	fi
75	mv -f ${i} ${i%@*}@latin.src
76	sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
77	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${i%.*}/${i%@*}@latin/g")
78done
79
80for i in *@Cyrl.src; do
81	if [ "$i" = "*@Cyrl.src" ]; then
82		break
83	fi
84	mv -f ${i} ${i%@*}@cyrillic.src
85	sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
86	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${i%.*}/${i%@*}@cyrillic/g")
87done
88
89# On locales with multiple modifiers rename the "default" version without the @modifier
90default_locales="sr_RS@cyrillic"
91for i in ${default_locales}; do
92	localename=${i%@*}
93	mod=${i#*@}
94	for l in ${localename}.*@${mod}.src; do
95		if [ "$l" = "${localename}.*@${mod}.src" ]; then
96			break
97		fi
98		mv -f ${l} ${l%@*}.src
99		sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
100	done
101done
102cd -
103
104grep '^LOCALES+' ${old}/Makefile > ${TEMP}
105
106if [ $1 = "ctypedef" ]
107then
108	keep=$(cat ${TEMP} | awk '{ print $2 ".src" }')
109	(cd ${old} && md5 -r ${keep} | sort) > ${TEMP2}
110	keep=$(awk '{ if ($1 != last1) print $2; last1 = $1; }' ${TEMP2})
111	for original in ${keep}
112	do
113		cp ${old}/${original} ${new}/
114	done
115	awk '{ if ($1 == last1) { print "SYMPAIRS+=\t" last2 " " $2 } \
116	else {last1 = $1; last2 = $2}}' ${TEMP2} > ${TEMP3}
117	rm -f ${TEMP2}
118	/usr/bin/sed -E -e 's/[ ]+/ /g' \
119		${CLDRDIR}/posix/UTF-8.cm \
120		> ${base}/../etc/final-maps/map.UTF-8
121	/usr/bin/sed -E -e 's/[ ]+/ /g' \
122		${CLDRDIR}/posix/eucCN.cm \
123		> ${base}/../etc/final-maps/map.eucCN
124	/usr/bin/sed -E -e 's/[ ]+/ /g' \
125		${CLDRDIR}/posix/eucCN.cm \
126		> ${base}/../etc/final-maps/map.GB2312
127	CHARMAPS="ARMSCII-8 Big5 CP1131 CP1251 \
128		CP866 GBK ISCII-DEV ISO8859-1 \
129		ISO8859-13 ISO8859-15 ISO8859-2 ISO8859-4 \
130		ISO8859-5 ISO8859-7 ISO8859-9 KOI8-R KOI8-U \
131		PT154 SJIS US-ASCII eucJP eucKR"
132
133	# GB18030 blows up, use pre-generate Illumos version
134
135	for map in ${CHARMAPS}
136	do
137		encoding=${map}
138		/usr/local/bin/perl ${base}/convert_map.pl \
139			${base}/../etc/charmaps/${map}.TXT ${encoding} \
140			| /usr/bin/sed -E -e 's/	+/ /g' \
141		> ${base}/../etc/final-maps/map.${map}
142		echo map ${map} converted.
143	done
144
145elif [ $1 = "colldef" ]
146then
147	awk -v tmp4=${TEMP4} '$1 == "SAME+=" && $0 !~ /legacy/ {
148		orig=$2
149		dest=$3
150		gsub(/.*\./, "", orig)
151		gsub(/.*\./, "", dest)
152		if (orig != dest )
153			print "LOCALES_MAPPED+=\t"$2 " "$3 > tmp4
154		}' ${old}/Makefile
155
156	for line in $(awk '{ print $3 }' ${TEMP4}); do
157		sed -i '' "/^SAME.*$line$/d" ${old}/Makefile
158	done
159	echo "" >> ${TEMP4}
160	for enc in ${COLLATIONS_SPECIAL}; do
161		sed -i '' "/^.*${enc}$/d" ${TEMP4}
162		echo "LOCALES+=	${enc}" >> ${TEMP4}
163	done
164
165	keep=$(cat ${TEMP} | awk '{ print $2 }')
166	for original in ${keep} ${COLLATIONS_SPECIAL}
167	do
168		cp ${old}/${original}.src ${new}/
169	done
170else  # below is everything but ctypedef
171
172	keep=$(cat ${TEMP} | awk '{ print $2 }')
173	for original in ${keep}
174	do
175		cp ${old}/${original}.src ${new}/
176	done
177
178fi
179
180grep -v '^LOCALES+' ${old}/Makefile | awk "${AWKCMD}" > ${new}/Makefile
181
182rm -f ${TEMP} ${TEMP3} ${TEMP4}
183