]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bc/locale_install.sh
Update to version 3.1.1
[FreeBSD/FreeBSD.git] / contrib / bc / locale_install.sh
1 #! /bin/sh
2 #
3 # SPDX-License-Identifier: BSD-2-Clause
4 #
5 # Copyright (c) 2018-2020 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 usage() {
31         printf "usage: %s NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2
32         exit 1
33 }
34
35 gencatfile() {
36
37         _gencatfile_loc="$1"
38         shift
39
40         _gencatfile_file="$1"
41         shift
42
43         mkdir -p $(dirname "$_gencatfile_loc")
44         gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1
45 }
46
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
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
148 script="$0"
149 scriptdir=$(dirname "$script")
150
151 . "$scriptdir/functions.sh"
152
153 test "$#" -ge 2 || usage
154
155 nlspath="$1"
156 shift
157
158 main_exec="$1"
159 shift
160
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"
169
170 locales_dir="$scriptdir/locales"
171
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
179
180 for file in $locales_dir/*.msg; do
181
182         locale=$(basename "$file" ".msg")
183         loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
184
185         localeexists "$locales" "$locale" "$destdir"
186         err="$?"
187
188         if [ "$err" -eq 0 ]; then
189                 continue
190         fi
191
192         if [ -L "$file" ]; then
193                 continue
194         fi
195
196         gencatfile "$loc" "$file"
197
198 done
199
200 for file in $locales_dir/*.msg; do
201
202         locale=$(basename "$file" ".msg")
203         loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
204
205         localeexists "$locales" "$locale" "$destdir"
206         err="$?"
207
208         if [ "$err" -eq 0 ]; then
209                 continue
210         fi
211
212         mkdir -p $(dirname "$loc")
213
214         if [ -L "$file" ]; then
215
216                 link=$(readlink "$file")
217                 linkdir=$(dirname "$file")
218                 locale=$(basename "$link" .msg)
219                 linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")
220                 relloc="${loc##$destdir/}"
221                 rel=$(relpath "$linksrc" "$relloc")
222
223                 if [ ! -f "$destdir/$linksrc" ]; then
224                         gencatfile "$destdir/$linksrc" "$linkdir/$link"
225                 fi
226
227                 ln -fs "$rel" "$loc"
228         fi
229
230 done