Path: blob/main/contrib/bc/scripts/locale_install.sh
39507 views
#! /bin/sh1#2# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2018-2025 Gavin D. Howard and contributors.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are met:8#9# * Redistributions of source code must retain the above copyright notice, this10# list of conditions and the following disclaimer.11#12# * Redistributions in binary form must reproduce the above copyright notice,13# this list of conditions and the following disclaimer in the documentation14# and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE26# POSSIBILITY OF SUCH DAMAGE.27#2829# Just print the usage and exit with an error.30# @param 1 A message to print.31usage() {32if [ $# -eq 1 ]; then33printf '%s\n' "$1"34fi35printf "usage: %s [-l] NLSPATH main_exec [DESTDIR]\n" "$0" 1>&236exit 137}3839# 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() {4344_gencatfile_loc="$1"45shift4647_gencatfile_file="$1"48shift4950mkdir -p $(dirname "$_gencatfile_loc")51gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&152}5354# 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() {5960_localeexists_locales="$1"61shift6263_localeexists_locale="$1"64shift6566_localeexists_destdir="$1"67shift6869if [ "$_localeexists_destdir" != "" ]; then70_localeexists_char="@"71_localeexists_locale="${_localeexists_locale%%_localeexists_char*}"72_localeexists_char="."73_localeexists_locale="${_localeexists_locale##*$_localeexists_char}"74fi7576test ! -z "${_localeexists_locales##*$_localeexists_locale*}"77return $?78}7980# Split a path into its components. They will be separated by newlines, so paths81# cannot have newlines in them.82# @param path The path to split.83splitpath() {8485_splitpath_path="$1"86shift8788if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then89printf 'Must use absolute paths\n'90exit 191fi9293if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then94exit 195fi9697_splitpath_list=""98_splitpath_item=""99100while [ "$_splitpath_path" != "/" ]; do101_splitpath_item=$(basename "$_splitpath_path")102_splitpath_list=$(printf '\n%s%s' "$_splitpath_item" "$_splitpath_list")103_splitpath_path=$(dirname "$_splitpath_path")104done105106if [ "$_splitpath_list" != "/" ]; then107_splitpath_list="${_splitpath_list#?}"108fi109110printf '%s' "$_splitpath_list"111}112113# Generate a relative path from one path to another.114# @param path1 The target path.115# @param path2 The other path.116relpath() {117118_relpath_path1="$1"119shift120121_relpath_path2="$1"122shift123124# 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}"127128_relpath_splitpath1=`splitpath "$_relpath_path1"`129_relpath_splitpath2=`splitpath "$_relpath_path2"`130131_relpath_path=""132_relpath_temp1="$_relpath_splitpath1"133134IFS="$_relpath_nl"135136# What this function does is find the parts that are the same and then137# calculates the difference based on how many folders up and down you must138# go.139140# This first loop basically removes the parts that are the same between141# them.142for _relpath_part in $_relpath_temp1; do143144_relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}"145146if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then147break148fi149150_relpath_splitpath2="$_relpath_temp2"151_relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}"152153done154155# Go up the appropriate number of times.156for _relpath_part in $_relpath_splitpath2; do157_relpath_path="../$_relpath_path"158done159160_relpath_path="${_relpath_path%../}"161162# Go down the appropriate number of times.163for _relpath_part in $_relpath_splitpath1; do164_relpath_path="$_relpath_path$_relpath_part/"165done166167_relpath_path="${_relpath_path%/}"168169unset IFS170171printf '%s\n' "$_relpath_path"172}173174script="$0"175scriptdir=$(dirname "$script")176177. "$scriptdir/functions.sh"178179# Set a default.180all_locales=0181182# Process command-line args.183while getopts "l" opt; do184185case "$opt" in186l) all_locales=1 ;;187?) usage "Invalid option: $opt" ;;188esac189190done191shift $(($OPTIND - 1))192193test "$#" -ge 2 || usage "Must have at least two arguments"194195nlspath="$1"196shift197198main_exec="$1"199shift200201if [ "$#" -ge 1 ]; then202destdir="$1"203shift204else205destdir=""206fi207208# Uninstall locales first.209"$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir"210211locales_dir="$scriptdir/../locales"212213# What this does is if installing to a package, it installs all locales that214# match supported charsets instead of installing all directly supported locales.215if [ "$destdir" = "" ]; then216locales=$(locale -a)217else218locales=$(locale -m)219fi220221# For each relevant .msg file, run gencat.222for file in $locales_dir/*.msg; do223224locale=$(basename "$file" ".msg")225226# If we are not installing all locales, there's a possibility we need to227# skip this one.228if [ "$all_locales" -eq 0 ]; then229230# Check if the locale exists and if not skip.231localeexists "$locales" "$locale" "$destdir"232err="$?"233234if [ "$err" -eq 0 ]; then235continue236fi237fi238239# We skip the symlinks for now.240if [ -L "$file" ]; then241continue242fi243244printf 'Installing %s...' "$locale"245246# Generate the proper location for the cat file.247loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")248249gencatfile "$loc" "$file"250251printf 'done\n'252253done254255# Now that we have done the non-symlinks, it's time to do the symlinks. Think256# that this second loop is unnecessary and that you can combine the two? Well,257# make sure that when you figure out you are wrong that you add to this comment258# with your story. Fortunately for me, I learned fast.259for file in $locales_dir/*.msg; do260261locale=$(basename "$file" ".msg")262263# Do the same skip as the above loop.264if [ "$all_locales" -eq 0 ]; then265266localeexists "$locales" "$locale" "$destdir"267err="$?"268269if [ "$err" -eq 0 ]; then270continue271fi272fi273274# Generate the proper location for the cat file.275loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")276277# Make sure the directory exists.278mkdir -p $(dirname "$loc")279280# Make sure to skip non-symlinks; they are already done.281if [ -L "$file" ]; then282283printf 'Linking %s...' "$locale"284285# This song and dance is because we want to generate relative symlinks.286# They take less space, but also, they are more resilient to being287# moved.288link=$(readlink "$file")289linkdir=$(dirname "$file")290locale=$(basename "$link" .msg)291linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")292relloc="${loc##$destdir/}"293rel=$(relpath "$linksrc" "$relloc")294295# If the target file doesn't exist (because it's for a locale that is296# not installed), generate it anyway. It's easier this way.297if [ ! -f "$destdir/$linksrc" ]; then298gencatfile "$destdir/$linksrc" "$linkdir/$link"299fi300301# Finally, symlink to the install of the generated cat file that302# corresponds to the correct msg file.303ln -fs "$rel" "$loc"304305printf 'done\n'306fi307308done309310311