-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·112 lines (102 loc) · 3.28 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
# shellcheck disable=SC2064
# Global variables
PROGRAM="$0"
# Set caution flags
set -o nounset
set -o errtrace
set -o errexit
set -o pipefail
# Logs an error and exits (code 1)
die() {
echo "ERROR:" "$@" >&2
exit 1
}
print_os() {
case $(uname -s) in
"Linux") echo -n "linux" ;;
*) echo -n "" ;;
esac
}
print_arch() {
case $(uname -m) in
"amd64"|"x86_64") echo -n "amd64" ;;
"arm64"|"aarch64") echo -n "arm64" ;;
"armv6l") echo -n "armv6" ;;
"armv7l") echo -n "armv7" ;;
*) echo -n "" ;;
esac
}
print_libc() {
case "${OSTYPE:-}" in
*-gnu) echo -n "gnu" ;;
*-musl) echo -n "musl" ;;
*) echo -n "" ;;
esac
}
# Downloads a file from a URL
download_url() {
local FILE="$1" URL="$2"
echo "# Downloading ${URL}..."
if command -v curl > /dev/null ; then
curl --silent --location --fail -o "${FILE}" "${URL}"
elif command -v wget > /dev/null ; then
wget --quiet -O "${FILE}" "${URL}"
elif command -v aria2c > /dev/null ; then
aria2c -o "${FILE}" "${URL}"
elif command -v pget > /dev/null ; then
pget -o "${FILE}" "${URL}"
else
die "couldn't locate curl, wget or similar command"
fi
}
# Downloads and unpacks the source files
download_files() {
if [[ $(basename "${PROGRAM}") == 'install.sh' ]] ; then
cd "$(dirname "${PROGRAM}")"
else
local DIR URL OS ARCH LIBC BASE="https://github.com/baraverkstad/upstate"
DIR=$(mktemp --tmpdir --directory upstate-install-XXXXXXXX)
trap "rm -rf ${DIR}" EXIT
cd "${DIR}"
OS=$(print_os)
ARCH=$(print_arch)
LIBC=$(print_libc)
if [[ -z "${VERSION:-}" ]] ; then
URL="${BASE}/releases/latest/download/upstate-${OS:-*}-${ARCH:-*}-${LIBC:-*}.zip"
else
URL="${BASE}/releases/download/${VERSION}/upstate-${OS:-*}-${ARCH:-*}-${LIBC:-*}.zip"
fi
download_url upstate.zip "${URL}" || die "failed to download URL"
command -v unzip > /dev/null || die "couldn't locate unzip command"
unzip -q -u -o upstate.zip
if [[ -d "upstate" || -d "upstate-main" || -d "upstate-${VERSION:-latest}" ]] ; then
cd upstate*
fi
fi
}
# Installs the source files
install_files() {
echo "Installing to /usr/local/bin/..."
if [[ -f bin/upstate ]] ; then
install -D bin/upstate /usr/local/bin/upstate
else
install -D bin/upstate.sh /usr/local/bin/upstate
fi
echo "Installing to /usr/local/share/man/..."
install -D --mode=644 man/man1/upstate.1 /usr/local/share/man/man1/upstate.1
gzip -f /usr/local/share/man/man1/upstate.1
echo "Installing to /usr/local/share/upstate/..."
install -D --mode=644 etc/upstate.conf /usr/local/share/upstate/upstate.conf
if [[ ! -r /etc/upstate.conf ]] && [[ ! -r /usr/local/etc/upstate.conf ]] ; then
install -D --mode=644 etc/upstate.conf /usr/local/etc/upstate.conf
echo
echo "An example /usr/local/etc/upstate.conf file has been installed."
echo "Please edit to match your server configuration."
fi
}
[[ $(whoami) == 'root' ]] || die "only root can run the installation"
download_files
install_files
echo
echo "Done! Upstate now installed!"