-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
oraji
514 lines (447 loc) · 16.7 KB
/
oraji
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/bash
project="oraji"
version="4.1.0"
author="Md. Jahidul Hamid"
source="https://github.com/neurobin/$project"
bug_report="$source/issues"
version_info="
********************** $project **********************
Version : $version
Author : $author
Source : $source
Bug report : $bug_report
"
help="
########################## $project #############################
# Usage:
# install java : sudo oraji 'jdk_or_jre_archive_path'
# install java : sudo oraji -i 'jdk_or_jre_archive_path'
# uninstall java : sudo oraji -u jdk|jre<version_number>
#
# Options:
# [ -v, --version ] : Show version
# [ -h, --help ] : Show help
# [ -i, --install ] : Install from a archive path
# [ -u, --uninstall ] : Uninstall java
# [ -s, --set ] : Set a java version
# [ -c, --check ] : Show the installed java versions
##############################################################
"
shopt -s extglob
####################################### General functions ######################################
err(){
echo "E: oraji: $@" >> /dev/stderr
}
chkroot(){
if [ x$EUID != x0 ]; then
err "
This script must be run with root privilege
*Sorry! Abort!**
"
exit 1
fi
}
expandPath() {
local path
local -a pathElements resultPathElements
IFS=':' read -r -a pathElements <<<"$1"
: "${pathElements[@]}"
for path in "${pathElements[@]}"; do
: "$path"
case "$path" in
"~+"/*)
path="$PWD/${path#"~+/"}"
;;
"~-"/*)
path="$OLDPWD/${path#"~-/"}"
;;
"~"/*)
path="$HOME/${path#"~/"}"
;;
"~"*)
old="$path"
username="${path%%/*}"
username="${username#"~"}"
if [ x"$username" = x ];then
path="$HOME"
else
IFS=: read _ _ _ _ _ homedir _ < <(getent passwd "$username")
if [[ $path = */* ]]; then
path="${homedir}/${path#*/}"
else
path="$homedir"
fi
if [ x"$path" = x ]; then
path="$old"
fi
fi
;;
esac
resultPathElements+=( "$path" )
done
local result
printf -v result '%s:' "${resultPathElements[@]}"
printf '%s\n' "${result%:}"
}
####################################### General functions end ######################################
################# Path Managing functions #####################################
disableJavaPath(){
jpath="$1"
pat="$(echo "$jpath" |sed -e 's/[^^]/[&]/g' -e 's/\^/\\^/g')"
sed -e "s/^[[:blank:]]*export[[:blank:]]*J2SDKDIR[=]$pat/# &/" \
-e "s/^[[:blank:]]*export[[:blank:]]*J2REDIR[=]$pat/# &/" \
-e "s/^[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:]$pat/# &/" \
-e "s/^[[:blank:]]*export[[:blank:]]*JAVA_HOME[=]$pat/# &/" \
-e "s/^[[:blank:]]*export[[:blank:]]*DERBY_HOME[=]$pat/# &/" \
--in-place /etc/profile
}
enableJavaPath(){
jpath="$1"
pat="$(echo "$jpath" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
sed -e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*J2SDKDIR[=]$pat.*\)/\1/g" \
-e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*J2REDIR[=]$pat.*\)/\1/g" \
-e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*PATH[=]\$PATH[:]$pat.*\)/\1/g" \
-e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*JAVA_HOME[=]$pat.*\)/\1/g" \
-e "s/^[[:blank:]]*#[[:blank:]]*\(export[[:blank:]]*DERBY_HOME[=]$pat.*\)/\1/g" \
--in-place /etc/profile
}
deleteJavaPath(){
jpath="$1"
[[ ! $jpath =~ ^/usr/lib/jvm/j(dk|re).* ]] && { err "E: argument to deletePath is invalid"; exit 1; }
pat="$(echo "$jpath" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
sed -e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*J2SDKDIR[=]$pat/d" \
-e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*J2REDIR[=]$pat/d" \
-e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:]$pat/d" \
-e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*JAVA_HOME[=]$pat/d" \
-e "/^[[:blank:]]*#*[[:blank:]]*export[[:blank:]]*DERBY_HOME[=]$pat/d" \
--in-place /etc/profile
}
disableOtherJavaPaths(){
sed -e "s/^[[:blank:]]*export[[:blank:]]*PATH[=]\$PATH[:][/]usr[/]lib[/]jvm[/]j\(dk\|re\)[[:digit:]_.]*/# &/g" \
-e "s/^[[:blank:]]*export[[:blank:]]*J2SDKDIR[=][/]usr[/]lib[/]jvm[/]j\(dk\|re\)[[:digit:]_.]*/# &/g" \
-e "s/^[[:blank:]]*export[[:blank:]]*J2REDIR[=][/]usr[/]lib[/]jvm[/]j\(dk\|re\)[[:digit:]_.]*/# &/g" \
-e "s/^[[:blank:]]*export[[:blank:]]*JAVA_HOME[=][/]usr[/]lib[/]jvm[/]j\(dk\|re\)[[:digit:]_.]*/# &/g" \
-e "s/^[[:blank:]]*export[[:blank:]]*DERBY_HOME[=][/]usr[/]lib[/]jvm[/]j\(dk\|re\)[[:digit:]_.]*/# &/g" \
--in-place /etc/profile
}
installJavaPaths(){
jname="$1"
echo "
...updating JAVA_HOME and other environment variables..."
# But first disable other Java environment variables
disableOtherJavaPaths
h1="export J2SDKDIR=/usr/lib/jvm/$jname"
h2="export J2REDIR=/usr/lib/jvm/$jname/jre"
if [[ $jname =~ ^jdk ]];then
h3="export PATH=\$PATH:/usr/lib/jvm/$jname/bin:/usr/lib/jvm/$jname/db/bin:/usr/lib/jvm/$jname/jre/bin"
else
h3="export PATH=\$PATH:/usr/lib/jvm/$jname/bin"
fi
h4="export JAVA_HOME=/usr/lib/jvm/$jname"
h5="export DERBY_HOME=/usr/lib/jvm/$jname/db"
cpat="\([[:blank:]]*[#][[:blank:]]*\|[[:blank:]]*\)"
pat1="$cpat$(echo "$h1" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
pat2="$cpat$(echo "$h2" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
pat3="$cpat$(echo "$h3" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
pat4="$cpat$(echo "$h4" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
pat5="$cpat$(echo "$h5" |sed 's/[^^]/[&]/g; s/\^/\\^/g')"
if [[ $jname =~ ^jdk ]];then
#Delete duplicate entry
sed -e "/^$pat1/d" \
-e "/^$pat2/d" \
-e "/^$pat3/d" \
-e "/^$pat4/d" \
-e "/^$pat5/d" \
--in-place /etc/profile
#Add new entry
sed -e "\$s#\$#\n$h1#" \
-e "\$s#\$#\n$h2#" \
-e "\$s#\$#\n$h3#" \
-e "\$s#\$#\n$h4#" \
-e "\$s#\$#\n$h5#" \
--in-place /etc/profile
else
#Delete duplicate
sed -e "/^$pat3/d" \
-e "/^$pat4/d" \
--in-place /etc/profile
#Add new
sed -e "\$s#\$#\n$h3#" \
sed -e "\$s#\$#\n$h4#" \
--in-place /etc/profile
fi
(( $? == 0 )) &&
. /etc/profile >/dev/null &&
echo "
***environment variables updated successfully***" ||
err "
---Failed to update environment variables"
}
getJName(){
while IFS= read -r -d $'\0'; do
array+=("$REPLY")
done < <(find /usr/lib/jvm -maxdepth 1 -type d \( -name 'jdk*' -o -name 'jre*' \) -printf "%P\0")
echo "Choice Version" >>/dev/stderr
for((i=0;i<${#array[@]};i++));do
echo "$i. ${array[$i]}" >>/dev/stderr
done
arri=""
while [[ ! "$arri" =~ ^[0-9]+$ ]];do
read -r -p "Enter your choice: " arri
done
echo "${array[arri]}"
}
################# Path Managing functions #####################################
####################################### Uninstall function ########################################
uninstall(){
shopt -s extglob
jname="$1"
[[ $jname == "" ]] && jname="$(getJName)"
#{
#if update-alternatives --display java >/dev/null 2>&1;then
# jname="$(update-alternatives --display java |grep -oE 'points\s*to\s*/usr/lib/jvm/[^/]*')"
# jname="${jname##*/}"
# read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname
#else
# read -e -i "$jname" -p "Enter the java version (with jdk/jre): " jname
#fi
#}
[[ -d /usr/lib/jvm/"$jname" ]] ||
{
err "
-----Directory not found: /usr/lib/jvm/$jname-----
"; exit 3;
}
[[ $jname == "" || $jname =~ \/ ]] && err "
------java name can not be empty or contain /------" && exit 4
##variable jname is safe to use now.
patt="^(jdk|jre).*"
[[ $jname =~ $patt ]] || { err "
------Couldn't recognize java name/version------";exit 5; }
jgenre="${BASH_REMATCH[1]}" ## whether it's jdk or jre
doing=false;
for prog in /usr/lib/jvm/"$jname"/bin/*;do
progn="${prog##*/}"
[[ $progn == *"."* ]] && continue
if [[ -f "$prog" ]];then
update-alternatives --remove "$progn" "/usr/lib/jvm/$jname/bin/$progn"
doing=true
fi
done
##This rm command is perfectly safe as we checked $jname to be non-empty
rm -rf "/usr/lib/jvm/$jname" && echo "
***removed: /usr/lib/jvm/$jname***"
##Let's delete the environment variables for this java version
deleteJavaPath "/usr/lib/jvm/$jname"
(( $? == 0 )) &&
source /etc/profile >/dev/null &&
echo "
***Removed java environment variables***" ||
err "
---Failed to remove java environment variables"
##We will remove mozilla plugins for only the active java version.
if $doing;then
rm ~/.mozilla/plugins/libnpjp2.so && echo "
***Mozilla plugin removed***" ||
err "
---Mozilla plugin not found (ignore this error)---" # optional
fi
echo "
...Checking if a fall-back Java is available..."
##try to restore mozilla plugin and other environment variables for fall-back java (if available)
if update-alternatives --display java >/dev/null 2>&1;then
echo "
***Congrats... Fall-Back java found***"
if echo "$(java -version 2>&1)" |grep -qsi "64-Bit";then
arch=amd64
else
arch=i386 ## detect arch
fi
jname="$(update-alternatives --display java |grep -oE 'points\s*to\s*/usr/lib/jvm/[^/]*')"
jname="${jname##*/}"
## We got a valid jname. Let's restore environment variables for this java version
enableJavaPath "/usr/lib/jvm/$jname"
(( $? == 0 )) &&
source /etc/profile >/dev/null &&
echo "
***Environment variables restored for Fall-Back java***" ||
err "
---Couldn't restore environment variables for Fall-Back java---"
[[ $jname =~ ^jdk ]] && ln -sf /usr/lib/jvm/$jname/jre/lib/$arch/libnpjp2.so ~/.mozilla/plugins/ ||
{
[[ $jname =~ ^jre ]] && ln -sf /usr/lib/jvm/$jname/lib/$arch/libnpjp2.so ~/.mozilla/plugins/
} && echo "
***Mozilla plugin restored for fall back java version***" ||
err "
---Couldn't restore mozilla plugin. Restore it manually if needed.
For jdk: ln -sf /usr/lib/jvm/jdk<version_number>/jre/lib/your_arch/libnpjp2.so ~/.mozilla/plugins/
For jre: ln -sf /usr/lib/jvm/jre<version_number>/lib/your_arch/libnpjp2.so ~/.mozilla/plugins/"
echo "
Fall-Back java version info:
"
cd ~ && java -version
else
echo "
***No Fall-Back java found***"
fi
echo "
******Uninstall Finished******"
}
####################################### Uninstall function ########################################
####################################### Opt parse ######################################################
ustall=false
ustall_jname=""
set=false
sname=""
jpath=""
while (( $# > 0 )); do
case "$1" in
-v|--version)
echo "$version_info"
exit 0
;;
-h|--help)
echo "$help"
exit 0
;;
-c|--check)
echo "
Current Java:
"
update-alternatives --display java 2>&1 |grep 'point' |sed -n 's#.*/\(j\(dk\|re\)[^/]*\)/.*#\1#p'
echo "
Available Java:
"
find /usr/lib/jvm -maxdepth 1 -type d \( -name 'jdk*' -o -name 'jre*' \) -printf "%P\n"
exit 0
;;
-u|--ustall)
$set && { err "More than one action is not permitted"; exit 1; }
ustall=true
ustall_jname="$2"
[[ "$jpath" != "" || "$ustall_jname" = -* ]] && { err "Invalid arg for uninstall option"; exit 1; }
chkroot
uninstall "$ustall_jname"
exit 0
shift
;;
-s|--set)
$ustall && { err "More than one action is not permitted"; exit 1; }
set=true
if [[ "$2" != "" ]]; then
sname="$2"
shift
else
sname="$(getJName)"
fi
;;
-i|--install|*)
$ustall && { err "Can't install and uninstall at the same time, choose one."; exit 1; }
$set && { err "Can't set and install at the same time, choose one."; exit 1; }
if [[ "$1" = -i || "$1" = --install ]];then
[[ "$jpath" = "" ]] && jpath="$2" || { err "More than one archive path given"; exit 1; }
shift
else
[[ "$jpath" = "" ]] && jpath="$1" || { err "More than one archive path given"; exit 1; }
fi
[[ "$jpath" = "" ]] && { err "Archive path missing. Usage: oraji -i archive_path"; exit 1; }
esac
shift
done
####################################### Opt parse ######################################################
##################
## check for root
chkroot
if ! $set; then
[[ $jpath == "" ]] && read -e -p "Enter the java archive path: " jpath && jpath="$(expandPath "$jpath")"
##Validate jpath
[[ ! $jpath =~ .*(jdk|jre).*\.tar\..* ]] && { err "Failed to recognize archive: $jpath";exit 2;}
[[ -d $jpath ]] && { err "You must pass an archive file, not a directory";exit 2; }
[[ ! -f $jpath ]] && { err "No such file: $jpath";exit 2;}
tmpdir="$(mktemp -d)"
tar -xvf "$jpath" -C "$tmpdir" || { err "Failed to extract archive"; exit 2; }
cd "$tmpdir"/j* || { err "Couldn't find java directory"; exit 3; }
jname="${PWD##*/}"
[[ $jname != "" && $tmpdir != "" ]] ||
{ err "
-----Variable jname/tmpdir can not be empty-----
Please file a bug report with all available terminal output and info at:
$bug_report
"; exit 4; }
mkdir -p /usr/lib/jvm/"$jname"
mv -f ./* -t /usr/lib/jvm/"$jname"
else
jname="$sname"
fi
#### File copied successfully
while [[ "$jname" = "" ]]; do
read -p "Enter the java version (with jdk/jre): " jname
done
patt="^(jdk|jre).*"
[[ $jname =~ $patt ]] || { err "Couldn't recognize java name/version";exit 5; }
jgenre="${BASH_REMATCH[1]}" ## whether it's jdk or jre
pr=22222 ## priority
if update-alternatives --display java >/dev/null 2>&1;then
oldIFS="$IFS"
IFS=$'\n' arr=($(update-alternatives --display java |grep -oE 'priority\s+[0-9]+' |grep -oE '[0-9]+' |sort -u))
IFS="$oldIFS"
## Find an upper priority
max=0
for n in "${arr[@]}" ; do
((n > max)) && max=$n
done
((pr<=max)) && pr=$((max+1))
fi
##Now install all available binaries with `update-alternatives --install` and set them to be used
[[ -d "/usr/lib/jvm/$jname" ]] || { err "No such directory: /usr/lib/jvm/$jname"; exit 1; }
for prog in /usr/lib/jvm/"$jname"/bin/*;do
progn="${prog##*/}"
[[ $progn == *"."* ]] && continue
if [[ -f $prog ]];then
update-alternatives --install "/usr/bin/$progn" "$progn" "/usr/lib/jvm/$jname/bin/$progn" $pr
chmod a+x "/usr/bin/$progn"
update-alternatives --set "$progn" "/usr/lib/jvm/$jname/bin/$progn"
fi
done
chown -R root:root /usr/lib/jvm/"$jname"
[[ $tmpdir != "" ]] && rm -rf "$tmpdir"
# /etc/init.d/apparmor restart # not needed
##Basic installation finished
cd ~ # needed to run java -version
if echo "$(java -version 2>&1)" |grep -qsi "64-Bit";then
arch=amd64
else
arch=i386 ## detect arch
fi
jv="${jname/#jre/}"
jv="${jv/#jdk/}" ##extracting java version from jname
if echo "$(java -version 2>&1)" |grep -qsi "java version.*$jv" ; then
##Install new paths
installJavaPaths "$jname"
echo "
...Managing mozilla plugin..."
mkdir -p ~/.mozilla/plugins
if [[ $jgenre == "jdk" ]];then
ln -sf /usr/lib/jvm/"$jname"/jre/lib/"$arch"/libnpjp2.so ~/.mozilla/plugins/
else
ln -sf /usr/lib/jvm/"$jname"/lib/"$arch"/libnpjp2.so ~/.mozilla/plugins/
fi
(( $? == 0)) && echo "
***Managed mozilla plugin successfully***" ||
err "
---Failed to manage mozilla plugin. If you think it's necessary run
ln -sf /usr/lib/jvm/$jname/jre/lib/your-system-architecture-here/libnpjp2.so ~/.mozilla/plugins/ #For jdk and
ln -sf /usr/lib/jvm/$jname/lib/your-system-architecture-here/libnpjp2.so ~/.mozilla/plugins/ #For jre
manually, otherwise ignore."
echo "
*********************Installaion Finished*********************
***************Showing Java version information***************
"
java -version
printf "%s\n" "
*******************See if everything is OK********************
(To populate environment variables immediately run 'source /etc/profile' manually)
"
else
err "-----Failed to install java. Run 'java -version' manually to confirm.----"
fi