-
Notifications
You must be signed in to change notification settings - Fork 0
/
openjdk_portable_downloader.sh
139 lines (120 loc) · 5.89 KB
/
openjdk_portable_downloader.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
#!/bin/bash
################################################################################
# openjdk_portable_downloader #
# #
# Use this script to download latest openjdk binaries #
# #
################################################################################
################################################################################
################################################################################
# #
# Copyright (C) 2020, @rfuehrer #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy #
# of this software and associated documentation files (the "Software"), to deal#
# in the Software without restriction, including without limitation the rights #
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #
# copies of the Software, and to permit persons to whom the Software is #
# furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS #
# IN.THE SOFTWARE. #
# #
################################################################################
################################################################################
################################################################################
OVERWRITE_EXISTING=0
CLEANUP_DESTINATION=1
# relatibe path (from script location) to java storage path
DIR_DESTINATION="./java_portable"
SITE_OPENJDK="https://jdk.java.net/archive/"
# ----------------------- DO NOT EDIT BELOW THIS LINE -----------------------
# set internal vars
DIR_TEMP=$(mktemp -d)
ABSOLUTE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DIR_DESTINATION=$ABSOLUTE_PATH/$DIR_DESTINATION
echo "OpenJDK destination path : $DIR_DESTINATION"
if [[ "$OSTYPE" == "linux-gnu" ]]; then
PLATFORM="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
PLATFORM="osx"
elif [[ "$OSTYPE" == "cygwin" ]]; then
PLATFORM="windows"
echo "Platform not supported at the moment. Abort."
exit 1
elif [[ "$OSTYPE" == "msys" ]]; then
PLATFORM="windows"
echo "Platform not supported at the moment. Abort."
exit 1
elif [[ "$OSTYPE" == "win32" ]]; then
Echo "Platform (32-bit) not supported. Abort."
exit 1
elif [[ "$OSTYPE" == "freebsd"* ]]; then
PLATFORM="linux"
else
echo "Platform can not be detected. Abort."
exit 1
fi
# get latest openjdk link
# get website content
DUMMY1=$(curl -s "$SITE_OPENJDK")
# get all links
DUMMY2=$(echo "$DUMMY1" | grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*(\.tar\.gz)")
# get first link auf all links of specified platform
DOWNLOAD=$(echo "$DUMMY2" | grep "$PLATFORM" | head -n 1)
# download openjdk
SITE_VERSION=$(echo "$DOWNLOAD" | sed -e 's#.*jdk-\(.*\)_.*#\1#' | sed -e 's#^\(.*\)_.*#\1#')
TEMPFILE="$DIR_TEMP/temp.openjdk-$PLATFORM-$SITE_VERSION.tar.gz"
# Cleaup obsolete versions
if [ $CLEANUP_DESTINATION -eq 1 ]; then
echo "Clean up destination dir..."
find "$DIR_DESTINATION/" -mindepth 1 -maxdepth 1 -not -name "openjdk-$PLATFORM-$SITE_VERSION" -print0 | xargs -0 rm -rf
fi
# check if platform/version already exists
if [[ -d "$DIR_DESTINATION/openjdk-$PLATFORM-$SITE_VERSION" ]]; then
if [ $OVERWRITE_EXISTING -eq 0 ]; then
echo "Version seems to exists in destination directory. Abort."
exit 1
else
echo "Overwritung existing version..."
fi
fi
# downloading openjdk
echo "Downloading OpenJDK v.$SITE_VERSION / $PLATFORM. Please wait..."
curl -s -o "$TEMPFILE" "$DOWNLOAD"
# extract openjdk
tar -xf "$TEMPFILE" -C "$DIR_TEMP/" --strip=3
# get downloaded version
VERSION=$("$DIR_TEMP/Home/bin/java" --version | head -n 1 | awk '{ print $2 }')
# prepare destination directory
mkdir -p "$DIR_DESTINATION"
FINALDIR="$DIR_DESTINATION/openjdk-$PLATFORM-$SITE_VERSION"
echo "OS detected : $PLATFORM"
echo "Temporary dir : $DIR_TEMP"
#osascript -e "tell application \"System Events\" to display dialog \"DEBUG: $DIR_TEMP\""
echo "OpenJDK download link : $DOWNLOAD"
echo "OpenJDK link version : $SITE_VERSION"
echo "OpenJDK version : $VERSION"
echo "OpenJDK destination path : $DIR_DESTINATION"
echo "OpenJDK downloaded version: $FINALDIR"
echo "Please press CTRL+C to abort NOW! (waiting 5 seconds)"
sleep 5
echo "Remove old files and dirs..."
# remove old openjdk (if exists)
if [ ! -z "$FINALDIR" ]; then rm -rf "$FINALDIR/"; fi
# copy downloaded openjdk to destination
cp -Rp "$DIR_TEMP/Home" "$FINALDIR"
# copy downloaded tar to destination (as backup; will be deleted at next activated cleanup!)
#cp $TEMPFILE $DIR_DESTINATION/
# remove downloaded/extracted temp parts
if [ ! -z "$DIR_TEMP" ]; then rm -rf "$DIR_TEMP/"; fi
if [ -f "$TEMPFILE" ]; then rm -rf "$TEMPFILE"; fi
echo "Done."