| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # Run a wrapped command with at least the requested locales available. |
| 4 | # Requires a dependency on locales | locales-all. |
| 5 | # The requested locales must be of the form foo_FOO.utf8, or special-cased |
| 6 | # in generate(), or listed in /usr/share/i18n/SUPPORTED, or specify the |
| 7 | # locale in the form locale=charset. |
| 8 | # |
| 9 | # Copyright 2016-2021 Simon McVittie |
| 10 | # Copyright 2017-2018 Collabora Ltd. |
| 11 | # |
| 12 | # SPDX-License-Identifier: MIT |
| 13 | # |
| 14 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | # of this software and associated documentation files (the "Software"), to |
| 16 | # deal in the Software without restriction, including without limitation the |
| 17 | # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 18 | # sell copies of the Software, and to permit persons to whom the Software is |
| 19 | # furnished to do so, subject to the following conditions: |
| 20 | # |
| 21 | # The above copyright notice and this permission notice shall be included in |
| 22 | # all copies or substantial portions of the Software. |
| 23 | # |
| 24 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 29 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 30 | # IN THE SOFTWARE. |
| 31 | |
| 32 | # Assume a Debian Policy §10.4-compatible shell like dash or bash (with the |
| 33 | # 'local' builtin) |
| 34 | # shellcheck disable=SC2039 |
| 35 | |
| 36 | set -eu |
| 37 | |
| 38 | me="$(basenamescip-shell system . . basename. "$0")" |
| 39 | tempdir= |
| 40 | |
| 41 | usagescip-shell shell gtk4 4.22.4+ds-1 usage(). () { |
| 42 | local status="${1-2}" |
| 43 | |
| 44 | if [scip-shell system . . `[`. "$status" -ne 0 ]; then |
| 45 | exec >&2 |
| 46 | fi |
| 47 | |
| 48 | echoscip-shell system . . echo. "Usage: $me [--generate LOCALE...] [--] COMMAND [ARGS...]" |
| 49 | exit "$status" |
| 50 | } |
| 51 | |
| 52 | getopt_temp=help |
| 53 | getopt_temp="$getopt_temp,generate:" |
| 54 | |
| 55 | getopt_temp="$(getoptscip-shell system . . getopt. -o '+' --long "$getopt_temp" -n "$0" -- "$@")" |
| 56 | eval set -- "$getopt_temp" |
| 57 | unset getopt_temp |
| 58 | |
| 59 | generatescip-shell shell gtk4 4.22.4+ds-1 generate(). () { |
| 60 | local locale="$1" |
| 61 | local charset="" |
| 62 | local source |
| 63 | local output |
| 64 | |
| 65 | echoscip-shell system . . echo. "$me: $locale..." >&2 |
| 66 | |
| 67 | case "$locale" in |
| 68 | (*=*) |
| 69 | source="${locale%=*}" |
| 70 | output="$source" |
| 71 | ;; |
| 72 | |
| 73 | (*.*) |
| 74 | source="${locale%.*}" |
| 75 | output="$source.$( |
| 76 | export LC_ALL=C |
| 77 | printfscip-shell system . . printf. '%s' "${locale##*.}" | \ |
| 78 | tr '[:upper:]' '[:lower:]' | \ |
| 79 | tr -d -C '[:lower:][:digit:]' |
| 80 | )" |
| 81 | ;; |
| 82 | |
| 83 | (*) |
| 84 | source="${locale}" |
| 85 | output="${locale}" |
| 86 | esac |
| 87 | |
| 88 | if [scip-shell system . . `[`. -e "/usr/lib/locale/$output/LC_MESSAGES/SYS_LC_MESSAGES" ]; then |
| 89 | printfscip-shell system . . printf. '\t%s\n' "Found in locales-all" >&2 |
| 90 | return |
| 91 | fi |
| 92 | |
| 93 | if ! [scip-shell system . . `[`. -d /usr/share/i18n/charmapsscip-shell filesystem . . `/usr/share/i18n/charmaps`. ]; then |
| 94 | echoscip-shell system . . echo. "$me: $locale not in locales-all and /usr/share/i18n/charmaps not found" >&2 |
| 95 | return |
| 96 | fi |
| 97 | |
| 98 | case "$locale" in |
| 99 | (*=*) |
| 100 | charset="${locale##*=}" |
| 101 | ;; |
| 102 | |
| 103 | (*.utf8 | *.UTF-8) |
| 104 | charset="UTF-8" |
| 105 | ;; |
| 106 | |
| 107 | (*.*) |
| 108 | charset="${locale##*.}" |
| 109 | ;; |
| 110 | esac |
| 111 | |
| 112 | if [scip-shell system . . `[`. -z "$charset" ]; then |
| 113 | echoscip-shell system . . echo. "$me: $locale charset not known" >&2 |
| 114 | exit 1 |
| 115 | fi |
| 116 | |
| 117 | printfscip-shell system . . printf. '\t%s\n' "Character set: $charset" >&2 |
| 118 | printfscip-shell system . . printf. '\t%s\n' "Source file: $source" >&2 |
| 119 | |
| 120 | if [scip-shell system . . `[`. -z "$tempdir" ]; then |
| 121 | tempdir="$(mktempscip-shell system . . mktemp. -d)" |
| 122 | trap 'rm -fr "$tempdir"' EXIT |
| 123 | fi |
| 124 | |
| 125 | printfscip-shell system . . printf. '\t%s\n' "Output: $tempdir/$output" >&2 |
| 126 | |
| 127 | localedefscip-shell system . . localedef. -i "$source" -f "$charset" "$tempdir/$output" |
| 128 | } |
| 129 | |
| 130 | while [scip-shell system . . `[`. "$#" -gt 0 ]; do |
| 131 | case "$1" in |
| 132 | (--help) |
| 133 | usagescip-shell shell gtk4 4.22.4+ds-1 usage(). 2 |
| 134 | # not reached |
| 135 | ;; |
| 136 | |
| 137 | (--generate) |
| 138 | generatescip-shell shell gtk4 4.22.4+ds-1 generate(). "$2" |
| 139 | shift 2 |
| 140 | ;; |
| 141 | |
| 142 | (--) |
| 143 | shift |
| 144 | break |
| 145 | ;; |
| 146 | |
| 147 | (-*) |
| 148 | echoscip-shell system . . echo. "$me: Unknown option: $1" >&2 |
| 149 | usagescip-shell shell gtk4 4.22.4+ds-1 usage(). 2 |
| 150 | # not reached |
| 151 | ;; |
| 152 | |
| 153 | (*) |
| 154 | break |
| 155 | ;; |
| 156 | esac |
| 157 | done |
| 158 | |
| 159 | if [scip-shell system . . `[`. -n "$tempdir" ]; then |
| 160 | export LOCPATH="$tempdir" |
| 161 | fi |
| 162 | |
| 163 | "$@" |
| 164 | |
| 165 | # vim:set sw=4 sts=4 et: |