-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.bash
301 lines (246 loc) · 9.93 KB
/
setup.bash
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ VARIABLES ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
declare -r CONST_APP_NAME="domposy"
declare -r CONST_APP_BRANCH_NAME="main"
declare -r CONST_LOGGER_NAME="simbashlog"
declare -r CONST_LOGGER_BRANCH_NAME="main"
declare -r CONST_GLOBAL_DOWNLOAD_PATH="/opt"
declare -r CONST_GLOBAL_BIN_PATH="/usr/bin"
declare -r CONST_USER_DOWNLOAD_PATH="$HOME/.local/share"
declare -r CONST_USER_BIN_PATH="$HOME/.local/bin"
declare -r CONST_DEPENDENCIES=(
"git"
"docker"
"tar"
"gzip"
)
# NOTE: The repos must contain a directory called 'src' with a bash script that has the same name as the repo and ends with '.bash'!
declare -r CONST_REPO_URLS=(
"https://github.com/fuchs-fabian/${CONST_LOGGER_NAME}.git"
"https://github.com/fuchs-fabian/${CONST_APP_NAME}.git"
)
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ LOGIC ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
function is_root {
[[ $(id -u) -eq 0 ]]
}
function check_dependencies {
for dependency in "${CONST_DEPENDENCIES[@]}"; do
echo "Checking if '$dependency' is available..."
command -v "$dependency" >/dev/null 2>&1 ||
{
echo "'$dependency' is not available. Please install it and try again."
exit 1
}
echo "'$dependency' is available."
done
}
function create_directory {
local directory="$1"
if [ ! -d "$directory" ]; then
echo "Creating directory '$directory'..."
mkdir -p "$directory"
fi
}
function get_app_name_from_repo_url {
basename "$1" .git
}
function clone_app_from_repo_url {
local repo_url="$1"
local app_name="$2"
local app_path="$3"
if [[ "$app_name" == "$CONST_LOGGER_NAME" ]]; then
echo "Cloning '$CONST_LOGGER_NAME' from branch '$CONST_LOGGER_BRANCH_NAME'..."
git clone --branch "$CONST_LOGGER_BRANCH_NAME" "$repo_url" "$app_path"
elif [[ "$app_name" == "$CONST_APP_NAME" ]]; then
echo "Cloning '$CONST_APP_NAME' from branch '$CONST_APP_BRANCH_NAME'..."
git clone --branch "$CONST_APP_BRANCH_NAME" "$repo_url" "$app_path"
else
echo "Cloning $app_name..."
git clone "$repo_url" "$app_path"
fi
}
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ INSTALLATION ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
function install {
local download_path=""
local bin_path=""
echo
echo "Installing..."
check_dependencies
if is_root; then
download_path="$CONST_GLOBAL_DOWNLOAD_PATH"
bin_path="$CONST_GLOBAL_BIN_PATH"
else
echo
echo "You are not root."
echo "The installation will be only available for the current user."
echo "To install it globally, please run this script as root (sudo)."
read -r -p "Do you want to continue? [y/N] " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
download_path="$CONST_USER_DOWNLOAD_PATH"
bin_path="$CONST_USER_BIN_PATH"
create_directory "$CONST_USER_DOWNLOAD_PATH"
create_directory "$CONST_USER_BIN_PATH"
else
echo "Installation aborted."
exit 1
fi
fi
echo "Donwload path: '$download_path'"
echo "Bin path: '$bin_path'"
for repo_url in "${CONST_REPO_URLS[@]}"; do
echo
local app_name
app_name=$(get_app_name_from_repo_url "$repo_url")
local app_path="$download_path/$app_name"
if [ -d "$app_path" ]; then
echo "'$app_name' is already downloaded."
else
echo "Downloading '$app_name'..."
clone_app_from_repo_url "$repo_url" "$app_name" "$app_path" ||
{
echo "Failed to download '$app_name'."
exit 1
}
fi
local original_app_path="$app_path/src/${app_name}.bash"
if [ -f "$original_app_path" ]; then
echo "Making '$app_name' executable..."
chmod +x "$original_app_path" ||
{
echo "Failed to make '$app_name' executable."
exit 1
}
echo "Creating symlink for '$app_name'..."
ln -sf "$original_app_path" "$bin_path/$app_name" ||
{
echo "Failed to create symlink for '$app_name' in '$bin_path'."
exit 1
}
echo "'$app_name' has been installed and is now executable."
if [ -n "$(command -v "$app_name")" ]; then
"$app_name" -v
echo "'$app_name' is working."
else
echo "'$app_name' is not working. Aborting..."
exit 1
fi
else
echo "The file '${app_name}.bash' was not found in '$app_path/src'."
fi
done
}
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ UNINSTALLATION ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
function uninstall {
local download_path=""
local bin_path=""
echo
echo "Uninstalling..."
local uninstall_success=false
if is_root; then
download_path="$CONST_GLOBAL_DOWNLOAD_PATH"
bin_path="$CONST_GLOBAL_BIN_PATH"
else
download_path="$CONST_USER_DOWNLOAD_PATH"
bin_path="$CONST_USER_BIN_PATH"
fi
echo "Donwload path: '$download_path'"
echo "Bin path: '$bin_path'"
for repo_url in "${CONST_REPO_URLS[@]}"; do
echo
app_name=$(get_app_name_from_repo_url "$repo_url")
if [ -d "$download_path/$app_name" ] && [ -L "$bin_path/$app_name" ]; then
read -r -p "Do you want to uninstall '$app_name'? [y/N] " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
echo "Uninstalling '$app_name'..."
echo "Removing symlink for '$app_name' in '$bin_path'..."
rm "$bin_path/$app_name" ||
{
echo "Failed to remove symlink for '$app_name' in '$bin_path'."
exit 1
}
app_path="$download_path/$app_name"
echo "Removing directory '$app_path'..."
rm -rf "$app_path" ||
{
echo "Failed to remove directory '$app_path'."
exit 1
}
echo "'$app_name' has been uninstalled."
uninstall_success=true
else
echo "Uninstallation for '$app_name' aborted."
fi
else
echo "'$app_name' is not installed?"
if is_root; then
echo "You run this script as root. Please run it as the user who installed it to uninstall it."
else
echo "You run this script as a user. Please run it as root to uninstall it globally or with the user who installed it."
fi
fi
done
if [ "$uninstall_success" = true ]; then
return 0
else
return 1
fi
}
# ╔═════════════════════╦══════════════════════╗
# ║ ║
# ║ UPDATE ║
# ║ ║
# ╚═════════════════════╩══════════════════════╝
function update {
echo
echo "Updating..."
echo "Note: The update will uninstall and install it again."
if uninstall; then
install
else
echo "Failed to uninstall. Aborting update..."
exit 1
fi
}
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
# ░░ ░░
# ░░ ░░
# ░░ MAIN ░░
# ░░ ░░
# ░░ ░░
# ░░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░░░░░░░░░░░░░░
echo "Current user: '$(whoami)'"
case "$1" in
install)
install
;;
uninstall)
uninstall
;;
update)
update
;;
*)
echo "Usage: $0 {install|uninstall|update}"
exit 1
;;
esac