aboutsummaryrefslogtreecommitdiff
path: root/scripts/locale_install.sh
blob: 3816f54ef4956de96269f6d8c79ec7ce0730ad59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#! /bin/sh
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2018-2023 Gavin D. Howard and contributors.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# Just print the usage and exit with an error.
# @param 1  A message to print.
usage() {
	if [ $# -eq 1 ]; then
		printf '%s\n' "$1"
	fi
	printf "usage: %s [-l] NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2
	exit 1
}

# Run gencat on one file.
# @param loc   The location of the resulting cat file.
# @param file  The file to use as the source for the cat file.
gencatfile() {

	_gencatfile_loc="$1"
	shift

	_gencatfile_file="$1"
	shift

	mkdir -p $(dirname "$_gencatfile_loc")
	gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1
}

# Return an exit code based on whether a locale exists.
# @param locales  The list of locales.
# @param locale   The locale to search for.
# @param destdir  The DESTDIR that locales should be installed to.
localeexists() {

	_localeexists_locales="$1"
	shift

	_localeexists_locale="$1"
	shift

	_localeexists_destdir="$1"
	shift

	if [ "$_localeexists_destdir" != "" ]; then
		_localeexists_char="@"
		_localeexists_locale="${_localeexists_locale%%_localeexists_char*}"
		_localeexists_char="."
		_localeexists_locale="${_localeexists_locale##*$_localeexists_char}"
	fi

	test ! -z "${_localeexists_locales##*$_localeexists_locale*}"
	return $?
}

# Split a path into its components. They will be separated by newlines, so paths
# cannot have newlines in them.
# @param path  The path to split.
splitpath() {

	_splitpath_path="$1"
	shift

	if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then
		printf 'Must use absolute paths\n'
		exit 1
	fi

	if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then
		exit 1
	fi

	_splitpath_list=""
	_splitpath_item=""

	while [ "$_splitpath_path" != "/" ]; do
		_splitpath_item=$(basename "$_splitpath_path")
		_splitpath_list=$(printf '\n%s%s' "$_splitpath_item" "$_splitpath_list")
		_splitpath_path=$(dirname "$_splitpath_path")
	done

	if [ "$_splitpath_list" != "/" ]; then
		_splitpath_list="${_splitpath_list#?}"
	fi

	printf '%s' "$_splitpath_list"
}

# Generate a relative path from one path to another.
# @param path1  The target path.
# @param path2  The other path.
relpath() {

	_relpath_path1="$1"
	shift

	_relpath_path2="$1"
	shift

	# Very carefully set IFS in a portable way. No, you cannot do IFS=$'\n'.
	_relpath_nl=$(printf '\nx')
	_relpath_nl="${_relpath_nl%x}"

	_relpath_splitpath1=`splitpath "$_relpath_path1"`
	_relpath_splitpath2=`splitpath "$_relpath_path2"`

	_relpath_path=""
	_relpath_temp1="$_relpath_splitpath1"

	IFS="$_relpath_nl"

	# What this function does is find the parts that are the same and then
	# calculates the difference based on how many folders up and down you must
	# go.

	# This first loop basically removes the parts that are the same between
	# them.
	for _relpath_part in $_relpath_temp1; do

		_relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}"

		if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then
			break
		fi

		_relpath_splitpath2="$_relpath_temp2"
		_relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}"

	done

	# Go up the appropriate number of times.
	for _relpath_part in $_relpath_splitpath2; do
		_relpath_path="../$_relpath_path"
	done

	_relpath_path="${_relpath_path%../}"

	# Go down the appropriate number of times.
	for _relpath_part in $_relpath_splitpath1; do
		_relpath_path="$_relpath_path$_relpath_part/"
	done

	_relpath_path="${_relpath_path%/}"

	unset IFS

	printf '%s\n' "$_relpath_path"
}

script="$0"
scriptdir=$(dirname "$script")

. "$scriptdir/functions.sh"

# Set a default.
all_locales=0

# Process command-line args.
while getopts "l" opt; do

	case "$opt" in
		l) all_locales=1 ;;
		?) usage "Invalid option: $opt" ;;
	esac

done
shift $(($OPTIND - 1))

test "$#" -ge 2 || usage "Must have at least two arguments"

nlspath="$1"
shift

main_exec="$1"
shift

if [ "$#" -ge 1 ]; then
	destdir="$1"
	shift
else
	destdir=""
fi

# Uninstall locales first.
"$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir"

locales_dir="$scriptdir/../locales"

# What this does is if installing to a package, it installs all locales that
# match supported charsets instead of installing all directly supported locales.
if [ "$destdir" = "" ]; then
	locales=$(locale -a)
else
	locales=$(locale -m)
fi

# For each relevant .msg file, run gencat.
for file in $locales_dir/*.msg; do

	locale=$(basename "$file" ".msg")

	# If we are not installing all locales, there's a possibility we need to
	# skip this one.
	if [ "$all_locales" -eq 0 ]; then

		# Check if the locale exists and if not skip.
		localeexists "$locales" "$locale" "$destdir"
		err="$?"

		if [ "$err" -eq 0 ]; then
			continue
		fi
	fi

	# We skip the symlinks for now.
	if [ -L "$file" ]; then
		continue
	fi

	printf 'Installing %s...' "$locale"

	# Generate the proper location for the cat file.
	loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")

	gencatfile "$loc" "$file"

	printf 'done\n'

done

# Now that we have done the non-symlinks, it's time to do the symlinks. Think
# that this second loop is unnecessary and that you can combine the two? Well,
# make sure that when you figure out you are wrong that you add to this comment
# with your story. Fortunately for me, I learned fast.
for file in $locales_dir/*.msg; do

	locale=$(basename "$file" ".msg")

	# Do the same skip as the above loop.
	if [ "$all_locales" -eq 0 ]; then

		localeexists "$locales" "$locale" "$destdir"
		err="$?"

		if [ "$err" -eq 0 ]; then
			continue
		fi
	fi

	# Generate the proper location for the cat file.
	loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")

	# Make sure the directory exists.
	mkdir -p $(dirname "$loc")

	# Make sure to skip non-symlinks; they are already done.
	if [ -L "$file" ]; then

		printf 'Linking %s...' "$locale"

		# This song and dance is because we want to generate relative symlinks.
		# They take less space, but also, they are more resilient to being
		# moved.
		link=$(readlink "$file")
		linkdir=$(dirname "$file")
		locale=$(basename "$link" .msg)
		linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")
		relloc="${loc##$destdir/}"
		rel=$(relpath "$linksrc" "$relloc")

		# If the target file doesn't exist (because it's for a locale that is
		# not installed), generate it anyway. It's easier this way.
		if [ ! -f "$destdir/$linksrc" ]; then
			gencatfile "$destdir/$linksrc" "$linkdir/$link"
		fi

		# Finally, symlink to the install of the generated cat file that
		# corresponds to the correct msg file.
		ln -fs "$rel" "$loc"

		printf 'done\n'
	fi

done