xref: /freebsd-src/tools/tools/locale/tools/finalize (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1#!/bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
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#
29# This is a helper script for the Makefile in the parent directory.
30# When the localization definitions are generated in the draft area,
31# this script will copy base ones that others symlink to, and rearrange
32# the generate makefile to pull the LOCALES first.
33#
34
35set -e
36
37usage ()
38{
39	echo "finalize <type>' to package standard localization"
40	echo "type must be one of { monetdef, msgdef, numericdef, timedef, colldef, ctypedef }"
41	exit 1
42}
43
44[ $# -ne 1 ] && usage
45[ $1 = "monetdef" -o $1 = "msgdef" -o $1 = "colldef" -o \
46  $1 = "numericdef" -o $1 = "timedef" -o $1 = "ctypedef" ] || usage
47
48self=$(realpath $0)
49base=${BASEDIR:-$(dirname ${self})}
50: ${ETCDIR:=${base}/../etc}
51: ${TOOLSDIR:=${base}}
52: ${OUTBASEDIR:=${base}/../${1}}
53: ${OLD_DIR:=${OUTBASEDIR}.draft}
54: ${NEW_DIR:=${OUTBASEDIR}}
55old=${OLD_DIR}
56new=${NEW_DIR}
57: ${TMPDIR:=/tmp}
58TEMP=${TMPDIR}/${1}.locales
59TEMP2=${TMPDIR}/${1}.hashes
60TEMP3=${TMPDIR}/${1}.symlinks
61TEMP4=${TMPDIR}/${1}.mapped
62FULLMAP=${TMPDIR}/utf8-map
63FULLEXTRACT=${TMPDIR}/extracted-names
64AWKCMD="/## PLACEHOLDER/ { \
65	  while ( getline line < \"${TEMP}\" ) {print line} } \
66	/## SYMPAIRS/ { \
67	  while ( getline line < \"${TEMP3}\" ) {print line} } \
68	/## LOCALES_MAPPED/ { \
69	  while ( getline line < \"${TEMP4}\" ) {print line} } \
70	!/## / { print \$0 }"
71
72# Rename the sources with 3 components name into the POSIX version of the name using @modifier
73mkdir -p $old $new
74cd $old
75pwd
76for i in *_*_*.*.src; do
77	if [ "$i" = "*_*_*.*.src" ]; then
78		break
79	fi
80	oldname=${i%.*}
81	nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
82	mv -f ${oldname}.src ${nname}.src
83	sed -i '' -e "s/${oldname}/${nname}/g" Makefile
84done
85
86# For variable without @modifier ambiguity do not keep the @modifier
87for i in *@*.src; do
88	if [ "$i" = "*@*.src" ]; then
89		break
90	fi
91	oldname=${i%.*}
92	shortname=${oldname%@*}
93	if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
94		mv -f $i ${shortname}.src
95		sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
96	fi
97done
98
99# Rename the modifiers into non abbreviated version
100for i in *@Latn.src; do
101	if [ "$i" = "*@Latn.src" ]; then
102		break
103	fi
104	mv -f ${i} ${i%@*}@latin.src
105	sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
106done
107
108for i in *@Cyrl.src; do
109	if [ "$i" = "*@Cyrl.src" ]; then
110		break
111	fi
112	mv -f ${i} ${i%@*}@cyrillic.src
113	sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
114done
115
116# On locales with multiple modifiers rename the "default" version without the @modifier
117default_locales="sr_RS@cyrillic"
118for i in ${default_locales}; do
119	localename=${i%@*}
120	mod=${i#*@}
121	for l in ${localename}.*@${mod}.src; do
122		if [ "$l" = "${localename}.*@${mod}.src" ]; then
123			break
124		fi
125		mv -f ${l} ${l%@*}.src
126		sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
127	done
128done
129cd -
130
131grep '^LOCALES+' ${old}/Makefile > ${TEMP}
132
133if [ $1 = "ctypedef" ]
134then
135	keep=$(cat ${TEMP} | awk '{ print $2 ".src" }')
136	(cd ${old} && md5 -r ${keep} | sort) > ${TEMP2}
137	keep=$(awk '{ if ($1 != last1) print $2; last1 = $1; }' ${TEMP2})
138	for original in ${keep}
139	do
140		cp ${old}/${original} ${new}/
141	done
142	awk '{ if ($1 == last1) { print "SYMPAIRS+=\t" last2 " " $2 } \
143	else {last1 = $1; last2 = $2}}' ${TEMP2} > ${TEMP3}
144	rm -f ${TEMP2}
145	/usr/bin/sed -E -e 's/[ ]+/ /g' \
146		${UNIDIR}/posix/UTF-8.cm \
147		> ${ETCDIR}/final-maps/map.UTF-8
148
149elif [ $1 = "colldef" ]
150then
151	awk -v tmp4=${TEMP4} '$1 == "SAME+=" && $0 !~ /legacy/ {
152		orig=$2
153		dest=$3
154		gsub(/.*\./, "", orig)
155		gsub(/.*\./, "", dest)
156		if (orig != dest )
157			print "LOCALES_MAPPED+=\t"$2 " "$3 > tmp4
158		}' ${old}/Makefile
159
160	for line in $(awk '{ print $3 }' ${TEMP4}); do
161		sed -i '' "/^SAME.*$line$/d" ${old}/Makefile
162	done
163	echo "" >> ${TEMP4}
164
165	keep=$(cat ${TEMP} | awk '{ print $2 }')
166	for original in ${keep}
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