-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_nowPlayingDeezer.sh
executable file
·407 lines (348 loc) · 10.8 KB
/
install_nowPlayingDeezer.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/bin/bash
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
BOLD='\033[1m'
ITALIC='\033[3m'
RESET='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
WHITE='\033[1;37m'
show_logo() {
echo -e "${CYAN}"
cat << "EOF"
_ _ ____ _ _
| \ | | _____ __ | _ \| | __ _ _ _(_)_ __ __ _
| \| |/ _ \ \ /\ / / | |_) | |/ _` | | | | | '_ \ / _` |
| |\ | (_) \ V V / | __/| | (_| | |_| | | | | | (_| |
|_| \_|\___/ \_/\_/ |_| |_|\__,_|\__, |_|_| |_|\__, |
|___/ |___/
____
| _ \ ___ ___ ____ ___ _ __
| | | |/ _ \/ _ \_ / / _ \ '__|
| |_| | __/ __// /_| __/ |
|____/ \___|\___/____\___|_|
EOF
echo -e "${RESET}"
}
clear_and_show_logo() {
clear
show_logo
}
center_text() {
local text="$1"
local width=$(tput cols)
local padding=$(( (width - ${#text}) / 2 ))
printf "%${padding}s%s%${padding}s\n" "" "$text" ""
}
print_fancy_box() {
local text="$1"
local width=$(tput cols)
local box_width=$((width - 4))
local padding=$(( (box_width - ${#text}) / 2 ))
echo -e "${CYAN}"
printf "+%${box_width}s+\n" | tr ' ' '-'
printf "|%*s%s%*s|\n" $padding "" "$text" $padding ""
printf "+%${box_width}s+\n" | tr ' ' '-'
echo -e "${RESET}"
}
show_progress() {
local duration=$1
local width=50
local bar_char="#"
local empty_char="-"
for ((i=0; i<=width; i++)); do
local percent=$((i*100/width))
local num_bars=$((i*width/width))
printf "\r[%-${width}s] %d%%" $(printf "%0.s${bar_char}" $(seq 1 $num_bars)) $percent
sleep $(bc <<< "scale=3; $duration/$width")
done
echo
}
info() {
echo -e "\n${BLUE}ℹ ${ITALIC}${WHITE}$1${RESET}"
}
success() {
echo -e "\n${GREEN}✔ ${BOLD}${WHITE}$1${RESET}"
}
error() {
echo -e "\n${RED}✖ ${BOLD}${WHITE}$1${RESET}"
}
warning() {
echo -e "\n${YELLOW}⚠ ${ITALIC}${WHITE}$1${RESET}"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
install_playerctl_debian() {
info "Installing playerctl on a Debian-based system..."
sudo apt update
sudo apt install -y playerctl
}
install_playerctl_redhat() {
info "Installing playerctl on a Red Hat-based system..."
sudo dnf install -y playerctl
}
install_playerctl_arch() {
info "Installing playerctl on Arch Linux..."
sudo pacman -S --noconfirm playerctl
}
choose_player() {
if [[ "$OSTYPE" == "darwin"* ]]; then
info "On macOS, we'll use Google Chrome for Deezer playback detection."
deezer_player="chrome"
else
players=($(playerctl -l))
if [ ${#players[@]} -eq 0 ]; then
error "No player detected. Make sure Deezer is running."
exit 1
fi
clear_and_show_logo
print_fancy_box "Available Players"
for i in "${!players[@]}"; do
echo -e " ${MAGENTA}${BOLD}$((i+1)).${RESET} ${CYAN}${players[$i]}${RESET}"
done
echo
while true; do
read -p "$(echo -e ${YELLOW}"Choose the Deezer player number (1-${#players[@]}) : "${RESET})" choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le "${#players[@]}" ]; then
deezer_player=${players[$((choice-1))]}
break
else
warning "Invalid choice. Please enter a number between 1 and ${#players[@]}."
fi
done
fi
}
ask_spotify_tokens() {
clear_and_show_logo
print_fancy_box "Spotify API Configuration"
while true; do
echo -e "${YELLOW}Enter your Spotify Client ID : ${RESET}"
read -s spotify_client_id
echo
if [ -n "$spotify_client_id" ]; then
echo -e "${GREEN}Client ID saved (length: ${#spotify_client_id})${RESET}"
break
else
echo -e "${RED}Client ID cannot be empty. Please try again.${RESET}"
fi
done
echo
while true; do
echo -e "${YELLOW}Enter your Spotify Client Secret : ${RESET}"
read -s spotify_client_secret
echo
if [ -n "$spotify_client_secret" ]; then
echo -e "${GREEN}Client Secret saved (length: ${#spotify_client_secret})${RESET}"
break
else
echo -e "${RED}Client Secret cannot be empty. Please try again.${RESET}"
fi
done
}
check_port() {
nc -z localhost $1 >/dev/null 2>&1
return $?
}
find_available_port() {
local port=$1
while check_port $port; do
port=$((port + 1))
done
echo $port
}
install_dependencies() {
if [[ "$OSTYPE" == "darwin"* ]]; then
info "Checking Node.js and npm installation..."
if ! command_exists node || ! command_exists npm; then
error "Node.js or npm is not installed. Please install Node.js and npm before running this script."
exit 1
fi
success "Node.js and npm are installed."
else
if ! command_exists npm; then
error "npm is not installed. Please install Node.js and npm before running this script."
exit 1
fi
fi
info "Installing npm dependencies..."
npm install
if [ $? -ne 0 ]; then
error "Dependency installation failed."
exit 1
fi
success "Dependencies installed successfully."
}
build_app() {
info "Building the application..."
npm run build
if [ $? -ne 0 ]; then
error "Application build failed."
exit 1
fi
success "Application built successfully."
}
launch_app() {
info "Launching the application in the background..."
nohup npm start > app.log 2>&1 &
success "Application launched successfully. PID: $!"
}
stop_app() {
info "Searching for running application..."
pid=$(ps aux | grep "[n]ode.*npm start" | awk '{print $2}')
if [ -n "$pid" ]; then
info "Application found with PID: $pid"
info "Stopping the application..."
kill $pid
sleep 2
if kill -0 $pid 2>/dev/null; then
warning "The application did not stop properly. Forcing shutdown..."
kill -9 $pid
fi
success "The application has been stopped successfully."
else
port=$(grep "PORT=" .env | cut -d '=' -f2)
pid=$(lsof -t -i:$port)
if [ -n "$pid" ]; then
info "Application found on port $port with PID: $pid"
info "Stopping the application..."
kill $pid
sleep 2
if kill -0 $pid 2>/dev/null; then
warning "The application did not stop properly. Forcing shutdown..."
kill -9 $pid
fi
success "The application has been stopped successfully."
else
warning "No running instance of the application was found."
fi
fi
}
update_env_and_restart() {
clear_and_show_logo
print_fancy_box "Update .env and Restart Application"
choose_player
ask_spotify_tokens
port=$(grep "PORT=" .env | cut -d '=' -f2)
cat << EOF > .env
PORT=$port
SPOTIFY_CLIENT_ID=$spotify_client_id
SPOTIFY_CLIENT_SECRET=$spotify_client_secret
PLAYERCTL_INSTANCE=$deezer_player
EOF
success ".env file updated successfully."
stop_app
launch_app
clear_and_show_logo
print_fancy_box "Update Completed"
success "The application has been updated and restarted on port $port."
info "You can access the application at http://localhost:$port/now-playing"
if [[ "$OSTYPE" == "darwin"* ]]; then
warning "Make sure Deezer is running in Google Chrome when you use the application."
else
warning "Make sure Deezer is running when you use the application."
fi
echo
read -p "Press Enter to return to the main menu..."
}
install_and_run() {
clear_and_show_logo
center_text "Installation and Configuration"
echo
print_fancy_box "Checking prerequisites"
show_progress 2
if [[ "$OSTYPE" == "darwin"* ]]; then
info "macOS detected. Skipping playerctl installation."
else
if ! command_exists playerctl; then
if command_exists apt-get; then
install_playerctl_debian
elif command_exists dnf; then
install_playerctl_redhat
elif command_exists pacman; then
install_playerctl_arch
else
error "Unable to detect a supported package manager."
error "Please install playerctl manually for your distribution."
exit 1
fi
if ! command_exists playerctl; then
error "playerctl installation failed."
exit 1
fi
fi
fi
clear_and_show_logo
success "Prerequisites checked."
show_progress 1
choose_player
ask_spotify_tokens
port=3000
available_port=$(find_available_port $port)
if [ $port -ne $available_port ]; then
warning "Port $port is not available. Using port $available_port."
port=$available_port
fi
cat << EOF > .env
PORT=$port
SPOTIFY_CLIENT_ID=$spotify_client_id
SPOTIFY_CLIENT_SECRET=$spotify_client_secret
PLAYERCTL_INSTANCE=$deezer_player
EOF
success ".env file created successfully."
install_dependencies
build_app
launch_app
clear_and_show_logo
print_fancy_box "Configuration completed"
success "The application is now installed and running on port $port."
info "You can access the application at http://localhost:$port/now-playing"
if [[ "$OSTYPE" == "darwin"* ]]; then
warning "Make sure Deezer is running in Google Chrome when you use the application."
else
warning "Make sure Deezer is running when you use the application."
fi
echo
info "To stop the application later, rerun this script and choose option 2."
echo
read -p "Press Enter to return to the main menu..."
}
main() {
while true; do
clear_and_show_logo
center_text "Now Playing Deezer - Installation and Management"
echo
print_fancy_box "Main Menu"
echo -e "${CYAN}1.${RESET} Install and configure the application"
echo -e "${CYAN}2.${RESET} Stop the application"
echo -e "${CYAN}3.${RESET} Update .env and restart application"
echo -e "${CYAN}4.${RESET} Quit"
echo
read -p "$(echo -e ${YELLOW}"Choose an option (1-4) : "${RESET})" choice
case $choice in
1)
install_and_run
;;
2)
stop_app
read -p "Press Enter to continue..."
;;
3)
update_env_and_restart
;;
4)
echo "Goodbye!"
exit 0
;;
*)
error "Invalid option. Please choose 1, 2, 3, or 4."
read -p "Press Enter to continue..."
;;
esac
done
}
main