blob: 3c466258273513a3c9cf1f166d3065065ff42d86 [file] [log] [blame]
Gavin Howard50080252019-02-20 15:27:43 -07001#! /bin/sh
2#
Gavin Howard7345cb92019-04-08 14:13:43 -06003# Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard50080252019-02-20 15:27:43 -07004#
Gavin Howard7345cb92019-04-08 14:13:43 -06005# All rights reserved.
Gavin Howard50080252019-02-20 15:27:43 -07006#
Gavin Howard7345cb92019-04-08 14:13:43 -06007# 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 Howard50080252019-02-20 15:27:43 -070028#
29
30usage() {
Gavin Howard08727ac2019-04-17 17:16:42 -060031 printf "usage: %s NLSPATH main_exec [DESTDIR]\n" "$0" 1>&2
Gavin Howard50080252019-02-20 15:27:43 -070032 exit 1
33}
34
Gavin Howardd31b5682019-04-09 17:22:37 -060035gencatfile() {
36
Gavin Howard62421e92019-04-17 12:56:30 -060037 _gencatfile_loc="$1"
Gavin Howardd31b5682019-04-09 17:22:37 -060038 shift
39
Gavin Howard62421e92019-04-17 12:56:30 -060040 _gencatfile_file="$1"
Gavin Howardd31b5682019-04-09 17:22:37 -060041 shift
42
Gavin Howard62421e92019-04-17 12:56:30 -060043 mkdir -p $(dirname "$_gencatfile_loc")
44 gencat "$_gencatfile_loc" "$_gencatfile_file" > /dev/null 2>&1
Gavin Howardd31b5682019-04-09 17:22:37 -060045}
46
Gavin Howarded5e79c2019-04-22 12:58:40 -060047localeexists() {
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 Howarde77f6132019-08-08 21:34:39 -060069splitpath() {
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
99relpath() {
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 Howard50080252019-02-20 15:27:43 -0700148script="$0"
149scriptdir=$(dirname "$script")
150
Gavin Howard6e70f552019-04-01 14:09:39 -0600151. "$scriptdir/functions.sh"
Gavin Howard50080252019-02-20 15:27:43 -0700152
Gavin Howardf680e352019-04-01 13:28:48 -0600153test "$#" -ge 2 || usage
Gavin Howard50080252019-02-20 15:27:43 -0700154
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600155nlspath="$1"
Gavin Howard50080252019-02-20 15:27:43 -0700156shift
157
Gavin Howardf680e352019-04-01 13:28:48 -0600158main_exec="$1"
159shift
Gavin Howard50080252019-02-20 15:27:43 -0700160
Gavin Howard08727ac2019-04-17 17:16:42 -0600161if [ "$#" -ge 1 ]; then
162 destdir="$1"
163 shift
164else
165 destdir=""
166fi
167
168"$scriptdir/locale_uninstall.sh" "$nlspath" "$main_exec" "$destdir"
Gavin Howard00382e92019-04-11 12:56:02 -0600169
Gavin Howard5d0d10e2019-04-05 13:53:50 -0600170locales_dir="$scriptdir/locales"
Gavin Howard50080252019-02-20 15:27:43 -0700171
Gavin Howarded5e79c2019-04-22 12:58:40 -0600172# 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.
174if [ "$destdir" = "" ]; then
175 locales=$(locale -a)
176else
177 locales=$(locale -m)
178fi
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600179
Gavin Howard5d0d10e2019-04-05 13:53:50 -0600180for file in $locales_dir/*.msg; do
Gavin Howard50080252019-02-20 15:27:43 -0700181
Gavin Howard80ba7c92019-04-09 17:22:53 -0600182 locale=$(basename "$file" ".msg")
Gavin Howard08727ac2019-04-17 17:16:42 -0600183 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
Gavin Howardf680e352019-04-01 13:28:48 -0600184
Gavin Howarded5e79c2019-04-22 12:58:40 -0600185 localeexists "$locales" "$locale" "$destdir"
186 err="$?"
187
188 if [ "$err" -eq 0 ]; then
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600189 continue
190 fi
191
Gavin Howard50080252019-02-20 15:27:43 -0700192 if [ -L "$file" ]; then
Stefan Esserc6e18142019-04-09 23:39:30 +0200193 continue
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600194 fi
195
Gavin Howardd31b5682019-04-09 17:22:37 -0600196 gencatfile "$loc" "$file"
Gavin Howard50080252019-02-20 15:27:43 -0700197
198done
Stefan Esserc6e18142019-04-09 23:39:30 +0200199
200for file in $locales_dir/*.msg; do
201
Gavin Howard80ba7c92019-04-09 17:22:53 -0600202 locale=$(basename "$file" ".msg")
Gavin Howard08727ac2019-04-17 17:16:42 -0600203 loc=$(gen_nlspath "$destdir/$nlspath" "$locale" "$main_exec")
Gavin Howard50080252019-02-20 15:27:43 -0700204
Gavin Howarded5e79c2019-04-22 12:58:40 -0600205 localeexists "$locales" "$locale" "$destdir"
206 err="$?"
207
208 if [ "$err" -eq 0 ]; then
Gavin Howard50080252019-02-20 15:27:43 -0700209 continue
210 fi
211
212 mkdir -p $(dirname "$loc")
213
214 if [ -L "$file" ]; then
Gavin Howard50080252019-02-20 15:27:43 -0700215
Stefan Esserc6e18142019-04-09 23:39:30 +0200216 link=$(readlink "$file")
Gavin Howarded5e79c2019-04-22 12:58:40 -0600217 linkdir=$(dirname "$file")
Stefan Esserc6e18142019-04-09 23:39:30 +0200218 locale=$(basename "$link" .msg)
Gavin Howardd31b5682019-04-09 17:22:37 -0600219 linksrc=$(gen_nlspath "$nlspath" "$locale" "$main_exec")
Gavin Howard6c12e6a2019-08-12 15:49:09 -0600220 relloc="${loc##$destdir/}"
221 rel=$(relpath "$linksrc" "$relloc")
Gavin Howardd31b5682019-04-09 17:22:37 -0600222
Stefan Esser88171392019-04-19 10:06:46 +0200223 if [ ! -f "$destdir/$linksrc" ]; then
Gavin Howarded5e79c2019-04-22 12:58:40 -0600224 gencatfile "$destdir/$linksrc" "$linkdir/$link"
Gavin Howardd31b5682019-04-09 17:22:37 -0600225 fi
Stefan Esserc6e18142019-04-09 23:39:30 +0200226
Gavin Howard66b947c2019-08-12 14:19:37 -0600227 ln -fs "$rel" "$loc"
Stefan Esserc6e18142019-04-09 23:39:30 +0200228 fi
Gavin Howard50080252019-02-20 15:27:43 -0700229
230done