-
Notifications
You must be signed in to change notification settings - Fork 4
/
install.sh
executable file
·142 lines (130 loc) · 3.54 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
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
#!/bin/bash
set -e
install_client=0
install_node=0
while (( "$#" )); do
case "$1" in
--client)
install_client=1
shift
;;
--node)
install_node=1
shift
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
done
if [[ $EUID -eq 0 ]]; then
running_as_root=1
else
running_as_root=0
fi
function print_banner() {
echo "**************************************"
echo "* *"
echo "* Installing safeup *"
echo "* *"
echo "**************************************"
}
function detect_os() {
os=$(uname -s)
case "$os" in
Linux*) os=linux ;;
Darwin*) os=mac ;;
*) echo "Unknown operating system"; exit 1 ;;
esac
}
function detect_arch() {
arch=$(uname -m)
case "$arch" in
x86_64*)
if [[ $os == "mac" ]]; then
arch_triple="x86_64-apple-darwin"
else
arch_triple="x86_64-unknown-$os-musl"
fi
;;
aarch64*)
if [[ $os == "mac" ]]; then
arch_triple="aarch64-apple-darwin"
else
arch_triple="aarch64-unknown-$os-musl"
fi
;;
arm64*)
if [[ $os == "mac" ]]; then
echo "Mac arm64 architecture not supported, installing x86_64 version"
arch_triple="x86_64-apple-darwin"
else
arch_triple="aarch64-unknown-$os-musl"
fi
;;
armv7*) arch_triple="armv7-unknown-$os-musleabihf" ;;
*) echo "Architecture $arch not supported"; exit 1 ;;
esac
echo "Will retrieve safeup for $arch_triple architecture"
}
function get_latest_version() {
release_data=$(curl --silent "https://api.github.com/repos/maidsafe/safeup/releases/latest")
version=$(echo "$release_data" | awk -F': ' '/"tag_name":/ {print $2}' | \
sed 's/"//g' | sed 's/,//g' | sed 's/v//g')
download_url=$(echo "$release_data" | \
awk -F': ' '/"browser_download_url":/ {print $2 $3}' | \
grep "safeup-$version-$arch_triple.tar.gz" | sed 's/"//g' | sed 's/,//g')
echo "Latest version of safeup is $version"
}
function install_safeup() {
if [[ $running_as_root -eq 1 ]]; then
target_dir="/usr/local/bin"
else
target_dir="$HOME/.local/bin"
mkdir -p "$target_dir"
mkdir -p "$HOME/.config/safe"
cat << 'EOF' > ~/.config/safe/env
#!/bin/sh
case ":${PATH}:" in
*:"$HOME/.local/bin":*)
;;
*)
export PATH="$HOME/.local/bin:$PATH"
;;
esac
EOF
echo "source $HOME/.config/safe/env" >> "$HOME/.bashrc"
fi
temp_dir=$(mktemp -d)
curl -L "$download_url" -o "$temp_dir/safeup.tar.gz"
tar -xzf "$temp_dir/safeup.tar.gz" -C "$temp_dir"
mv "$temp_dir/safeup" "$target_dir/safeup"
chmod +x "$target_dir/safeup"
rm -rf "$temp_dir"
echo "safeup installed to $target_dir/safeup"
}
function post_install() {
if [[ $install_client -eq 1 ]]; then
echo "Now running safeup to install the safe client..."
$target_dir/safeup client
fi
if [[ $install_node -eq 1 ]]; then
echo "Now running safeup to install safenode..."
$target_dir/safeup node
fi
if [[ $running_as_root -eq 1 ]]; then
echo "Please run 'safeup --help' to see how to install network components."
else
printf "\n"
echo "The safeup binary has been installed, but it's not available in this session."
echo "You must either run 'source ~/.config/safe/env' in this session, or start a new session."
echo "When safeup is available, please run 'safeup --help' to see how to install network components."
fi
}
print_banner
detect_os
detect_arch
get_latest_version
install_safeup
post_install