-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·327 lines (241 loc) · 7.55 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·327 lines (241 loc) · 7.55 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/bin/bash
###### Validate tools ######
ERR=0
# Check that curl is available
curl --version >/dev/null 2>&1
if [ $? -ne 0 ]; then
ERR=1
echo -e "The 'curl' utility is missing.\nMake sure it is installed and available in your PATH!"
fi
# Check that tar is available
tar --version >/dev/null 2>&1
if [ $? -ne 0 ]; then
ERR=1
echo -e "The 'tar' utility is missing.\nMake sure it is installed and available in your PATH!"
fi
if [ $ERR != 0 ]; then
exit 1
fi
# Make sure script is run as root
if [ "$EUID" -ne 0 ]
then echo "Please run as root!"
exit
fi
if [ -z "$SUDO_USER" ]; then
echo "Run with sudo, not as root directly."
exit 1
fi
###### Function Definitions ######
# Function to ask user for permissions
function yn {
while true; do
read -p "$* [y/n]: " yn
case $yn in
[Yy]*) echo 0; return 0 ;;
[Nn]*) echo 1; return 1;;
esac
done
}
function install {
local src=$1 dst=$2
local st=0
if [ -f "$dst" ]; then
echo ""
echo "File $dst already exists."
st=$(yn "Want to Overwrite it?")
echo ""
fi
if [ $st -eq 0 ]; then
echo -n -e "[\033[32minstall\033[0m] Creating $dst ... "
mv -f "$src" "$dst" >/dev/null 2>&1
if [ -f "$dst" ]; then
echo -e "\033[32mOK\033[0m"
else
echo -e "\033[31mFAILED\033[0m"
exit 1
fi
fi
}
function uninstall {
local target=$1
if [ -e "$target" ]; then
echo -n -e "[\033[31muninstall\033[0m] Removing $target ... "
if [ -d "$target" ]; then
rm -rf "$target" >/dev/null 2>&1
elif [ -f "$target" ]; then
rm -f "$target" >/dev/null 2>&1
fi
if [ ! -e "$target" ]; then
echo -e "\033[32mOK\033[0m"
else
echo -e "\033[31mFAILED\033[0m"
exit 1
fi
fi
}
MODE=0
function run {
local msg=$1 cmd=$2
local -n reply=$3
local prefix="[\033[32minstall\033[0m]"
if [ $MODE -ne 0 ]; then
prefix="[\033[31muninstall\033[0m]"
fi
echo -n -e "$prefix $msg ... "
reply=$(eval "$cmd")
if [ $? -eq 0 ]; then
echo -e "\033[32mOK\033[0m"
else
echo -e "\033[31mFAILED\033[0m"
exit 1
fi
}
###### Mode Setup ######
# Set mode (0 = Install, 1 = Uninstall)
ARG=$1
MODE=0
if [ "$ARG" == "uninstall" ]; then
MODE=1
elif [ "$ARG" == "" ]; then
MODE=0
else
echo "Unknown argument: $ARG"
exit 1
fi
###### Home Fix ######
# Update home
HOME="/home/$SUDO_USER"
###### Display logo ######
echo -e "\033[33m"
echo " █▄ "
echo " ▄██▄▄ "
echo " ▄███▀ ▄▀▀█▄ ██ ████▄ ▄▀▀█▄ ████▄"
echo " ██ ▄█▀██ ██ ██ ██ ▄█▀██ ██ ██"
echo "▄▀███▄▄▀█▄██▄██▄██ ▀█▄▀█▄██▄████▀"
echo " _________________ ██ "
echo -e " |\033[97mRelease installer\033[33m| ▀ "
echo " ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ "
echo -e -n "\033[0m"
############### Install Mode ###############
if [ $MODE -eq 0 ]; then
###### Detect system arch ######
run "Detecting system architecture" "uname -m" ARCH
# Check that system arch is valid
if [ "$ARCH" != "x86_64" ] && [ "$ARCH" != "i686" ] && [ "$ARCH" != "armv7l" ] && [ "$ARCH" != "aarch64" ]; then
echo "There is no binary build for $ARCH"
exit 1
fi
###### Detecting latest catnap version ######
# Get latest actual release url from redirect url of '/releases/latest'
run "Pulling latest release url" \
"curl -Ls -o /dev/null -w %{url_effective} 'https://github.com/iinsertNameHere/catnap/releases/latest/'" \
RAW_URL
# Build download url from RAW_URL
BASE_URL=$(echo $RAW_URL | sed 's/tag/download/')
# Extract version number
run "Detecting version number" \
"echo '$BASE_URL' | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+'" \
VERSION
if [ -z "$VERSION" ]; then
echo "Detected invalid version number from release URL: '$RAW_URL'"
exit 1
fi
BIN_NAME="catnap-$VERSION-$ARCH"
CFG_SRC_NAME="catnap-$VERSION-config.tar.gz"
# Check if catnap is already installed
run "Checking if already installed" \
"catnap -v >/dev/null 2>&1; echo \$?"\
ALREADY_INSTALLED
# Detect the current version number that is installed
INSTALLED_VERSION=""
if [ $ALREADY_INSTALLED -eq 0 ]; then
run "Detecting installed version" \
"catnap -v | sed 's/Catnap //'" \
INSTALLED_VERSION
fi
###### Start installation ######
echo ""
echo "Architecture: $ARCH"
if [ "$INSTALLED_VERSION" != "" ]; then
echo "Installed Version: $INSTALLED_VERSION"
fi
echo "Latest Version: $VERSION"
echo ""
START_MSG="Start installation?"
# Warn user if latest version is already installed
if [ "$INSTALLED_VERSION" = "$VERSION" ]; then
echo -e "\033[33mAlready on the latest version ($VERSION)\033[0m"
echo ""
START_MSG="Start install anyways?"
fi
if [ $(yn "$START_MSG") -eq 1 ]; then
exit 0
fi
echo ""
# Create temp dir and cd into it
WORK_DIR=$(mktemp -d)
# Trap cleanup
trap "rm -rf $WORK_DIR; exit 1" SIGINT
trap "rm -rf $WORK_DIR; exit 1" SIGTERM
trap "rm -rf $WORK_DIR" EXIT
cd "$WORK_DIR"
###### Download files ######
# Download bin file
run "Downloading $BIN_NAME" \
"curl -LO '$BASE_URL/$BIN_NAME' >/dev/null 2>&1" \
NULL
# Download config source files
run "Downloading $CFG_SRC_NAME" \
"curl -LO '$BASE_URL/$CFG_SRC_NAME' >/dev/null 2>&1" \
NULL
###### Extract source ######
CONF_TMP_PATH="$WORK_DIR/config"
CONF_INSTALL_PATH="$HOME/.config/catnap"
# Extract config files
run "Extracting source files" \
"tar -xf $CFG_SRC_NAME config >/dev/null 2>&1" \
NULL
###### Install bin file ######
chmod +x "$WORK_DIR/$BIN_NAME"
install "$WORK_DIR/$BIN_NAME" "/usr/local/bin/catnap"
###### Install Config files ######
# Create config dir
mkdir -p "$CONF_INSTALL_PATH/themes"
# Install config.cat
install "$CONF_TMP_PATH/config.cat" "$CONF_INSTALL_PATH/config.cat"
# Install distros.cat
install "$CONF_TMP_PATH/distros.cat" "$CONF_INSTALL_PATH/distros.cat"
# Install theme
install "$CONF_TMP_PATH/themes/catppuccin-mocha.cat" "$CONF_INSTALL_PATH/themes/catppuccin-mocha.cat"
# Make the user the owner of the config files
chown $SUDO_USER:$SUDO_USER -R "$CONF_INSTALL_PATH"
###### Installed catnap successfully ######
echo ""
echo "Successfully installed catnap $VERSION!"
############### Uninstall Mode ###############
else
if [ $(yn "Continue uninstalling catnap?") -eq 1 ]; then
exit 0
fi
echo ""
###### Uninstall bin file ######
uninstall "/usr/local/bin/catnap"
echo ""
###### Uninstall Config files ######
if [ $(yn "Remove config files?") -eq 1 ]; then
echo ""
echo "Successfully uninstalled catnap"
exit 0
fi
echo ""
CONF_INSTALL_PATH="$HOME/.config/catnap"
# Uninstall config.cat
uninstall "$CONF_INSTALL_PATH/config.cat"
# Uninstall distros.cat
uninstall "$CONF_INSTALL_PATH/distros.cat"
# Uninstall theme
uninstall "$CONF_INSTALL_PATH/themes/catppuccin-mocha.cat"
###### Uninstalled catnap successfully ######
echo ""
echo "Successfully uninstalled catnap"
fi