-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·217 lines (189 loc) · 6.13 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
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
#!/usr/bin/env bash
set -e
function make_config {
mkdir -p "$HOME/.config/oo_bin"
}
function install_dependencies {
# Case for WSL and Ubuntu linux
if [[ $(uname -s) =~ "Linux" ]]; then
if which apt-get 2>/dev/null; then
if ! which autossh; then
sudo apt-get -y update && sudo apt-get install -y autossh
fi
if ! which pip3; then
sudo apt-get -y update && sudo apt-get install -y python3-pip
fi
if ! which whois; then
sudo apt-get -y update && sudo apt-get install -y whois
fi
if ! which jq; then
sudo apt-get -y update && sudo apt-get install -y jq
fi
else
echo "We could not automatically install the dependencies on your system. Please install ssh and autossh manually."
fi
elif [[ $(uname -s) =~ "Darwin" ]]; then
if which brew 2>/dev/null; then
if ! which autossh; then
brew install autossh
fi
if [ "$(which python3)" != "/usr/local/bin/python3" ]; then
brew install python
fi
if ! which whois; then
brew install whois
fi
if ! which jq; then
brew install jq
fi
else
echo "Could not find Homebrew (https://brew.sh/). Install Homebrew, or, manually install autossh."
fi
fi
}
function install {
if ls ~/.local/share/oo_bin/*.pkl >/dev/null 2>&1; then
echo ""
echo "The application cannot be updated while tunnels are running. Please stop all tunnels and try again:"
echo ""
echo "oo tunnels stop"
echo "oo --update"
exit 1
fi
if [ $PRERELEASE ]; then
FILENAME=$(curl -L https://api.github.com/repos/outsideopen/oo-bin-py/releases | jq -r 'map(select(.prerelease)) | .[0].assets[0].name')
DOWNLOAD_URL=$(curl -L https://api.github.com/repos/outsideopen/oo-bin-py/releases | jq -r 'map(select(.prerelease)) | .[0].assets[0].browser_download_url')
else
FILENAME=$(curl -L https://api.github.com/repos/outsideopen/oo-bin-py/releases/latest | jq -r '.assets[0].name')
DOWNLOAD_URL=$(curl -L https://api.github.com/repos/outsideopen/oo-bin-py/releases/latest | jq -r '.assets[0].browser_download_url')
fi
curl -LJO $DOWNLOAD_URL
pip3 install --force-reinstall ./"$FILENAME"
rm "$FILENAME"
}
function add_tunnels_config_download {
if ! grep -s -q "\[tunnels\.update\]" $HOME/.config/oo_bin/config.toml; then
read -p "Enable remote tunnels config update? (Y/N): " CONFIRM
case $CONFIRM in
[yY]*) CONFIRM=true ;;
*) return ;;
esac
AUTH_FAIL=1
while [ $AUTH_FAIL != 0 ]; do
read -p "url: [https://outsideopen.com/oo_bin] " URL
URL=${URL:-https://outsideopen.com/oo_bin}
read -r -p "username: " USERNAME
read -r -s -p "password: " PASSWORD
echo ""
read -p "Automatically check for updates, once a day? (Y/N): " AUTO
case $AUTO in
[yY]*) AUTO="true" ;;
*) AUTO="false" ;;
esac
set +e
(curl --fail -u "${USERNAME}:${PASSWORD}" https://outsideopen.com/api/oo_bin/ >/dev/null 2>&1)
AUTH_FAIL=$?
set -e
if [ $AUTH_FAIL != 0 ]; then
echo ""
echo "We could not contact the update server. Please check your network connection, and make sure you have the correct credentials, before trying again."
fi
done
{
echo '[tunnels.update]'
echo "url = '${URL}'"
echo "username = '${USERNAME}'"
echo "password = '${PASSWORD}'"
echo "auto_update = ${AUTO}"
} >>"$HOME/.config/oo_bin/config.toml"
fi
}
function bash_version_check {
VERSION=$(bash --version | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
MAJOR_VERSION=$(echo $VERSION | sed -E 's/([0-9]+)\.[0-9]+\.[0-9]+/\1/')
if [ "${MAJOR_VERSION}" -lt "4" ]; then
echo "Command line completions requires Bash v4+"
echo "The completions will not be installed, please consider updating bash, and running this script again."
return 1
fi
return 0
}
function bash_add_local_bin_to_path {
if cat $HOME/.bashrc | grep -q 'PATH=$PATH:$HOME/.local/bin'; then
echo '`$HOME/.local/bin` is already added to your path. Nothing to be done'
else
echo 'Adding `PATH=$PATH:$HOME/.local/bin` to ~/.bashrc'
export PATH=$PATH:$HOME/.local/bin
echo 'PATH=$PATH:$HOME/.local/bin' >>$HOME/.bashrc
fi
}
function bash_add_completions {
_OO_COMPLETE=bash_source oo >$HOME/.config/oo_bin/oo-complete.bash
if cat $HOME/.bashrc | grep -q 'source $HOME/.config/oo_bin/oo-complete.bash'; then
echo '`~/.config/oo_bin/oo-complete.bash` is already sourced in your bashrc. Nothing to be done'
else
echo 'Adding `source ~/.config/oo_bin/oo-complete.bash` to ~/.bashrc'
echo 'source $HOME/.config/oo_bin/oo-complete.bash' >>$HOME/.bashrc
fi
}
function zsh_add_completions {
_OO_COMPLETE=zsh_source oo >$HOME/.config/oo_bin/oo-complete.zsh
if cat $HOME/.zshrc | grep -q 'source $HOME/.config/oo_bin/oo-complete.zsh'; then
echo '`~/.config/oo_bin/oo-complete.zsh` is already sourced in your zshhrc. Nothing to be done'
else
echo 'Adding `source ~/.config/oo_bin/oo-complete.zsh` to ~/.zshrc'
echo 'autoload -Uz compinit && compinit' >>$HOME/.zshrc
echo 'source $HOME/.config/oo_bin/oo-complete.zsh' >>$HOME/.zshrc
fi
}
function fish_add_completions {
if test -f $HOME/.config/fish/completions/oo.fish; then
echo '`~/.config/fish/completions/oo.fish` already exists. Nothing to be done'
else
_OO_COMPLETE=fish_source oo >$HOME/.config/fish/completions/oo.fish
echo 'Created/Updated ~/.config/fish/completions/oo.fish'
fi
}
for i in "$@"; do
case $i in
--prerelease)
PRERELEASE=true
;;
*)
# unknown option
;;
esac
done
make_config
echo 'Install Dependencies'
echo '********************'
install_dependencies
echo ''
echo 'Add ~/.local/bin to the path'
echo '****************************'
bash_add_local_bin_to_path
echo ''
echo 'Install Bin Scripts'
echo '*******************'
install
echo ''
echo 'Config Auto Update'
echo '******************'
add_tunnels_config_download
# We need to update here, or the completions may fail
oo --update
echo ''
echo 'Configure Completions'
echo '*********************'
# We check the bash version, because Mac still has an old version of Bash by default
if bash_version_check; then
bash_add_completions
fi
if which zsh &>/dev/null; then
zsh_add_completions
fi
if which fish &>/dev/null; then
fish_add_completions
fi
echo ''
echo "If your .baschrc/.zshrc was modified, you may need to restart your terminal for the changes to take effect."