-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjava-alternatives.sh
executable file
·339 lines (294 loc) · 5.72 KB
/
java-alternatives.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
# allow non-.deb version of Java from Sun to be managed by Debian's alteratives system
# so that we can also install .deb versions on the same system
# Mikel Ward <mikel@mikelward.com>
###
# default values for options
#
basedir=
priority=10
subdirs="bin man"
bindir=/usr/bin
mandir=/usr/share/man
dirmap="bin=$bindir:man=$mandir"
debug=false
simulate=false
verbose=false
###
# functions for error handling, usage, and command line option handling
#
debug()
{
$debug && echo "$*" >&2
}
error()
{
echo "$*" >&2
}
info()
{
$verbose && echo "$*" >&2
}
notice()
{
echo "$*" >&2
}
run()
{
if $simulate || $debug; then
# IFS is used to preserve newlines in --slaves output
OIFS=$IFS
IFS=
echo "$@" >&2
IFS=$OIFS
fi
if ! $simulate; then
"$@"
fi
}
usage()
{
cat <<EOF >&2
Usage: java-alternatives.sh [-dhnv] [-p <priority>] [<java root>]
Example: java-alternatives.sh /usr/jdk1.5.0_22
EOF
}
###
# process the command line
#
while getopts ":dhnp:v" option
do
case $option in
d)
debug=true
;;
h)
usage
exit 0
;;
n)
simulate=true
;;
p)
priority=$OPTARG
;;
v)
verbose=true
;;
':')
error "Missing argument to -$OPTARG"
usage
exit 2
;;
'?')
error "Invalid option -$OPTARG"
usage
exit 2
;;
*)
error "The -$option option is not supported yet"
usage
exit 2
;;
esac
done
shift $((OPTIND - 1))
basedir=$1
if test -z "$basedir"; then
error "No Java root directory specified"
usage
exit 2
fi
debug "basedir is $basedir"
names=
###
# common array handling functions
#
# add_to_array <array name> <value>
# this is not very efficient for large arrays
# uses newlines to separate entries to allow for storing file names containing spaces
add_to_array()
{
OIFS=$IFS
IFS='
'
local array=$1
local value=$2
eval $array="\${$array:+\$$array
}\$value"
IFS=$OIFS
}
# in_array <array name> <value>
in_array()
{
OIFS=$IFS
IFS='
'
local array=$1
local value=$2
local found=0
for element in ${!array}; do
if test "$element" = "$value"; then
found=1
break
fi
done
IFS=$OIFS
if test $found -eq 1; then
return 0
else
return 1
fi
}
###
# functions for this program
#
# determine where the symlink should be,
# e.g. for /usr/jdk1.5.0_22/bin/java return /usr/bin/java
get_destination_path()
{
local base=$1
local path=$2
local dirmap=$3
# strip any trailing slash for consistency
base=${base%/}
if test "$base" = ""; then
error "Cannot determine directory for $path: root directory?"
return 1
fi
local subdir=${path#$base}
if test "$subdir" = "$path"; then
error "Cannot determine subdirectory for $path"
return 1
fi
subdir=${subdir#/}
if test "$subdir" = ""; then
error "Unexpected empty directory: root directory?"
return 1
fi
subdir=${subdir%%/*}
if test "$subdir" = ""; then
error "Unexpected empty directory: //?"
return 1
fi
dirmap=$(get_mapped_directory $subdir $dirmap)
if test $? -eq 0 -a -n "$dirmap"; then
local dest
dest=${path#$base}
dest=${dest#/$subdir}
dest=$dirmap$dest
echo $dest
return 0
else
error "Cannot get target directory for $path"
return 1
fi
}
# given a bare subdirectory,
# return where files from that subdirectory
# should be installed
# e.g. "bin" => "/usr/bin"
# at the moment, it only works for an exact match on the first pathname component
# this may change later
get_mapped_directory()
{
local subdir=$1
local dirmap=$2
local result=
OIFS=$IFS
IFS=:
debug "Getting mapped directory for $subdir"
local dir
for dir in $dirmap; do
local key
key=${dir%%=*}
if test "$key" = "$dir"; then
error "Skipping invalid dirmap entry $dir"
continue
fi
if test "$subdir" = "$key"; then
local val
val=${dir#$key=}
if test "$val" = "$dir"; then
error "Skipping invalid dirmap entry $dir with key=$key"
continue
else
debug "$subdir -> $val"
result=$val
break
fi
fi
done
IFS=$OIFS
if test -n "$result"; then
echo $result
return 0
else
notice "Missing dirmap for $subdir"
return 1
fi
}
###
# main
#
# determine where each file (program, man page, etc.)
# should be installed to and build the command line args to
# update-alternatives --install...
files=$(for subdir in $subdirs; do find $basedir/$subdir -type f -print; done)
for file in $files; do
# We might get /usr/java/share/man/man1/java.1 and
# /usr/java/share/man/ja_JP.eucJP/man1/java.1.
# We can only have one or the other, because both would
# be called /etc/alternatives/java.1.
# There's no guarantee these are passed to us in any given order,
# so naively skip anything that doesn't look like a default
# man page.
case $file in */man/*)
case $file in */man/man*)
# keep default locale man pages
:
;;
*)
info "Skipping $file: prefer default locale man pages"
continue
;;
esac
;;
esac
name=${file##*/}
if test "$name" = "$file"; then
error "Cannot determine filename for $file"
continue
fi
dest=$(get_destination_path $basedir $file $dirmap)
if test -z "$dest"; then
error "Cannot determine target for $file"
continue
fi
if ! in_array names $name; then
add_to_array names $name
case $file in */bin/java)
# the main java program should be specified via --install
install=" --install $dest $name $file"
;;
*)
# the other files are specified via --slave
# weird spacing is to make it line up with --install line in output
slaves="$slaves
--slave $dest $name $file"
;;
esac
else
notice "Skipping $file: already in use"
fi
done
# TODO: bin/java should honor $dirmap too
# $slaves already has a newline before it, so don't add one here too
if $simulate; then
echo "/usr/sbin/update-alternatives
$install $priority $slaves"
else
run sudo /usr/sbin/update-alternatives \
$install $priority $slaves
fi
# vim: set ts=4 sw=4 tw=0 et: