xref: /freebsd/tools/tools/locale/tools/finalize (revision 6419bb52)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4#
5# Copyright 2015 John Marino <draco@marino.st>
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30# This is a helper script for the Makefile in the parent directory.
31# When the localization definitions are generated in the draft area,
32# this script will copy base ones that others symlink to, and rearrange
33# the generate makefile to pull the LOCALES first.
34#
35
36set -e
37
38usage ()
39{
40	echo "finalize <type>' to package standard localization"
41	echo "type must be one of { monetdef, msgdef, numericdef, timedef, colldef, ctypedef }"
42	exit 1
43}
44
45[ $# -ne 1 ] && usage
46[ $1 = "monetdef" -o $1 = "msgdef" -o $1 = "colldef" -o \
47  $1 = "numericdef" -o $1 = "timedef" -o $1 = "ctypedef" ] || usage
48
49self=$(realpath $0)
50base=$(dirname ${self})
51old=${base}/../${1}.draft
52new=${base}/../${1}
53TEMP=/tmp/${1}.locales
54TEMP2=/tmp/${1}.hashes
55TEMP3=/tmp/${1}.symlinks
56TEMP4=/tmp/${1}.mapped
57FULLMAP=/tmp/utf8-map
58FULLEXTRACT=/tmp/extracted-names
59AWKCMD="/## PLACEHOLDER/ { \
60	  while ( getline line < \"${TEMP}\" ) {print line} } \
61	/## SYMPAIRS/ { \
62	  while ( getline line < \"${TEMP3}\" ) {print line} } \
63	/## LOCALES_MAPPED/ { \
64	  while ( getline line < \"${TEMP4}\" ) {print line} } \
65	!/## / { print \$0 }"
66
67# Rename the sources with 3 components name into the POSIX version of the name using @modifier
68cd $old
69pwd
70for i in *_*_*.*.src; do
71	if [ "$i" = "*_*_*.*.src" ]; then
72		break
73	fi
74	oldname=${i%.*}
75	nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
76	mv -f ${oldname}.src ${nname}.src
77	sed -i '' -e "s/${oldname}/${nname}/g" Makefile
78	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${oldname}/${nname}/g")
79done
80
81# For variable without @modifier ambiguity do not keep the @modifier
82for i in *@*.src; do
83	if [ "$i" = "*@*.src" ]; then
84		break
85	fi
86	oldname=${i%.*}
87	shortname=${oldname%@*}
88	if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
89		mv -f $i ${shortname}.src
90		sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
91		COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${oldname}/${shortname}/g")
92	fi
93done
94
95# Rename the modifiers into non abbreviated version
96for i in *@Latn.src; do
97	if [ "$i" = "*@Latn.src" ]; then
98		break
99	fi
100	mv -f ${i} ${i%@*}@latin.src
101	sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
102	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${i%.*}/${i%@*}@latin/g")
103done
104
105for i in *@Cyrl.src; do
106	if [ "$i" = "*@Cyrl.src" ]; then
107		break
108	fi
109	mv -f ${i} ${i%@*}@cyrillic.src
110	sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
111	COLLATIONS_SPECIAL=$(echo ${COLLATIONS_SPECIAL} | sed -e "s/${i%.*}/${i%@*}@cyrillic/g")
112done
113
114# On locales with multiple modifiers rename the "default" version without the @modifier
115default_locales="sr_RS@cyrillic"
116for i in ${default_locales}; do
117	localename=${i%@*}
118	mod=${i#*@}
119	for l in ${localename}.*@${mod}.src; do
120		if [ "$l" = "${localename}.*@${mod}.src" ]; then
121			break
122		fi
123		mv -f ${l} ${l%@*}.src
124		sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
125	done
126done
127cd -
128
129grep '^LOCALES+' ${old}/Makefile > ${TEMP}
130
131if [ $1 = "ctypedef" ]
132then
133	keep=$(cat ${TEMP} | awk '{ print $2 ".src" }')
134	(cd ${old} && md5 -r ${keep} | sort) > ${TEMP2}
135	keep=$(awk '{ if ($1 != last1) print $2; last1 = $1; }' ${TEMP2})
136	for original in ${keep}
137	do
138		cp ${old}/${original} ${new}/
139	done
140	awk '{ if ($1 == last1) { print "SYMPAIRS+=\t" last2 " " $2 } \
141	else {last1 = $1; last2 = $2}}' ${TEMP2} > ${TEMP3}
142	rm -f ${TEMP2}
143	/usr/bin/sed -E -e 's/[ ]+/ /g' \
144		${UNIDIR}/posix/UTF-8.cm \
145		> ${base}/../etc/final-maps/map.UTF-8
146	/usr/bin/sed -E -e 's/[ ]+/ /g' \
147		${UNIDIR}/posix/eucCN.cm \
148		> ${base}/../etc/final-maps/map.eucCN
149	/usr/bin/sed -E -e 's/[ ]+/ /g' \
150		${UNIDIR}/posix/eucCN.cm \
151		> ${base}/../etc/final-maps/map.GB2312
152
153	# GB18030 and Big5 are pre-generated from CLDR data
154	CHARMAPS="ARMSCII-8 CP1131 CP1251 \
155		CP866 GBK ISCII-DEV ISO8859-1 \
156		ISO8859-13 ISO8859-15 ISO8859-2 ISO8859-4 \
157		ISO8859-5 ISO8859-7 ISO8859-9 KOI8-R KOI8-U \
158		PT154 SJIS US-ASCII eucJP eucKR"
159
160	for map in ${CHARMAPS}
161	do
162		encoding=${map}
163		/usr/local/bin/perl ${base}/convert_map.pl \
164			${base}/../etc/charmaps/${map}.TXT ${encoding} \
165			| /usr/bin/sed -E -e 's/	+/ /g' \
166		> ${base}/../etc/final-maps/map.${map}
167		echo map ${map} converted.
168	done
169
170elif [ $1 = "colldef" ]
171then
172	awk -v tmp4=${TEMP4} '$1 == "SAME+=" && $0 !~ /legacy/ {
173		orig=$2
174		dest=$3
175		gsub(/.*\./, "", orig)
176		gsub(/.*\./, "", dest)
177		if (orig != dest )
178			print "LOCALES_MAPPED+=\t"$2 " "$3 > tmp4
179		}' ${old}/Makefile
180
181	for line in $(awk '{ print $3 }' ${TEMP4}); do
182		sed -i '' "/^SAME.*$line$/d" ${old}/Makefile
183	done
184	echo "" >> ${TEMP4}
185	for enc in ${COLLATIONS_SPECIAL}; do
186		sed -i '' "/^.*${enc}$/d" ${TEMP4}
187		echo "LOCALES+=	${enc}" >> ${TEMP4}
188	done
189
190	keep=$(cat ${TEMP} | awk '{ print $2 }')
191	for original in ${keep} ${COLLATIONS_SPECIAL}
192	do
193		cp ${old}/${original}.src ${new}/
194	done
195else  # below is everything but ctypedef
196
197	keep=$(cat ${TEMP} | awk '{ print $2 }')
198	for original in ${keep}
199	do
200		cp ${old}/${original}.src ${new}/
201	done
202
203fi
204
205grep -v '^LOCALES+' ${old}/Makefile | awk "${AWKCMD}" > ${new}/Makefile
206
207rm -f ${TEMP} ${TEMP3} ${TEMP4}
208