finalize revision 367262
1#!/bin/sh
2#
3# $FreeBSD: stable/11/tools/tools/locale/tools/finalize 367262 2020-11-02 01:05:41Z lwhsu $
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
44for i in *_*_*.*.src; do
45	oldname=${i%.*}
46	nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
47	mv -i ${oldname}.src ${nname}.src
48done
49	sed -i '' -Ee "s/([a-zA-Z]{2})_([a-zA-Z]+)_([a-zA-Z]{2}).([a-zA-Z0-9-]+)/\1_\3.\4@\2/g" ${old}/Makefile
50
51# For variable without @modifier ambiguity do not keep the @modifier
52for i in *@*.src; do
53	oldname=${i%.*}
54	shortname=${oldname%@*}
55	if [ $(ls ${shortname}@* | wc -l) -eq 1 -a ! -f ${shortname}.src ] ; then
56		mv -i $i ${shortname}.src
57		sed -i '' -e "s/${oldname}/${shortname}/g" ${old}/Makefile
58	fi
59done
60
61# Rename the modifiers into non abbreviated version
62for i in *@Latn.src; do
63	if [ "$i" = "*@Latn.src" ]; then
64		break
65	fi
66	mv ${i} ${i%@*}@latin.src
67done
68	sed -i '' -e "s/@Latn/@latin/g" ${old}/Makefile
69for i in *@Cyrl.src; do
70	if [ "$i" = "*@Cyrl.src" ]; then
71		break
72	fi
73	mv ${i} ${i%@*}@cyrillic.src
74done
75	sed -i '' -e "s/@Cyrl/@cyrillic/g" ${old}/Makefile
76
77# On locales with multiple modifiers rename the "default" version without the @modifier
78default_locales="sr_RS@cyrillic"
79for i in ${default_locales}; do
80	localename=${i%@*}
81	mod=${i#*@}
82	for l in ${localename}.*@${mod}.src; do
83		if [ "$l" = "${localename}.*@${mod}.src" ]; then
84			break
85		fi
86		mv ${l} ${l%@*}.src
87		sed -i '' -e "s/${l%.*}/${l%@*}/g" ${old}/Makefile
88	done
89done
90cd -
91
92grep '^LOCALES+' ${old}/Makefile > ${TEMP}
93
94if [ $1 = "ctypedef" ]
95then
96	keep=$(cat ${TEMP} | awk '{ print $2 ".src" }')
97	(cd ${old} && md5 -r ${keep} | sort) > ${TEMP2}
98	keep=$(awk '{ if ($1 != last1) print $2; last1 = $1; }' ${TEMP2})
99	for original in ${keep}
100	do
101		cp ${old}/${original} ${new}/
102	done
103	awk '{ if ($1 == last1) { print "SYMPAIRS+=\t" last2 " " $2 } \
104	else {last1 = $1; last2 = $2}}' ${TEMP2} > ${TEMP3}
105	rm -f ${TEMP2}
106	/usr/bin/sed -E -e 's/[ ]+/ /g' \
107		${CLDRDIR}/posix/UTF-8.cm \
108		> ${base}/../etc/final-maps/map.UTF-8
109	/usr/bin/sed -E -e 's/[ ]+/ /g' \
110		${CLDRDIR}/posix/eucCN.cm \
111		> ${base}/../etc/final-maps/map.eucCN
112	/usr/bin/sed -E -e 's/[ ]+/ /g' \
113		${CLDRDIR}/posix/eucCN.cm \
114		> ${base}/../etc/final-maps/map.GB2312
115
116	# GB18030 and Big5 are pre-generated from CLDR data
117	CHARMAPS="ARMSCII-8 CP1131 CP1251 \
118		CP866 GBK ISCII-DEV ISO8859-1 \
119		ISO8859-13 ISO8859-15 ISO8859-2 ISO8859-4 \
120		ISO8859-5 ISO8859-7 ISO8859-9 KOI8-R KOI8-U \
121		PT154 SJIS US-ASCII eucJP eucKR"
122
123	for map in ${CHARMAPS}
124	do
125		encoding=${map}
126		/usr/local/bin/perl ${base}/convert_map.pl \
127			${base}/../etc/charmaps/${map}.TXT ${encoding} \
128			| /usr/bin/sed -E -e 's/	+/ /g' \
129		> ${base}/../etc/final-maps/map.${map}
130		echo map ${map} converted.
131	done
132
133elif [ $1 = "colldef" ]
134then
135	awk -v tmp4=${TEMP4} '$1 == "SAME+=" && $0 !~ /legacy/ {
136		orig=$2
137		dest=$3
138		gsub(/.*\./, "", orig)
139		gsub(/.*\./, "", dest)
140		if (orig != dest )
141			print "LOCALES_MAPPED+=\t"$2 " "$3 > tmp4
142		}' ${old}/Makefile
143
144	for line in $(awk '{ print $3 }' ${TEMP4}); do
145		sed -i '' "/^SAME.*$line$/d" ${old}/Makefile
146	done
147	echo "" >> ${TEMP4}
148	for enc in ${COLLATIONS_SPECIAL}; do
149		nname=`echo $enc | sed -e 's/_Hans//g'`
150		sed -i '' "/^.*${nname}$/d" ${TEMP4}
151		echo "LOCALES+=	${nname}" >> ${TEMP4}
152	done
153
154	keep=$(cat ${TEMP} | awk '{ print $2 }')
155	for original in ${keep}
156	do
157		cp ${old}/${original}.src ${new}/
158	done
159else  # below is everything but ctypedef
160
161	keep=$(cat ${TEMP} | awk '{ print $2 }')
162	for original in ${keep}
163	do
164		cp ${old}/${original}.src ${new}/
165	done
166
167fi
168
169grep -v '^LOCALES+' ${old}/Makefile | awk "${AWKCMD}" > ${new}/Makefile
170
171rm -f ${TEMP} ${TEMP3} ${TEMP4}
172