3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
30 # Just print the usage and exit with an error.
35 printf "usage: %s [-l] NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2
39 # Run gencat on one file.
40 # @param loc The location of the resulting cat file.
41 # @param file The file to use as the source for the cat file.
50 mkdir -p $(dirname "$_gencatfile_loc")
51 gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1
54 # Return an exit code based on whether a locale exists.
55 # @param locales The list of locales.
56 # @param locale The locale to search for.
57 # @param destdir The DESTDIR that locales should be installed to.
60 _localeexists_locales="$1"
63 _localeexists_locale="$1"
66 _localeexists_destdir="$1"
69 if [ "$_localeexists_destdir" != "" ]; then
70 _localeexists_char="@"
71 _localeexists_locale="${_localeexists_locale%%_localeexists_char*}"
72 _localeexists_char="."
73 _localeexists_locale="${_localeexists_locale##*$_localeexists_char}"
76 test ! -z "${_localeexists_locales##*$_localeexists_locale*}"
80 # Split a path into its components. They will be separated by newlines, so paths
81 # cannot have newlines in them.
82 # @param path The path to split.
88 if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then
89 printf 'Must use absolute paths\n'
93 if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then
100 while [ "$_splitpath_path" != "/" ]; do
101 _splitpath_item=$(basename "$_splitpath_path")
102 _splitpath_list=$(printf '\n%s%s' "$_splitpath_item" "$_splitpath_list")
103 _splitpath_path=$(dirname "$_splitpath_path")
106 if [ "$_splitpath_list" != "/" ]; then
107 _splitpath_list="${_splitpath_list#?}"
110 printf '%s' "$_splitpath_list"
113 # Generate a relative path from one path to another.
114 # @param path1 The target path.
115 # @param path2 The other path.
124 # Very carefully set IFS in a portable way. No, you cannot do IFS=$'\n'.
125 _relpath_nl=$(printf '\nx')
126 _relpath_nl="${_relpath_nl%x}"
128 _relpath_splitpath1=`splitpath "$_relpath_path1"`
129 _relpath_splitpath2=`splitpath "$_relpath_path2"`
132 _relpath_temp1="$_relpath_splitpath1"
136 # What this function does is find the parts that are the same and then
137 # calculates the difference based on how many folders up and down you must
140 # This first loop basically removes the parts that are the same between
142 for _relpath_part in $_relpath_temp1; do
144 _relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}"
146 if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then
150 _relpath_splitpath2="$_relpath_temp2"
151 _relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}"
155 # Go up the appropriate number of times.
156 for _relpath_part in $_relpath_splitpath2; do
157 _relpath_path="../$_relpath_path"
160 _relpath_path="${_relpath_path%../}"
162 # Go down the appropriate number of times.
163 for _relpath_part in $_relpath_splitpath1; do
164 _relpath_path="$_relpath_path$_relpath_part/"
167 _relpath_path="${_relpath_path%/}"
171 printf '%s\n' "$_relpath_path"
175 scriptdir=$(dirname "$script")
177 . "$scriptdir/functions.sh"
182 # Process command-line args.
183 while getopts "l" opt; do
186 l) all_locales=1 ; shift ;;
187 ?) usage "Invalid option: $opt" ;;
192 test "$#" -ge 2 || usage
200 if [ "$#" -ge 1 ]; then
207 # Uninstall locales first.
208 "$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir"
210 locales_dir="$scriptdir/../locales"
212 # What this does is if installing to a package, it installs all locales that
213 # match supported charsets instead of installing all directly supported locales.
214 if [ "$destdir" = "" ]; then
220 # For each relevant .msg file, run gencat.
221 for file in $locales_dir/*.msg; do
223 locale=$(basename "$file" ".msg")
225 # If we are not installing all locales, there's a possibility we need to
227 if [ "$all_locales" -eq 0 ]; then
229 # Check if the locale exists and if not skip.
230 localeexists "$locales" "$locale" "$destdir"
233 if [ "$err" -eq 0 ]; then
238 # We skip the symlinks for now.
239 if [ -L "$file" ]; then
243 # Generate the proper location for the cat file.
244 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
246 gencatfile "$loc" "$file"
250 # Now that we have done the non-symlinks, it's time to do the symlinks. Think
251 # that this second loop is unnecessary and that you can combine the two? Well,
252 # make sure that when you figure out you are wrong that you add to this comment
253 # with your story. Fortunately for me, I learned fast.
254 for file in $locales_dir/*.msg; do
256 locale=$(basename "$file" ".msg")
258 # Do the same skip as the above loop.
259 if [ "$all_locales" -eq 0 ]; then
261 localeexists "$locales" "$locale" "$destdir"
264 if [ "$err" -eq 0 ]; then
269 # Generate the proper location for the cat file.
270 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
272 # Make sure the directory exists.
273 mkdir -p $(dirname "$loc")
275 # Make sure to skip non-symlinks; they are already done.
276 if [ -L "$file" ]; then
278 # This song and dance is because we want to generate relative symlinks.
279 # They take less space, but also, they are more resilient to being
281 link=$(readlink "$file")
282 linkdir=$(dirname "$file")
283 locale=$(basename "$link" .msg)
284 linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")
285 relloc="${loc##$destdir/}"
286 rel=$(relpath "$linksrc" "$relloc")
288 # If the target file doesn't exist (because it's for a locale that is
289 # not installed), generate it anyway. It's easier this way.
290 if [ ! -f "$destdir/$linksrc" ]; then
291 gencatfile "$destdir/$linksrc" "$linkdir/$link"
294 # Finally, symlink to the install of the generated cat file that
295 # corresponds to the correct msg file.