1#! /bin/sh
2#
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2018-2021 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
30usage() {
31	if [ $# -eq 1 ]; then
32		printf '%s\n' "$1"
33	fi
34	printf "usage: %s NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2
35	exit 1
36}
37
38gencatfile() {
39
40	_gencatfile_loc="$1"
41	shift
42
43	_gencatfile_file="$1"
44	shift
45
46	mkdir -p $(dirname "$_gencatfile_loc")
47	gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1
48}
49
50localeexists() {
51
52	_localeexists_locales="$1"
53	shift
54
55	_localeexists_locale="$1"
56	shift
57
58	_localeexists_destdir="$1"
59	shift
60
61	if [ "$_localeexists_destdir" != "" ]; then
62		_localeexists_char="@"
63		_localeexists_locale="${_localeexists_locale%%_localeexists_char*}"
64		_localeexists_char="."
65		_localeexists_locale="${_localeexists_locale##*$_localeexists_char}"
66	fi
67
68	test ! -z "${_localeexists_locales##*$_localeexists_locale*}"
69	return $?
70}
71
72splitpath() {
73
74	_splitpath_path="$1"
75	shift
76
77	if [ "$_splitpath_path" = "${_splitpath_path#/}" ]; then
78		printf 'Must use absolute paths\n'
79		exit 1
80	fi
81
82	if [ "${_splitpath_path#\n*}" != "$_splitpath_path" ]; then
83		exit 1
84	fi
85
86	_splitpath_list=""
87	_splitpath_item=""
88
89	while [ "$_splitpath_path" != "/" ]; do
90		_splitpath_item=$(basename "$_splitpath_path")
91		_splitpath_list=$(printf '\n%s%s' "$_splitpath_item" "$_splitpath_list")
92		_splitpath_path=$(dirname "$_splitpath_path")
93	done
94
95	if [ "$_splitpath_list" != "/" ]; then
96		_splitpath_list="${_splitpath_list#?}"
97	fi
98
99	printf '%s' "$_splitpath_list"
100}
101
102relpath() {
103
104	_relpath_path1="$1"
105	shift
106
107	_relpath_path2="$1"
108	shift
109
110	_relpath_nl=$(printf '\nx')
111	_relpath_nl="${_relpath_nl%x}"
112
113	_relpath_splitpath1=`splitpath "$_relpath_path1"`
114	_relpath_splitpath2=`splitpath "$_relpath_path2"`
115
116	_relpath_path=""
117	_relpath_temp1="$_relpath_splitpath1"
118
119	IFS="$_relpath_nl"
120
121	for _relpath_part in $_relpath_temp1; do
122
123		_relpath_temp2="${_relpath_splitpath2#$_relpath_part$_relpath_nl}"
124
125		if [ "$_relpath_temp2" = "$_relpath_splitpath2" ]; then
126			break
127		fi
128
129		_relpath_splitpath2="$_relpath_temp2"
130		_relpath_splitpath1="${_relpath_splitpath1#$_relpath_part$_relpath_nl}"
131
132	done
133
134	for _relpath_part in $_relpath_splitpath2; do
135		_relpath_path="../$_relpath_path"
136	done
137
138	_relpath_path="${_relpath_path%../}"
139
140	for _relpath_part in $_relpath_splitpath1; do
141		_relpath_path="$_relpath_path$_relpath_part/"
142	done
143
144	_relpath_path="${_relpath_path%/}"
145
146	unset IFS
147
148	printf '%s\n' "$_relpath_path"
149}
150
151script="$0"
152scriptdir=$(dirname "$script")
153
154. "$scriptdir/functions.sh"
155
156all_locales=0
157
158while getopts "l" opt; do
159
160	case "$opt" in
161		l) all_locales=1 ; shift ;;
162		?) usage "Invalid option $opt" ;;
163	esac
164
165done
166
167test "$#" -ge 2 || usage
168
169nlspath="$1"
170shift
171
172main_exec="$1"
173shift
174
175if [ "$#" -ge 1 ]; then
176	destdir="$1"
177	shift
178else
179	destdir=""
180fi
181
182"$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir"
183
184locales_dir="$scriptdir/locales"
185
186# What this does is if installing to a package, it installs all locales that
187# match supported charsets instead of installing all directly supported locales.
188if [ "$destdir" = "" ]; then
189	locales=$(locale -a)
190else
191	locales=$(locale -m)
192fi
193
194for file in $locales_dir/*.msg; do
195
196	locale=$(basename "$file" ".msg")
197
198	if [ "$all_locales" -eq 0 ]; then
199
200		localeexists "$locales" "$locale" "$destdir"
201		err="$?"
202
203		if [ "$err" -eq 0 ]; then
204			continue
205		fi
206	fi
207
208	if [ -L "$file" ]; then
209		continue
210	fi
211
212	loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
213
214	gencatfile "$loc" "$file"
215
216done
217
218for file in $locales_dir/*.msg; do
219
220	locale=$(basename "$file" ".msg")
221
222	if [ "$all_locales" -eq 0 ]; then
223
224		localeexists "$locales" "$locale" "$destdir"
225		err="$?"
226
227		if [ "$err" -eq 0 ]; then
228			continue
229		fi
230	fi
231
232	loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
233
234	mkdir -p $(dirname "$loc")
235
236	if [ -L "$file" ]; then
237
238		link=$(readlink "$file")
239		linkdir=$(dirname "$file")
240		locale=$(basename "$link" .msg)
241		linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")
242		relloc="${loc##$destdir/}"
243		rel=$(relpath "$linksrc" "$relloc")
244
245		if [ ! -f "$destdir/$linksrc" ]; then
246			gencatfile "$destdir/$linksrc" "$linkdir/$link"
247		fi
248
249		ln -fs "$rel" "$loc"
250	fi
251
252done
253