Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 3 | # Copyright (c) 2018-2019 Gavin D. Howard and contributors. |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 4 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 5 | # All rights reserved. |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 6 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 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. |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 28 | # |
| 29 | |
| 30 | usage() { |
Gavin Howard | 08727ac | 2019-04-17 17:16:42 -0600 | [diff] [blame] | 31 | printf "usage: %s NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2 |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 32 | exit 1 |
| 33 | } |
| 34 | |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 35 | gencatfile() { |
| 36 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 37 | _gencatfile_loc="$1" |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 38 | shift |
| 39 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 40 | _gencatfile_file="$1" |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 41 | shift |
| 42 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 43 | mkdir -p $(dirname "$_gencatfile_loc") |
| 44 | gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1 |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 45 | } |
| 46 | |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 47 | localeexists() { |
| 48 | |
| 49 | _localeexists_locales="$1" |
| 50 | shift |
| 51 | |
| 52 | _localeexists_locale="$1" |
| 53 | shift |
| 54 | |
| 55 | _localeexists_destdir="$1" |
| 56 | shift |
| 57 | |
| 58 | if [ "$_localeexists_destdir" != "" ]; then |
| 59 | _localeexists_char="@" |
| 60 | _localeexists_locale="${_localeexists_locale%%_localeexists_char*}" |
| 61 | _localeexists_char="." |
| 62 | _localeexists_locale="${_localeexists_locale##*$_localeexists_char}" |
| 63 | fi |
| 64 | |
| 65 | test ! -z "${_localeexists_locales##*$_localeexists_locale*}" |
| 66 | return $? |
| 67 | } |
| 68 | |
Gavin Howard | e77f613 | 2019-08-08 21:34:39 -0600 | [diff] [blame] | 69 | splitpath() { |
| 70 | |
| 71 | _splitpath_path="$1" |
| 72 | shift |
| 73 | |
| 74 | if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then |
| 75 | printf 'Must use absolute paths\n' |
| 76 | exit 1 |
| 77 | fi |
| 78 | |
| 79 | if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then |
| 80 | exit 1 |
| 81 | fi |
| 82 | |
| 83 | _splitpath_list="" |
| 84 | _splitpath_item="" |
| 85 | |
| 86 | while [ "$_splitpath_path" != "/" ]; do |
| 87 | _splitpath_item=$(basename "$_splitpath_path") |
| 88 | _splitpath_list=$(printf '\n%s%s' "$_splitpath_item" "$_splitpath_list") |
| 89 | _splitpath_path=$(dirname "$_splitpath_path") |
| 90 | done |
| 91 | |
| 92 | if [ "$_splitpath_list" != "/" ]; then |
| 93 | _splitpath_list="${_splitpath_list#?}" |
| 94 | fi |
| 95 | |
| 96 | printf '%s' "$_splitpath_list" |
| 97 | } |
| 98 | |
| 99 | relpath() { |
| 100 | |
| 101 | _relpath_path1="$1" |
| 102 | shift |
| 103 | |
| 104 | _relpath_path2="$1" |
| 105 | shift |
| 106 | |
| 107 | _relpath_nl=$(printf '\nx') |
| 108 | _relpath_nl="${_relpath_nl%x}" |
| 109 | |
| 110 | _relpath_splitpath1=`splitpath "$_relpath_path1"` |
| 111 | _relpath_splitpath2=`splitpath "$_relpath_path2"` |
| 112 | |
| 113 | _relpath_path="" |
| 114 | _relpath_temp1="$_relpath_splitpath1" |
| 115 | |
| 116 | IFS="$_relpath_nl" |
| 117 | |
| 118 | for _relpath_part in $_relpath_temp1; do |
| 119 | |
| 120 | _relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}" |
| 121 | |
| 122 | if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then |
| 123 | break |
| 124 | fi |
| 125 | |
| 126 | _relpath_splitpath2="$_relpath_temp2" |
| 127 | _relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}" |
| 128 | |
| 129 | done |
| 130 | |
| 131 | for _relpath_part in $_relpath_splitpath2; do |
| 132 | _relpath_path="../$_relpath_path" |
| 133 | done |
| 134 | |
| 135 | _relpath_path="${_relpath_path%../}" |
| 136 | |
| 137 | for _relpath_part in $_relpath_splitpath1; do |
| 138 | _relpath_path="$_relpath_path$_relpath_part/" |
| 139 | done |
| 140 | |
| 141 | _relpath_path="${_relpath_path%/}" |
| 142 | |
| 143 | unset IFS |
| 144 | |
| 145 | printf '%s\n' "$_relpath_path" |
| 146 | } |
| 147 | |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 148 | script="$0" |
| 149 | scriptdir=$(dirname "$script") |
| 150 | |
Gavin Howard | 6e70f55 | 2019-04-01 14:09:39 -0600 | [diff] [blame] | 151 | . "$scriptdir/functions.sh" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 152 | |
Gavin Howard | f680e35 | 2019-04-01 13:28:48 -0600 | [diff] [blame] | 153 | test "$#" -ge 2 || usage |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 154 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 155 | nlspath="$1" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 156 | shift |
| 157 | |
Gavin Howard | f680e35 | 2019-04-01 13:28:48 -0600 | [diff] [blame] | 158 | main_exec="$1" |
| 159 | shift |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 160 | |
Gavin Howard | 08727ac | 2019-04-17 17:16:42 -0600 | [diff] [blame] | 161 | if [ "$#" -ge 1 ]; then |
| 162 | destdir="$1" |
| 163 | shift |
| 164 | else |
| 165 | destdir="" |
| 166 | fi |
| 167 | |
| 168 | "$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir" |
Gavin Howard | 00382e9 | 2019-04-11 12:56:02 -0600 | [diff] [blame] | 169 | |
Gavin Howard | 5d0d10e | 2019-04-05 13:53:50 -0600 | [diff] [blame] | 170 | locales_dir="$scriptdir/locales" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 171 | |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 172 | # What this does is if installing to a package, it installs all locales that |
| 173 | # match supported charsets instead of installing all directly supported locales. |
| 174 | if [ "$destdir" = "" ]; then |
| 175 | locales=$(locale -a) |
| 176 | else |
| 177 | locales=$(locale -m) |
| 178 | fi |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 179 | |
Gavin Howard | 5d0d10e | 2019-04-05 13:53:50 -0600 | [diff] [blame] | 180 | for file in $locales_dir/*.msg; do |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 181 | |
Gavin Howard | 80ba7c9 | 2019-04-09 17:22:53 -0600 | [diff] [blame] | 182 | locale=$(basename "$file" ".msg") |
Gavin Howard | 08727ac | 2019-04-17 17:16:42 -0600 | [diff] [blame] | 183 | loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec") |
Gavin Howard | f680e35 | 2019-04-01 13:28:48 -0600 | [diff] [blame] | 184 | |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 185 | localeexists "$locales" "$locale" "$destdir" |
| 186 | err="$?" |
| 187 | |
| 188 | if [ "$err" -eq 0 ]; then |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 189 | continue |
| 190 | fi |
| 191 | |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 192 | if [ -L "$file" ]; then |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 193 | continue |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 194 | fi |
| 195 | |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 196 | gencatfile "$loc" "$file" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 197 | |
| 198 | done |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 199 | |
| 200 | for file in $locales_dir/*.msg; do |
| 201 | |
Gavin Howard | 80ba7c9 | 2019-04-09 17:22:53 -0600 | [diff] [blame] | 202 | locale=$(basename "$file" ".msg") |
Gavin Howard | 08727ac | 2019-04-17 17:16:42 -0600 | [diff] [blame] | 203 | loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec") |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 204 | |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 205 | localeexists "$locales" "$locale" "$destdir" |
| 206 | err="$?" |
| 207 | |
| 208 | if [ "$err" -eq 0 ]; then |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 209 | continue |
| 210 | fi |
| 211 | |
| 212 | mkdir -p $(dirname "$loc") |
| 213 | |
| 214 | if [ -L "$file" ]; then |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 215 | |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 216 | link=$(readlink "$file") |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 217 | linkdir=$(dirname "$file") |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 218 | locale=$(basename "$link" .msg) |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 219 | linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec") |
Gavin Howard | 6c12e6a | 2019-08-12 15:49:09 -0600 | [diff] [blame] | 220 | relloc="${loc##$destdir/}" |
| 221 | rel=$(relpath "$linksrc" "$relloc") |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 222 | |
Stefan Esser | 8817139 | 2019-04-19 10:06:46 +0200 | [diff] [blame] | 223 | if [ ! -f "$destdir/$linksrc" ]; then |
Gavin Howard | ed5e79c | 2019-04-22 12:58:40 -0600 | [diff] [blame] | 224 | gencatfile "$destdir/$linksrc" "$linkdir/$link" |
Gavin Howard | d31b568 | 2019-04-09 17:22:37 -0600 | [diff] [blame] | 225 | fi |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 226 | |
Gavin Howard | 66b947c | 2019-08-12 14:19:37 -0600 | [diff] [blame] | 227 | ln -fs "$rel" "$loc" |
Stefan Esser | c6e1814 | 2019-04-09 23:39:30 +0200 | [diff] [blame] | 228 | fi |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 229 | |
| 230 | done |