xref: /freebsd/tools/tools/locale/tools/finalize (revision 19261079)
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=${BASEDIR:-$(dirname ${self})}
51: ${ETCDIR:=${base}/../etc}
52: ${TOOLSDIR:=${base}}
53: ${OUTBASEDIR:=${base}/../${1}}
54: ${OLD_DIR:=${OUTBASEDIR}.draft}
55: ${NEW_DIR:=${OUTBASEDIR}}
56old=${OLD_DIR}
57new=${NEW_DIR}
58: ${TMPDIR:=/tmp}
59TEMP=${TMPDIR}/${1}.locales
60TEMP2=${TMPDIR}/${1}.hashes
61TEMP3=${TMPDIR}/${1}.symlinks
62TEMP4=${TMPDIR}/${1}.mapped
63FULLMAP=${TMPDIR}/utf8-map
64FULLEXTRACT=${TMPDIR}/extracted-names
65AWKCMD="/## PLACEHOLDER/ { \
66	  while ( getline line < \"${TEMP}\" ) {print line} } \
67	/## SYMPAIRS/ { \
68	  while ( getline line < \"${TEMP3}\" ) {print line} } \
69	/## LOCALES_MAPPED/ { \
70	  while ( getline line < \"${TEMP4}\" ) {print line} } \
71	!/## / { print \$0 }"
72
73# Rename the sources with 3 components name into the POSIX version of the name using @modifier
74mkdir -p $old $new
75cd $old
76pwd
77for i in *_*_*.*.src; do
78	if [ "$i" = "*_*_*.*.src" ]; then
79		break
80	fi
81	oldname=${i%.*}
82	nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
83	mv -f ${oldname}.src ${nname}.src
84	sed -i '' -e "s/${oldname}/${nname}/g" Makefile
85done
86
87# For variable without @modifier ambiguity do not keep the @modifier
88for i in *@*.src; do
89	if [ "$i" = "*@*.src" ]; then
90		break
91	fi
92	oldname=${i%.*}
93	shortname=${oldname%@*}
94	if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
95		mv -f $i ${shortname}.src
96		sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
97	fi
98done
99
100# Rename the modifiers into non abbreviated version
101for i in *@Latn.src; do
102	if [ "$i" = "*@Latn.src" ]; then
103		break
104	fi
105	mv -f ${i} ${i%@*}@latin.src
106	sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
107done
108
109for i in *@Cyrl.src; do
110	if [ "$i" = "*@Cyrl.src" ]; then
111		break
112	fi
113	mv -f ${i} ${i%@*}@cyrillic.src
114	sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
115done
116
117# On locales with multiple modifiers rename the "default" version without the @modifier
118default_locales="sr_RS@cyrillic"
119for i in ${default_locales}; do
120	localename=${i%@*}
121	mod=${i#*@}
122	for l in ${localename}.*@${mod}.src; do
123		if [ "$l" = "${localename}.*@${mod}.src" ]; then
124			break
125		fi
126		mv -f ${l} ${l%@*}.src
127		sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
128	done
129done
130cd -
131
132grep '^LOCALES+' ${old}/Makefile > ${TEMP}
133
134if [ $1 = "ctypedef" ]
135then
136	keep=$(cat ${TEMP} | awk '{ print $2 ".src" }')
137	(cd ${old} && md5 -r ${keep} | sort) > ${TEMP2}
138	keep=$(awk '{ if ($1 != last1) print $2; last1 = $1; }' ${TEMP2})
139	for original in ${keep}
140	do
141		cp ${old}/${original} ${new}/
142	done
143	awk '{ if ($1 == last1) { print "SYMPAIRS+=\t" last2 " " $2 } \
144	else {last1 = $1; last2 = $2}}' ${TEMP2} > ${TEMP3}
145	rm -f ${TEMP2}
146	/usr/bin/sed -E -e 's/[ ]+/ /g' \
147		${UNIDIR}/posix/UTF-8.cm \
148		> ${ETCDIR}/final-maps/map.UTF-8
149
150elif [ $1 = "colldef" ]
151then
152	awk -v tmp4=${TEMP4} '$1 == "SAME+=" && $0 !~ /legacy/ {
153		orig=$2
154		dest=$3
155		gsub(/.*\./, "", orig)
156		gsub(/.*\./, "", dest)
157		if (orig != dest )
158			print "LOCALES_MAPPED+=\t"$2 " "$3 > tmp4
159		}' ${old}/Makefile
160
161	for line in $(awk '{ print $3 }' ${TEMP4}); do
162		sed -i '' "/^SAME.*$line$/d" ${old}/Makefile
163	done
164	echo "" >> ${TEMP4}
165
166	keep=$(cat ${TEMP} | awk '{ print $2 }')
167	for original in ${keep}
168	do
169		cp ${old}/${original}.src ${new}/
170	done
171else  # below is everything but ctypedef
172
173	keep=$(cat ${TEMP} | awk '{ print $2 }')
174	for original in ${keep}
175	do
176		cp ${old}/${original}.src ${new}/
177	done
178
179fi
180
181grep -v '^LOCALES+' ${old}/Makefile | awk "${AWKCMD}" > ${new}/Makefile
182
183rm -f ${TEMP} ${TEMP3} ${TEMP4}
184