tools/update_lua_locale_files.sh
author Simon McVittie <smcv@debian.org>
Mon, 12 Sep 2022 10:40:53 -0400
branch1.0.0
changeset 15859 7b1d6dfa3173
parent 13511 b62b14aa88d4
permissions -rwxr-xr-x
Remove FindSDL2 find-module, use sdl2-config.cmake instead This requires SDL >= 2.0.4. Since <https://bugzilla.libsdl.org/show_bug.cgi?id=2464> was fixed in SDL 2.0.4, SDL behaves as a CMake "config-file package", even if it was not itself built using CMake: it installs a sdl2-config.cmake file to ${libdir}/cmake/SDL2, which tells CMake where to find SDL's headers and library, analogous to a pkg-config .pc file. As a result, we no longer need to copy/paste a "find-module package" to be able to find a system copy of SDL >= 2.0.4 with find_package(SDL2). Find-module packages are now discouraged by the CMake developers, in favour of having upstream projects behave as config-file packages. This results in a small API change: FindSDL2 used to set SDL2_INCLUDE_DIR and SDL2_LIBRARY, but the standard behaviour for config-file packages is to set <name>_INCLUDE_DIRS and <name>_LIBRARIES. Use the CONFIG keyword to make sure we search in config-file package mode, and will not find a FindSDL2.cmake in some other directory that implements the old interface. In addition to deleting redundant code, this avoids some assumptions in FindSDL2 about the layout of a SDL installation. The current libsdl2-dev package in Debian breaks those assumptions; this is considered a bug and will hopefully be fixed soon, but it illustrates how fragile these assumptions can be. We can be more robust against different installation layouts by relying on SDL's own CMake integration. When linking to a copy of CMake in a non-standard location, users can now set the SDL2_DIR or CMAKE_PREFIX_PATH environment variable to point to it; previously, these users would have used the SDL2DIR environment variable. This continues to be unnecessary if using matching system-wide installations of CMake and SDL2, for example both from Debian.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     1
#!/bin/sh -
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     2
# Script to update all Lua locale files.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     3
# It's Clunky and slow!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     4
# Note this script may sooner or later be phased out when we move to Gettext.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     5
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     6
# HOW TO USE:
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
     7
# - Run this script in the tools/ directory.
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
     8
# - (Optional) Change LOCALEFILES below to limit the number of locale files to update
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
     9
# Result: All .lua files in share/hedgewars/Data/Locale will be updated.
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    10
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    11
# SETTINGS:
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    12
# Space-separated list of locale files to update, or *.lua for all.
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    13
# (Note: always include stub.lua)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    14
LOCALEFILES="*.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    15
13511
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    16
# END OF SETTINGS
b62b14aa88d4 Document and clean up tools directory a bit
Wuzzy <Wuzzy2@mail.ru>
parents: 13090
diff changeset
    17
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    18
# List of all Lua files to scan:
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    19
# * Missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    20
# * Campaign missions
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    21
# * Lua libraries
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    22
# * Styles (aka multiplayer scripts)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    23
# * Mission maps
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    24
# IMPORTANT: Don't forget to update this list when new places for Lua
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    25
#            directories have been added!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    26
LUAFILES="../Missions/Challenge/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    27
 ../Missions/Scenario/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    28
 ../Missions/Training/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    29
 ../Missions/Campaign/*/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    30
 ../Scripts/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    31
 ../Scripts/Multiplayer/*.lua\
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    32
 ../Maps/*/map.lua"
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    33
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    34
cd ../share/hedgewars/Data/Locale;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    35
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    36
# Temporary files
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    37
TEMP_LOC=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    38
TEMP_HEAD=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    39
TEMP_TAIL=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    40
TEMP_LUA=$(mktemp);
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    41
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    42
# Collect strings
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    43
echo "Step 1: Collect strings";
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    44
echo -n "" > $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    45
for F in loc loc_noop;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    46
	do
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    47
	grep -F "$F(\"" $LUAFILES | sed 's/")/")\n/g' | sed "s/.*$F(\"/loc(\"/;s/\").*/\")/" | grep loc | sort | uniq >> $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    48
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    49
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    50
# Update locale files
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    51
# This step is clunky and inefficient. Improve performance (if you are bored)!
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    52
echo "Step 2: Update locale files (this may take a while)";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    53
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    54
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    55
	echo $i;
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    56
	cat $TEMP_LOC | while read f
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    57
		do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    58
		STR=$(echo "$f" | sed 's/loc("//;s/")\s*$//;s/"/\\"/g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    59
		MAPS=$(grep -F -l -- "loc(\"${STR}\")" $LUAFILES | sed 's/.*\/\([^\/]*\)\/map.lua/\1/;s/.*Campaign\/\([^\/]*\)\//\1:/;s/.*\///;s/.lua//;s/ /_/g' | xargs | sed 's/ /, /g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    60
		C=$(echo $MAPS | sed 's/,/\n/' | wc -l)
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    61
		grep -Fq -- "[\"${STR}\"]" $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    62
		if (($?));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    63
		then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    64
			if ((C>0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    65
			then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    66
				echo "--      [\"${STR}\"] = \"\", -- $MAPS" >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    67
			else
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    68
				echo "--      [\"${STR}\"] = \"\"," >> $i;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    69
			fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    70
		fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    71
	done;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    72
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    73
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    74
# Sort
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    75
echo "Step 3: Sort strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    76
for i in $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    77
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    78
	echo $i;
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    79
	rm -f $TEMP_HEAD $TEMP_TAIL $TEMP_LUA;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    80
	cat $i | grep -Ev "}|{" | grep -Ev "^[[:space:]]*$" | sort | uniq > $TEMP_LUA;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    81
	echo "locale = {" > $TEMP_HEAD;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    82
	echo "}" > $TEMP_TAIL;
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    83
	cat $TEMP_HEAD $TEMP_LUA $TEMP_TAIL > $i;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    84
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    85
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    86
# Drop unused
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    87
echo "Step 4: Delete unused strings";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    88
cat stub.lua | grep '"] =' | while read f;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    89
do
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    90
	PHRASE=$(echo "$f" | sed 's/[^[]*\["//;s/"] =.*//;s/"/\\"/g');
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
    91
	CNT=$(grep -Frc "loc(\"$PHRASE\")" $TEMP_LOC);
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    92
	if (($CNT==0));
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    93
	then
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    94
		echo "|$PHRASE|";
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    95
		PHRASE=$(echo "$PHRASE" | sed 's/\\/\\\\/g;s/\[/\\[/g;s/\]/\\]/g;s/\//\\\//g');
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    96
		sed -i "/.*\[\"$PHRASE\"\].*/d" $LOCALEFILES;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    97
	fi;
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    98
done
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
    99
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   100
# Delete temporary files
13090
3f3ad415d849 Use mktemp in update_lua_locale_files.sh
Wuzzy <Wuzzy2@mail.ru>
parents: 12715
diff changeset
   101
rm $TEMP_HEAD $TEMP_TAIL $TEMP_LUA $TEMP_LOC;
12715
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   102
f84849acda02 Turn loc_gen.txt into an executable Shell script
Wuzzy <almikes@aol.com>
parents:
diff changeset
   103
echo "Done."