1#! /bin/sh 2# 3# SPDX-License-Identifier: BSD-2-Clause 4# 5# Copyright (c) 2018-2023 Gavin D. Howard and contributors. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions are met: 9# 10# * Redistributions of source code must retain the above copyright notice, this 11# list of conditions and the following disclaimer. 12# 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. 16# 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. 28# 29 30# Just print the usage and exit with an error. 31usage() { 32 if [ $# -eq 1 ]; then 33 printf '%s\n' "$1" 34 fi 35 printf "usage: %s [-l] NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2 36 exit 1 37} 38 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. 42gencatfile() { 43 44 _gencatfile_loc="$1" 45 shift 46 47 _gencatfile_file="$1" 48 shift 49 50 mkdir -p $(dirname "$_gencatfile_loc") 51 gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1 52} 53 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. 58localeexists() { 59 60 _localeexists_locales="$1" 61 shift 62 63 _localeexists_locale="$1" 64 shift 65 66 _localeexists_destdir="$1" 67 shift 68 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}" 74 fi 75 76 test ! -z "${_localeexists_locales##*$_localeexists_locale*}" 77 return $? 78} 79 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. 83splitpath() { 84 85 _splitpath_path="$1" 86 shift 87 88 if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then 89 printf 'Must use absolute paths\n' 90 exit 1 91 fi 92 93 if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then 94 exit 1 95 fi 96 97 _splitpath_list="" 98 _splitpath_item="" 99 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") 104 done 105 106 if [ "$_splitpath_list" != "/" ]; then 107 _splitpath_list="${_splitpath_list#?}" 108 fi 109 110 printf '%s' "$_splitpath_list" 111} 112 113# Generate a relative path from one path to another. 114# @param path1 The target path. 115# @param path2 The other path. 116relpath() { 117 118 _relpath_path1="$1" 119 shift 120 121 _relpath_path2="$1" 122 shift 123 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}" 127 128 _relpath_splitpath1=`splitpath "$_relpath_path1"` 129 _relpath_splitpath2=`splitpath "$_relpath_path2"` 130 131 _relpath_path="" 132 _relpath_temp1="$_relpath_splitpath1" 133 134 IFS="$_relpath_nl" 135 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 138 # go. 139 140 # This first loop basically removes the parts that are the same between 141 # them. 142 for _relpath_part in $_relpath_temp1; do 143 144 _relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}" 145 146 if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then 147 break 148 fi 149 150 _relpath_splitpath2="$_relpath_temp2" 151 _relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}" 152 153 done 154 155 # Go up the appropriate number of times. 156 for _relpath_part in $_relpath_splitpath2; do 157 _relpath_path="../$_relpath_path" 158 done 159 160 _relpath_path="${_relpath_path%../}" 161 162 # Go down the appropriate number of times. 163 for _relpath_part in $_relpath_splitpath1; do 164 _relpath_path="$_relpath_path$_relpath_part/" 165 done 166 167 _relpath_path="${_relpath_path%/}" 168 169 unset IFS 170 171 printf '%s\n' "$_relpath_path" 172} 173 174script="$0" 175scriptdir=$(dirname "$script") 176 177. "$scriptdir/functions.sh" 178 179# Set a default. 180all_locales=0 181 182# Process command-line args. 183while getopts "l" opt; do 184 185 case "$opt" in 186 l) all_locales=1 ; shift ;; 187 ?) usage "Invalid option: $opt" ;; 188 esac 189 190done 191 192test "$#" -ge 2 || usage 193 194nlspath="$1" 195shift 196 197main_exec="$1" 198shift 199 200if [ "$#" -ge 1 ]; then 201 destdir="$1" 202 shift 203else 204 destdir="" 205fi 206 207# Uninstall locales first. 208"$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir" 209 210locales_dir="$scriptdir/../locales" 211 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. 214if [ "$destdir" = "" ]; then 215 locales=$(locale -a) 216else 217 locales=$(locale -m) 218fi 219 220# For each relevant .msg file, run gencat. 221for file in $locales_dir/*.msg; do 222 223 locale=$(basename "$file" ".msg") 224 225 # If we are not installing all locales, there's a possibility we need to 226 # skip this one. 227 if [ "$all_locales" -eq 0 ]; then 228 229 # Check if the locale exists and if not skip. 230 localeexists "$locales" "$locale" "$destdir" 231 err="$?" 232 233 if [ "$err" -eq 0 ]; then 234 continue 235 fi 236 fi 237 238 # We skip the symlinks for now. 239 if [ -L "$file" ]; then 240 continue 241 fi 242 243 # Generate the proper location for the cat file. 244 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec") 245 246 gencatfile "$loc" "$file" 247 248done 249 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. 254for file in $locales_dir/*.msg; do 255 256 locale=$(basename "$file" ".msg") 257 258 # Do the same skip as the above loop. 259 if [ "$all_locales" -eq 0 ]; then 260 261 localeexists "$locales" "$locale" "$destdir" 262 err="$?" 263 264 if [ "$err" -eq 0 ]; then 265 continue 266 fi 267 fi 268 269 # Generate the proper location for the cat file. 270 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec") 271 272 # Make sure the directory exists. 273 mkdir -p $(dirname "$loc") 274 275 # Make sure to skip non-symlinks; they are already done. 276 if [ -L "$file" ]; then 277 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 280 # moved. 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") 287 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" 292 fi 293 294 # Finally, symlink to the install of the generated cat file that 295 # corresponds to the correct msg file. 296 ln -fs "$rel" "$loc" 297 fi 298 299done 300