locale: tools: Make finalize idempotent

The finalize script renames source files with 3 components in their name
into names with two components with an @modifier, in the process.

Running the script for a second time without cleaning will strip the
@modifier from the files, producing invalid Makefiles and unusable
locales.

Prevent this by adding a guard at the beginning of the script.

Also, use a sub-shell for directory changes to avoid working directory
issues.

Reviewed by:	bapt
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D53981
This commit is contained in:
Jose Luis Duran
2025-12-06 12:20:19 +00:00
parent 36cfa8093d
commit 934364da7f
+58 -53
View File
@@ -69,64 +69,69 @@ AWKCMD="/## PLACEHOLDER/ { \
while ( getline line < \"${TEMP4}\" ) {print line} } \
!/## / { print \$0 }"
# Rename the sources with 3 components name into the POSIX version of the name using @modifier
mkdir -p $old $new
cd $old
pwd
for i in *_*_*.*.src; do
if [ "$i" = "*_*_*.*.src" ]; then
break
fi
oldname=${i%.*}
nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
mv -f ${oldname}.src ${nname}.src
sed -i '' -e "s/${oldname}/${nname}/g" Makefile
done
if ! find "$old" -name "*_*_*.*.src" -type f -print -quit | grep -q .; then
exit
fi
# For variable without @modifier ambiguity do not keep the @modifier
for i in *@*.src; do
if [ "$i" = "*@*.src" ]; then
break
fi
oldname=${i%.*}
shortname=${oldname%@*}
if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
mv -f $i ${shortname}.src
sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
fi
done
mkdir -p $new
(
cd $old
# Rename the modifiers into non abbreviated version
for i in *@Latn.src; do
if [ "$i" = "*@Latn.src" ]; then
break
fi
mv -f ${i} ${i%@*}@latin.src
sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
done
for i in *@Cyrl.src; do
if [ "$i" = "*@Cyrl.src" ]; then
break
fi
mv -f ${i} ${i%@*}@cyrillic.src
sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
done
# On locales with multiple modifiers rename the "default" version without the @modifier
default_locales="sr_RS@cyrillic"
for i in ${default_locales}; do
localename=${i%@*}
mod=${i#*@}
for l in ${localename}.*@${mod}.src; do
if [ "$l" = "${localename}.*@${mod}.src" ]; then
# Rename the sources with 3 components name into the POSIX version of the name using @modifier
for i in *_*_*.*.src; do
if [ "$i" = "*_*_*.*.src" ]; then
break
fi
mv -f ${l} ${l%@*}.src
sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
oldname=${i%.*}
nname=`echo $oldname | awk '{ split($0, a, "_"); print a[1]"_"a[3]"@"a[2];} '`
mv -f ${oldname}.src ${nname}.src
sed -i '' -e "s/${oldname}/${nname}/g" Makefile
done
done
cd -
# For variable without @modifier ambiguity do not keep the @modifier
for i in *@*.src; do
if [ "$i" = "*@*.src" ]; then
break
fi
oldname=${i%.*}
shortname=${oldname%@*}
if [ $(ls ${shortname}@* | wc -l) -eq 1 ] ; then
mv -f $i ${shortname}.src
sed -i '' -e "s/${oldname}/${shortname}/g" Makefile
fi
done
# Rename the modifiers into non abbreviated version
for i in *@Latn.src; do
if [ "$i" = "*@Latn.src" ]; then
break
fi
mv -f ${i} ${i%@*}@latin.src
sed -i '' -e "s/${i%.*}/${i%@*}@latin/g" Makefile
done
for i in *@Cyrl.src; do
if [ "$i" = "*@Cyrl.src" ]; then
break
fi
mv -f ${i} ${i%@*}@cyrillic.src
sed -i '' -e "s/${i%.*}/${i%@*}@cyrillic/g" Makefile
done
# On locales with multiple modifiers rename the "default" version without the @modifier
default_locales="sr_RS@cyrillic"
for i in ${default_locales}; do
localename=${i%@*}
mod=${i#*@}
for l in ${localename}.*@${mod}.src; do
if [ "$l" = "${localename}.*@${mod}.src" ]; then
break
fi
mv -f ${l} ${l%@*}.src
sed -i '' -e "s/${l%.*}/${l%@*}/g" Makefile
done
done
)
grep '^LOCALES+' ${old}/Makefile > ${TEMP}