-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
install.sh
executable file
·98 lines (85 loc) · 2.41 KB
/
install.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
#!/usr/bin/env bash
set -euo pipefail
BIN=moclojer
REPO='moclojer/moclojer'
INSTALL_DIR="/usr/local/bin"
DOWNLOAD_DIR=''
VERSION=''
# Args
print_help() {
echo "Installs latest (or specific) version of ${BIN}. Installation directory defaults to /usr/local/bin."
echo -e
echo "Usage:"
echo "install [--dir <dir>] [--download-dir <download-dir>] [--version <version>]"
echo -e
echo "Defaults:"
echo " * Installation directory: ${INSTALL_DIR}"
echo " * Download directory: temporary"
echo " * Version: <Latest release on github>"
exit 1
}
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
--dir)
INSTALL_DIR="$2"
shift
shift
;;
--download-dir)
DOWNLOAD_DIr="$2"
shift
shift
;;
--version)
VERSION="$2"
shift
shift
;;
*) # unknown option
print_help
shift
;;
esac
done
if [[ -z "$DOWNLOAD_DIR" ]]; then
DOWNLOAD_DIR="$(mktemp -d)"
trap 'rm -rf "$DOWNLOAD_DIR"' EXIT
fi
if [[ "$VERSION" == "" ]]; then
VERSION=$(curl -s https://api.github.com/repos/${REPO}/releases/latest | grep -o "\"tag_name\": \".*\"" | cut -d'"' -f4)
fi
# Local enviroment
case "$(uname -s)" in
Linux*) PLATFORM=Linux ;;
Darwin*) PLATFORM=macOS ;;
esac
if hash java 2>/dev/null; then
JAVA_BIN=$(which java)
else
echo "Java not found: to install moclojer you need Java installed on your operating system."
exit 1
fi
# Build file name and download link
# TODO: use when you have GraalVM native-image _${PLATFORM}
FILENAME=${BIN}.jar
DOWNLOAD_URL="https://github.com/${REPO}/releases/download/${VERSION}/${FILENAME}"
# Download
# Running this part in a subshell so when it finishes we go back to the previous directory
mkdir -p "$DOWNLOAD_DIR" && (
cd "$DOWNLOAD_DIR"
echo -e "Downloading $DOWNLOAD_URL to $DOWNLOAD_DIR"
curl -o "$FILENAME" -sL "$DOWNLOAD_URL"
)
# Install
if [[ "$DOWNLOAD_DIR" != "$INSTALL_DIR" ]]; then
mkdir -p "$INSTALL_DIR"
if [ -f "$INSTALL_DIR/$BIN" ]; then
echo "Moving $INSTALL_DIR/$BIN to $INSTALL_DIR/$BIN.old"
mv -f "$INSTALL_DIR/$BIN" "$INSTALL_DIR/$BIN.old"
fi
mv -f "$DOWNLOAD_DIR/$FILENAME" "$INSTALL_DIR/$FILENAME"
echo -e "#!/usr/bin/env bash\n${JAVA_BIN} -jar ${INSTALL_DIR}/${FILENAME} \$@" >"$INSTALL_DIR/$BIN"
chmod +x "$INSTALL_DIR/$BIN"
fi
echo "Successfully installed $BIN in $INSTALL_DIR"