-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSpacefile.sh
274 lines (235 loc) · 9.99 KB
/
Spacefile.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#
# Copyright 2016-2017 Blockie AB
#
# This file is part of Space.
#
# Space is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3 of the License.
#
# Space is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Space. If not, see <http://www.gnu.org/licenses/>.
#
# Disable warning about indirectly checking exit code
# shellcheck disable=2181
SPACE_INSTALL_BIN()
{
command install -m 755 "${_bin_file_name}" "${_bindest}"
if [ "$?" -gt 0 ]; then
if [ "$(id -u)" != 0 ]; then
PRINT "This program must be run as root in order to install ${_bin_file_name} to ${_bindest}. If performing the manual install, please run: \"sudo ./space /install/\". Otherwise just run the automated install again using 'sudo sh -c \"curl https://get.space.sh\"' instead." "error"
fi
PRINT "[${_bin_full_path}] FAILED" "error"
return 1
fi
PRINT "[${_bin_full_path}] OK" "ok"
}
SPACE_DEP_INSTALL()
{
SPACE_DEP="PRINT"
# This is a lighter variant of a dep_installer,
# since the full variant depends on the OS module,
# and we don't want that dependency here.
if ! command -v git >/dev/null; then
PRINT "Git is not installed, you will have to install it using your package manager." "error"
return 1
else
PRINT "Dependencies found." "ok"
fi
}
SPACE_BUILD()
{
SPACE_DEP="PRINT"
if ! command -v docker >/dev/null; then
PRINT "Docker is not installed. Failed to build container image." "error"
return 1
fi
if [ ! -f "./space" ]; then
PRINT "Build must be performed from Space root development directory" "error"
exit 1
fi
IMAGE_VERSION=$(./space -V 2>&1);
for version_part in $IMAGE_VERSION; do
IMAGE_VERSION=$version_part;
done;
# shellcheck disable=2086
docker build --build-arg VERSION=$IMAGE_VERSION -t docker.pkg.github.com/space-sh/space -f ./build/Dockerfile .
}
# Disable warning about indirectly checking exit code
# shellcheck disable=2181
SPACE_INSTALL()
{
SPACE_DEP="PRINT SPACE_INSTALL_BIN"
SPACE_ENV="AC_PREFIX BIN_PREFIX"
PRINT "Installing..."
local _binprefix="${1-}"
local _acprefix="${2-}"
# Check if to auto detect installation paths.
if [ -z "${_binprefix}" ]; then
PRINT "No bin prefix provided on cmd line, attempting auto detect." "debug"
if [ -n "${PREFIX-}" ]; then
PRINT "Auto detected bin prefix: \${PREFIX}=${PREFIX}." "debug"
_binprefix="${PREFIX}"
else
PRINT "Did not auto detect bin prefix \${PREFIX}. Using default: ${BIN_PREFIX}. 'export env PREFIX' to override." "debug"
_binprefix="${BIN_PREFIX}"
fi
fi
if [ -z "${_acprefix}" ]; then
PRINT "No auto completion prefix provided on cmd line, attempting auto detect." "debug"
if [ -n "${PREFIX-}" ]; then
PRINT "Auto detected auto completion prefix: \${PREFIX}=${PREFIX}." "debug"
PRINT "Looking for destinations..." "debug"
local _acdests="etc/bash_completion.d usr/share/bash-completion/completions"
for _acdir in ${_acdests}; do
local _dir="${PREFIX}/${_acdir}"
PRINT "Trying: ${_dir}." "debug"
if [ -d "${_dir}" ]; then
PRINT "Auto detected auto completion destination: ${_dir}."
_acprefix="${_dir}"
break
fi
done
fi
fi
if [ -z "${_acprefix}" ]; then
PRINT "Did not auto detect auto completion prefix \${PREFIX}. Using default: ${AC_PREFIX}. 'export env PREFIX' to override." "debug"
_acprefix="${AC_PREFIX}"
fi
local _bindest=${_binprefix}/bin
local _acdest=${_acprefix}
local _mandest=${_binprefix}/share/man/man1
local _bin_file_name="space"
local _ac_file_name="init_autocompletion.sh"
local _man_file_name="space.1"
local _ac_file_path="./completion/${_ac_file_name}"
local _man_file_path="./${_man_file_name}"
local _bin_full_path=${_bindest}/${_bin_file_name}
#
# Install program
# Existing installation
if [ -f "${_bin_full_path}" ]; then
local _dest_file_size=
_dest_file_size=$(wc -c < "${_bin_full_path}" )
local _dest_file_version=
_dest_file_version=$($_bin_full_path -V 2>&1)
local _src_file_size=
_src_file_size=$(wc -c < "${_bin_file_name}" )
local _src_file_version=
_src_file_version=$(./${_bin_file_name} -V 2>&1)
PRINT "Space is already installed on ${_bin_full_path}" "warning"
if [ "${_dest_file_size}" -eq "${_src_file_size}" ]; then
PRINT "Source file size is the same as destination: ${_src_file_size} bytes long." "warning"
else
PRINT "Source file is ${_src_file_size} bytes long, while destination file is ${_dest_file_size} bytes." "warning"
fi
PRINT "Replacing current installed version [${_dest_file_version}] with [${_src_file_version}]" "warning"
# Sleep for 5 before proceeding
PRINT "Grace time: 5 seconds before proceeding..." "warning"
sleep 5
# In case of existing old installs (0.9.0 and earlier), they likely
# won't have man pages installed and the destination dir might not exist.
# Make sure destination exists, otherwise we just create it
mkdir -p "$_mandest"
# Install!
SPACE_INSTALL_BIN
if [ "$?" -gt 0 ]; then
return 1
fi
else
# Often times PREFIX exists but not /bin or /man/{man1}
# Make sure destination exists, otherwise we just create it
mkdir -p "$_bindest"
mkdir -p "$_mandest"
# Install!
SPACE_INSTALL_BIN
if [ "$?" -gt 0 ]; then
return 1
fi
fi
# Install man page
# Check source file is present
if [ ! -f $_man_file_path ]; then
PRINT "[${_mandest}/${_man_file_name}] FAILED to find man page: $_man_file_path. Skipping..." "warning"
else
if [ -d "${_mandest}" ]; then
command install -m 644 "${_man_file_path}" "${_mandest}"
if [ "$?" -gt 0 ]; then
PRINT "[${_mandest}/${_man_file_name}] FAILED to install man page. Skipping..." "warning"
else
PRINT "[${_mandest}/${_man_file_name}] OK" "ok"
fi
else
PRINT "[${_mandest}/${_man_file_name}] FAILED to find destination directory: $_mandest. Skipping..." "warning"
fi
fi
# Install auto completion
# Check source file is present
if [ ! -f $_ac_file_path ]; then
PRINT "[${_acdest}/${_ac_file_name}] FAILED" "warning"
PRINT "Optional auto completion will not work because the completion script could not be located at ${_ac_file_path}" "warning"
else
if [ -d "${_acdest}" ]; then
command install -m 644 "$_ac_file_path" "${_acdest}/space"
if [ "$?" -gt 0 ]; then
PRINT "[${_acdest}/space] FAILED" "warning"
PRINT "Optional auto completion will not work because destination doesn't exist: [${_acdest}]. Make sure the destination directory is valid and bash-completion is installed. After that, repeat the installation process if bash completion is desired." "warning"
else
PRINT "[${_acdest}/space] OK" "ok"
PRINT "You might want to re-login into bash to get the bash completion loaded."
fi
else
PRINT "[${_acdest}/space] FAILED" "warning"
PRINT "Optional auto completion will not work because destination doesn't exist: [${_acdest}]. Make sure the destination directory is valid and bash-completion is installed. After that, repeat the installation process if bash completion is desired." "warning"
local _uname_s=
_uname_s=$(uname -s)
if [ "$_uname_s" = "Darwin" ]; then
PRINT "Ah! I noticed you are running on Darwin. If you want to enable optional auto completion, take the following hint:"
PRINT "brew install bash-completion && ./space /install/ -- \"/usr/local\" \"/usr/local/etc/bash_completion.d\""
PRINT "After that, relog on the terminal in order to have the new auto completion loaded."
fi
fi
fi
}
# Disable warning about indirectly checking exit code
# shellcheck disable=2181
SPACE_UNINSTALL()
{
# External
# shellcheck disable=2034
SPACE_DEP="PRINT"
# shellcheck disable=2034
SPACE_ENV="AC_PREFIX BIN_PREFIX"
local _find_space=
_find_space=$(which space 2>&1)
if [ -z "${_find_space}" ]; then
PRINT "Failed to find Space installed on the system. Either not installed or missing on PATH." "warning"
else
local _uname_s=
_uname_s=$(uname -s)
local _find_ac=${AC_PREFIX}
local _find_man=
_find_man=$(man -w space 2>&1)
# If man didn't work, look for known locations
if [ "$?" -gt 0 ]; then
local _man_file_name="space.1"
_find_man=${BIN_PREFIX}/share/man/man1/${_man_file_name}
if [ ! -f "$_find_man" ]; then
_find_man=${PREFIX-}/share/man/man1/${_man_file_name}
if [ ! -f "$_find_man" ]; then
PRINT "Failed to retrieve man page location for \"$_man_file_name\"" "warning"
_find_man="$_man_file_name"
fi
fi
fi
if [ "$_uname_s" = "Darwin" ]; then
_find_ac="/usr/local/etc/bash_completion.d"
fi
PRINT "In order to uninstall Space, remove the following files: \"${_find_space}\" \"${_find_ac}/space\" \"${_find_man}\""
fi
}