forked from Botspot/pi-apps
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage
executable file
·718 lines (574 loc) · 28.8 KB
/
manage
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#!/bin/bash
#$1 is an action, like install
#$2 is app name, like Arduino
DIRECTORY="$(readlink -f "$(dirname "$0")")"
function error {
echo -e "\e[91m$1\e[39m"
exit 1
}
if [ -z "$1" ];then
error "You need to specify an operation, and in most cases, which app to operate on."
fi
set -a #make all functions in the api available to apps
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
diagnose_apps() { #Given a list of apps that failed to install/uninstall, loop through each error log, diagnose it, and provide a "Send report" button if applicable.
local failed_apps="$1"
local num_lines="$(wc -l <<<"$failed_apps")"
local i=1 #counter to track which failed_app in the list is current
for app in $failed_apps ;do
logfile="$(get_logfile "$app")"
#given the app's logfile, categorize the error and set the error_type variable
diagnosis="$(log_diagnose "$logfile")"
error_type="$(echo "$diagnosis" | head -n1)" #first line of diagnosis is the type of error
error_caption="$(echo "$diagnosis" | tail -n +2)" #subsequent lines of diagnosis are caption(s)
#set the window-text
if [ "$error_type" == unknown ];then
text="<b>${app^}</b> failed to $action for an <b>unknown</b> reason."
else
text="<b>${app^}</b> failed to $action because Pi-Apps encountered $([[ "$error_type" == [aeiou]* ]] && echo an || echo a) <b>$error_type</b> error."
fi
#if the error_type is NOT system, internet, or package, AND the app exists in the official Pi-Apps repository, AND the app-script has not been modified, AND the system setup is supported, AND the app is not a package-app, then enable the "Send report button"
if [ "$(app_type "$app")" == package ];then
text+=$'\n'"Error report cannot be sent because this \"app\" is really just a shortcut to $action a Debian package. It's not a problem that Pi-Apps can fix."
elif [[ "$error_type" =~ ^(system|internet|package)$ ]];then
text+=$'\n'"Error report cannot be sent because this is not an issue with Pi-Apps."
elif ! list_apps online | grep -q "$app" ;then
text+=$'\n'"Error report cannot be sent because this app is not in the official repository."
elif [ "$action" == install ] && [ "$(app_type "$app")" == standard ] && [ "$(sha256sum "${DIRECTORY}/apps/${app}/$(script_name_cpu "$app")" | awk '{print $1}')" != "$(sha256sum "${DIRECTORY}/update/pi-apps/apps/${app}/$(script_name_cpu "$app")" | awk '{print $1}')" ];then
text+=$'\n'"Error report cannot be sent because this app is not the official version."
elif [ "$action" == uninstall ] && [ "$(app_type "$app")" == standard ] && [ "$(sha256sum "${DIRECTORY}/apps/${app}/uninstall" | awk '{print $1}')" != "$(sha256sum "${DIRECTORY}/update/pi-apps/apps/${app}/uninstall" | awk '{print $1}')" ];then
text+=$'\n'"Error report cannot be sent because this app does not match the official version."
elif [ "$supported" == no ];then
text+=$'\n'"Error report cannot be sent because your system is unsupported."
else
#if all of the above checks evaluate to FALSE, then display the "Send report" button.
send_button=(--button='Send report'!"${DIRECTORY}/icons/upload.png":2)
fi
#display support links, depending on if this was a package-app or a script-app
if [ "$(app_type "$app")" == package ];then
text+=$'\n'"As this is an APT error, consider Googling the errors or asking for help in the <a href=\"https://forums.raspberrypi.com\">Raspberry Pi Forums</a>."
else
text+=$'\n'"Support is available on <a href=\"https://discord.gg/RXSTvaUvuu\">Discord</a> and <a href=\"https://github.com/Botspot/pi-apps/issues/new/choose\">Github</a>."
fi
#if the error_caption is empty, display logfile instead
if [ -z "$error_caption" ];then
text+=$'\n'"You can view the terminal output below. (scroll down)"
error_caption="$(cat "$logfile")"
else
text+=$'\n'"Below, Pi-Apps explains what went wrong and how you can fix it."
fi
#this dialog may be one in a series of failed_app dialogs. Name the window-close button accordingly.
if [ $i -lt $num_lines ];then
close_button=(--button="Next error!${DIRECTORY}/icons/forward.png":1)
else
close_button=(--button="Close!${DIRECTORY}/icons/exit.png":1)
fi
echo "$error_caption" | yad "${yadflags[@]}" --text-info --width=700 --height=300 --title="Error occured when ${action}ing $app" \
--image="${DIRECTORY}/icons/error.png" --image-on-top \
--text="$text" --fontname=12 \
--button='View log'!"${DIRECTORY}/icons/log-file.png"!"Review the output from when <b>$app</b> tried to $action.":"bash -c 'view_file "\""$logfile"\""'" \
"${send_button[@]}" \
"${close_button[@]}"
button="${PIPESTATUS[1]}"
if [ $button == 2 ];then
#send error log
send_error_report "$logfile"
fi
i=$((i+1))
done
}
validate_apps_gui() { #Given a list of actions and apps, graphically notify the user if there is a problem or ask for confirmation. Result is sent to stdout.
#action and app are separated by semicolon. (;)
#example line of input on $1: "install;Zoom"
local IFS=$'\n'
local queue="$1"
#Ensure that the first word of each line is 'install' or 'uninstall'
local i=1 #track the line number in queue
for line in $queue ;do
action="$(echo "$line" | awk -F ';' '{print $1}')"
if [ "$action" != install ] && [ "$action" != uninstall ] && [ "$action" != update ];then
warning "illegal mode: '$action' Removing this line from the queue"
#remove the app from list
queue="$(echo "$queue" | sed ${i}d)"
fi
i=$((i+1))
done
[ -z "$queue" ] && exit 0
#Ensure that each app-name is valid
local i=1 #track the line number in queue
for line in $queue ;do
action="$(echo "$line" | awk -F ';' '{print $1}')"
app="$(echo "$line" | awk -F ';' '{print $2}')"
if [ ! -d "${DIRECTORY}/apps/${app}" ];then
yad --text="Invalid app "\""<b>$app</b>"\"". Cannot $action it." \
--text-align=center --center --title='Error' --window-icon="${DIRECTORY}/icons/logo.png" \
--button=OK!"${DIRECTORY}/icons/check.png":0
#remove the app from list
queue="$(echo "$queue" | sed ${i}d)"
fi
i=$((i+1))
done
[ -z "$queue" ] && exit 0
#if trying to install an already-installed app, or trying to uninstall and already-uninstalled app, ask for confirmation
local i=1 #track the line number in queue
for line in $queue ;do
action="$(echo "$line" | awk -F ';' '{print $1}')"
app="$(echo "$line" | awk -F ';' '{print $2}')"
if [ "$(app_status "${app}")" == "${action}ed" ];then
yad --text="<b>$app</b> is already ${action}ed. Are you sure you want to $action it again?" \
--text-align=center --center --title='Quick question' --window-icon="${DIRECTORY}/icons/logo.png" \
--button=No!"${DIRECTORY}/icons/exit.png":1 --button=Yes!"${DIRECTORY}/icons/check.png":0
if [ $? != 0 ];then
#user clicked No, so remove the app from list
queue="$(echo "$queue" | sed ${i}d)"
fi
fi
i=$((i+1))
done
[ -z "$queue" ] && exit 0
#Check if any apps are updatable and ask user if they really want to INSTALL an outdated version. (This is skipped if uninstalling apps)
for line in $queue ;do
action="$(echo "$line" | awk -F ';' '{print $1}')"
app="$(echo "$line" | awk -F ';' '{print $2}')"
if [ $action == install ];then
#determine the filename for the app's script to be run
script_name_cpu="$(script_name_cpu "$app")"
#if the app-script doesn't match version in update folder
if [ -f "${DIRECTORY}/update/pi-apps/apps/${app}/${script_name_cpu}" ] && ! diff "${DIRECTORY}/update/pi-apps/apps/${app}/${script_name_cpu}" "${DIRECTORY}/apps/${app}/${script_name_cpu}" -q >/dev/null ;then
"${DIRECTORY}/updater" set-status &>/dev/null & #check for updates in background, so if user chooses "Yes", the updater will be guaranteed to have the app listed
yad --text="Hold up..."$'\n'"<b>$app</b>'s $script_name_cpu script does not match the online version. Either you are about to install an outdated version, or you've made changes to the script yourself."$'\n\n'"<b>Would you like to update $app</b> before installing it?" \
--text-align=center --center --title='Quick question' --window-icon="${DIRECTORY}/icons/logo.png" --width=400 \
--button="Install anyway"!"${DIRECTORY}/icons/forward.png":1 --button="Update first"!"${DIRECTORY}/icons/download.png":0
if [ $? == 0 ];then
#if user clicked Yes, run the updater and avoid launching a second terminal
use_terminal=0 "${DIRECTORY}/updater" gui fast
#now that the api script was potentially updated, source it again to get new functions
source "${DIRECTORY}/api" || error "failed to source ${DIRECTORY}/api"
fi
fi
fi
done
[ -z "$queue" ] && exit 0
echo "$queue"
}
#remove old mcpi repositories - this runonce is here so that terminal-only users will still receive the fix.
(runonce <<"EOF"
if [ -f /etc/apt/sources.list.d/Alvarito050506_mcpi-devs.list ];then
sudo rm -f /etc/apt/sources.list.d/Alvarito050506_mcpi-devs.list
fi
if [ -f /etc/apt/sources.list.d/mcpi-revival.list ];then
sudo rm -f /etc/apt/sources.list.d/mcpi-revival.list
sudo rm -f /etc/apt/trusted.gpg.d/mcpi-revival.gpg
fi
if dpkg -l box86-no-binfmt-restart &>/dev/null ;then
sudo apt purge -y box86-no-binfmt-restart
sudo apt update
sudo apt install -y box86
fi
EOF
) &>/dev/null
#An apt repository's Packages file can be corrupted so that an apt update will silently fail. See: https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1809174
#This line will fix the problem by removing any zero-size Packages files.
removal_list="$(find /var/lib/apt/lists -type f -name '*Packages' -size 0 2>/dev/null)"
if [ ! -z "$removal_list" ]; then
if [ x$DISPLAY != x ] ; then
while ! sudo -n true; do
yad --title="Broken Local Packages Repo Detected" --text="Please enter your user password \nso pi-apps can attempt a repair:" --image="dialog-password" --entry --hide-text 2>/dev/null | sudo -S echo "" 2>&1 >/dev/null
done
fi
echo "$removal_list" | xargs sudo rm -f
fi
#Silently re-download repo if github repository is over 3 months out of date
{
#first compare local git repo's last-commit-time with the current system time (Unix epoch time format)
current_git_date="$(cd "$DIRECTORY"; git show -s --format=%ct)"
current_local_date="$(date +%s)"
if [ -z "$current_git_date" ] || [ "$current_local_date" -gt $(($current_git_date + 7776000)) ];then
#if local git repo's last-commit-time is 3 months older than current system time, now compare the local git repo's last-commit-time with the online repo's modification time to avoid false positives.
#This two-tiered approach prevents unnecessary GitHub API calls and speeds up manage script's execution for normal usage.
command -v curl >/dev/null || sudo apt install -y curl
upstream_git_date="$(curl https://api.github.com/repos/Botspot/pi-apps/commits/master 2>&1 | grep '"date":' | tail -n 1 | sed 's/"date"://g' | xargs date +%s -d 2>/dev/null)"
if [[ "$upstream_git_date" =~ ^[0-9]+$ ]] && ([ -z "$current_git_date" ] || [ "$upstream_git_date" -gt $(($current_git_date + 7776000)) ]);then
yad --window-icon="${DIRECTORY}/icons/logo.png" --width=500 --no-buttons --center --title="Auto-updating Pi-Apps" \
--text="Your Pi-Apps installation is somehow 3 months out-of-date."$'\n'"Reinstalling Pi-Apps and saving the old version to ${DIRECTORY}-3-months-old..." &
yadpid=$!
clear
echo -e "\nYour Pi-Apps installation is somehow 3 months out-of-date.\nReinstalling Pi-Apps and saving the old version to ${DIRECTORY}-3-months-old...\n\n" 1>&2
cd $HOME
rm -rf ~/pi-apps-forced-update
command -v git >/dev/null || sudo apt install -y git
git clone "$(cat "${DIRECTORY}/etc/git_url")" pi-apps-forced-update 1>&2 && \
cp -af "${DIRECTORY}/data" ~/pi-apps-forced-update && \
mv -f "$DIRECTORY" "${DIRECTORY}-3-months-old" && \
mv -f ~/pi-apps-forced-update "$DIRECTORY"
sleep 10
kill $yadpid 2>/dev/null
#run new manage script in background
"${DIRECTORY}/manage" "$@"
exit $?
fi
fi
}
mkdir -p "${DIRECTORY}/data/status" "${DIRECTORY}/data/update-status" "${DIRECTORY}/logs"
#remove week-old logfiles
find "${DIRECTORY}/logs" -type f -mtime +6 -exec rm -f {} \; &>/dev/null &
#check if hardware and OS is supported
if is_supported_system >/dev/null;then
supported=yes
else
supported=no
fi
if [ "$1" == 'daemon' ];then
#Daemon to run in the background and install/uninstall/update apps as the user makes selections
#This allows the user to queue up a list of actions that will run sequentially.
#this folder will contain the runtime files necessary to make the daemon work.
mkdir -p "${DIRECTORY}/data/manage-daemon"
#a list of pending steps is displayed with yad. This function adds an item to the list
add_to_list() { # $1 is action, $2 is app, $3 is exit code status ('0' = success), $4 is output location (usually blank, but can be 'stdout')
local action="$1"
local app="$2"
local status="$3"
# $4 specifies where the output should go. Default is to send the output to the yadlist pipe, but if value is 'stdout', output to stdout.
local output="$4"
if [ -f "${DIRECTORY}/apps/$app/icon-24.png" ];then
local icon="${DIRECTORY}/apps/$app/icon-24.png"
else
local icon="${DIRECTORY}/icons/none-24.png"
fi
if [ -z "$status" ];then
#if there is no status number, then this action has not occured yet.
local content="${DIRECTORY}/icons/wait.png
${DIRECTORY}/icons/$action.png
Will $action
$icon
$app"
elif [ "$status" == 0 ];then
#if status is 0, then action completed successfully.
local content="${DIRECTORY}/icons/success.png
${DIRECTORY}/icons/$action.png
$(echo "${action^}ed" | sed 's/Updateed/Updated/g')
$icon
$app"
elif [ "$status" == 'in-progress' ];then
#if status is "in-progress", then action is currently being executed.
local content="${DIRECTORY}/icons/prompt.png
${DIRECTORY}/icons/$action.png
$(echo "${action^}ing..." | sed 's/Updateing/Updating/g')
$icon
$app"
else
#if status is 1, then action completed unsuccessfully.
local content="${DIRECTORY}/icons/failure.png
${DIRECTORY}/icons/$action.png
${action^} failed
$icon
$app"
fi
#write the output to yadlist or stdout
if [ "$output" == stdout ];then
echo "$content"
else
echo "$content" > "${DIRECTORY}/data/manage-daemon/yadlist"
fi
}
clear_list() { #clear the queue-viewer window
echo -e '\f' > "${DIRECTORY}/data/manage-daemon/yadlist"
}
write_list() { #given $queue in $1, rebuild the queue-viewer window. This avoids clearing the list until the new one has been generated, so makes for a smoother experience
local queue="$1"
local output='' #variable to send to yadlist file
local IFS=$'\n'
local line
for line in $queue ;do
action="$(echo "$line" | awk -F';' '{print $1}')"
app="$(echo "$line" | awk -F';' '{print $2}')"
code="$(echo "$line" | awk -F';' '{print $3}')"
output+="$(add_to_list "$action" "$app" "$code" stdout)"$'\n'
done
output="${output::-1}" #remove final newline character
clear_list
echo "$output" > "${DIRECTORY}/data/manage-daemon/yadlist"
}
#make a named pipe so that other daemon processes can notify this master daemon process to complete new tasks
if [ ! -e "${DIRECTORY}/data/manage-daemon/queue" ];then
mkfifo "${DIRECTORY}/data/manage-daemon/queue"
fi
#each line in $2 is something like "install Zoom" or "uninstall Arduino"
queue="$2"
#To simplify parsing, place a ';' character between $1 (the action) and subsequent args. (the app)
queue="$(echo "$queue" | sed 's/^\( *[^ ]\+\) /\1;/')"
#validate the selections first
queue="$(validate_apps_gui "$queue")"
[ -z "$queue" ] && exit 0
#send each requested action to the queue file
if [ ! -z "$queue" ];then
echo "$queue" > "${DIRECTORY}/data/manage-daemon/queue" &
fi
#only one instance of this script should ever be running at a time.
#Use a PID file to check if another daemon process is running.
if [ -f "${DIRECTORY}/data/manage-daemon/pid" ];then
#check if PID is running
if [ -e "/proc/$(cat "${DIRECTORY}/data/manage-daemon/pid")" ];then
echo "Sending instructions to daemon. (PID $(cat "${DIRECTORY}/data/manage-daemon/pid"))"
#Immediately add these new actions to the gui list
IFS=$'\n'
for line in $queue ;do
#get first word of this line - the action. Subsequent words are the name of the app.
action="$(echo "$line" | awk -F ';' '{print $1}')"
app="$(echo "$line" | awk -F ';' '{print $2}')"
add_to_list "$action" "$app"
done
#exit script - data has been sent to already-running daemon
exit 0
fi
fi #past this point, this instance is acting as the daemon.
#write my own PID to the pid file
echo $$ > "${DIRECTORY}/data/manage-daemon/pid"
#Display a list of actions and their current status.
#This list is updated with new information as time progresses.
#Another named pipe is created to refresh the yad list later.
rm -f "${DIRECTORY}/data/manage-daemon/yadlist"
mkfifo "${DIRECTORY}/data/manage-daemon/yadlist" #make a named pipe
[ -z "$geometry2" ] && geometry2='--center'
tail -F "${DIRECTORY}/data/manage-daemon/yadlist" | yad "$geometry2" --title='Monitor Progress' \
--list --tail --no-headers --column=:IMG --column=:IMG --column=Text --column=:IMG --column=Text --width=330 --height=200 \
--separator='\n' --window-icon="${DIRECTORY}/icons/logo.png" \
--dclick-action=true --select-action=true \
--no-buttons &
yadpid=$!
trap "kill $yadpid 2>/dev/null" EXIT
#Used to track which line of $queue is currently being dealt with.
current_line_num=1
queue=''
IFS=$'\n'
while true;do #repeat until nothing is left in the queue
#check for new actions to be executed
echo -n > "${DIRECTORY}/data/manage-daemon/queue" & #ensure that the pipe is in write mode to prevent cat from hanging
new_lines="$(cat "${DIRECTORY}/data/manage-daemon/queue")"
if [ ! -z "$new_lines" ];then #if new actions have been added to the queue
#keep track of all actions for this session in the $queue variable
if [ -z "$queue" ];then
queue="$new_lines"
else
queue+=$'\n'"$new_lines"
fi
echo "queue is '$queue'"
for line in $new_lines ;do
echo "Received '$line'"
#indicate current action in current line of $queue
queue="$(echo "$queue" | sed "${current_line_num}s/$/;in-progress/")"
#refresh the list in queue-viewer window
write_list "$queue"
#get first word of this line - the action. Subsequent words are the name of the app.
action="$(echo "$line" | awk -F';' '{print $1}')"
app="$(echo "$line" | awk -F';' '{print $2}')"
echo "Action is $action, app is $app"
"${DIRECTORY}/manage" "$action" "$app"
exitcode=$?
#record exit code in current line of $queue
queue="$(echo "$queue" | sed "${current_line_num}s/;in-progress$/;$exitcode/")"
#one more line of $queue has been completed.
current_line_num=$((current_line_num+1))
done
#refresh the list in queue-viewer window
write_list "$queue"
else #no further items in queue, so stop the daemon
#before exiting the loop, add a line to the queue-viewer window indicating that all items have completed.
echo "${DIRECTORY}/icons/none-1.png
${DIRECTORY}/icons/none-1.png
Done.
${DIRECTORY}/icons/none-1.png
" > "${DIRECTORY}/data/manage-daemon/yadlist"
#exit loop
break
fi
done
#all actions have been completed. Daemon has effectively stopped listening, so remove its pid file
rm -f "${DIRECTORY}/data/manage-daemon/pid"
#close the queue-viewer window in a few seconds
(sleep 5; kill $yadpid 2>/dev/null) &
#diagnose every failed app's logfile
failed_apps="$(echo "$queue" | grep -v ';0$' | awk -F';' '{print $2}')"
diagnose_apps "$failed_apps"
elif [ "$1" == 'multi-uninstall' ] || [ "$1" == 'multi-install' ];then
if [ "$1" == 'multi-uninstall' ];then
action=uninstall
elif [ "$1" == 'multi-install' ];then
action=install
fi
app_list="$2" #newline-separated list of apps to install/uninstall
#check if any app names are invalid - use the validate_apps_gui function which requires the action to prefix each line. Then filter out the action afterwards.
app_list="$(validate_apps_gui "$(echo "$app_list" | sed "s/^/${action};/g")" | sed 's/.*;//g')"
[ -z "$app_list" ] && exit 0
#install/uninstall one app at a time. If it fails then add the app to the list of failed apps
IFS=$'\n'
failed_apps=''
for app in $app_list ;do
"${DIRECTORY}/manage" $action "$app"
if [ $? != 0 ];then
#this app failed to install - add it to the list of failed apps
failed_apps+="$app"$'\n'
fi
done
if [ ! -z "$failed_apps" ];then
exit 1
fi
elif [ "$1" == 'install-if-not-installed' ];then
#if not installed
if [ "$(app_status "$2")" != installed ];then
#install it
"${DIRECTORY}/manage" install "$2" || exit 1
fi
elif [ "$1" == 'install' ] || [ "$1" == 'uninstall' ];then
#for this operation, a program name must be specified.
app="$2"
if [ -z "$app" ];then
error "For this operation, you must specify which app to operate on."
elif [ ! -d "${DIRECTORY}/apps/$app" ];then
error "${DIRECTORY}/apps/$app does not exist!"
fi
if [ "$1" == install ];then
action=install
else
action=uninstall
fi
#check for internet connection
errors="$(command wget --spider https://github.com 2>&1)"
if [ $? != 0 ];then
error "No internet connection! (github.com failed to respond)\nErrors:\n$errors"
fi
#ensure not a disabled app
if [ "$action" == install ] && [ "$(app_status "${app}")" == 'disabled' ];then
warning "Not installing the $app app. IT IS DISABLED."
exit 0
fi
#display warning if hardware and os is unsupported
if [ "$supported" == no ];then
warning "YOUR SYSTEM IS UNSUPPORTED:\n$(is_supported_system)"
sleep 1
echo -e "\e[103m\e[30mThe ability to send error reports has been disabled.\e[39m\e[49m"
sleep 1
echo -e "\e[103m\e[30mWaiting 10 seconds... (To cancel, press Ctrl+C or close this terminal)\e[39m\e[49m"
sleep 10
fi
#determine path for log file to be created
logfile="${DIRECTORY}/logs/${action}-incomplete-${app}.log"
if [ -f "$logfile" ] || [ -f "$(echo "$logfile" | sed 's+-incomplete-+-success-+g')" ] || [ -f "$(echo "$logfile" | sed 's+-incomplete-+-fail-+g')" ];then
#append a number to logfile's file-extension if the original filename already exists
i=1
while true;do
#if variable $i is 2, then example newlogfile value: /path/to/install-Discord.log2
newlogfile="$logfile$i"
if [ ! -f "$newlogfile" ] && [ ! -f "$(echo "$newlogfile" | sed 's+/-incomplete-+-success-+g')" ] && [ ! -f "$(echo "$newlogfile" | sed 's+-incomplete-+-fail-+g')" ];then
logfile="${newlogfile}"
break
fi
i=$((i+1))
done
fi
#analytics
bitly_link "$app" "$action" &
#if this app has scripts, determine which script to run
if [ "$(app_type "$app")" == standard ];then
if [ "$action" == install ];then
scriptname="$(script_name_cpu "$app")" #will be install, install-32, or install-64
[ -z "$scriptname" ] && error "It appears $app does not have an install-${arch} script suitable for your ${arch}-bit OS."
else #uninstall mode
scriptname=uninstall
fi
appscript=("${DIRECTORY}/apps/${app}/${scriptname}")
chmod u+x "$appscript" &>/dev/null
#if this app just lists a package-name, set the appscript to install that package
else
appscript=(bash -c -o pipefail "apt_lock_wait ; sudo -E apt $(echo "$action" | sed 's/uninstall/purge --autoremove/g') -yf $(cat "${DIRECTORY}/apps/$app/packages") 2>&1 | less_apt")
fi
status "${action^}ing \e[1m${app}\e[0m\e[96m..." | tee -a "$logfile"
echo
cd $HOME
if [[ "$3" == "update" ]]; then
script_input="update"
else
script_input=""
fi
nice "${appscript[@]}" "$script_input" &> >(tee -a "$logfile")
exitcode="${PIPESTATUS[0]}"
#if app succeeded
if [ $exitcode == 0 ];then
status_green "\n${action^}ed ${app} successfully." | tee -a "$logfile"
echo "${action}ed" > "${DIRECTORY}/data/status/${app}"
format_logfile "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-success-+g')" #rename logfile to indicate it was successful
else #if app failed to install/uninstall
#remove dummy deb if app failed to install (to avoid dummy debs being left on a users install for broken apps)
package_name="$(app_to_pkgname "$app")"
if [[ ${action} == "install" ]] && [[ "$(app_type "$app")" == standard ]] && package_installed "$package_name"; then
if [[ "$script_input" == "update" ]]; then
#if install failed as part of an update, don't autoremove the packages, only purge the dummy deb
output="$(sudo -E apt purge -y $package_name 2>&1 | less_apt | tee /dev/stderr)"
if [ $? != 0 ];then
echo "$output" | tee -a "$logfile"
warning "Failed to purge dummy deb ($package_name)" 2>&1 | tee -a "$logfile"
else
echo "$output" | tee -a "$logfile"
fi
unset output
else
#install and not update failed, run purge_packages
purge_packages | tee -a "$logfile"
fi
fi
unset package_name
echo -e "\n\e[91mFailed to ${action} ${app}!\e[39m
\e[40m\e[93m\e[5m◢◣\e[25m\e[39m\e[49m\e[93mNeed help? Copy the \e[1mENTIRE\e[0m\e[49m\e[93m terminal output or take a screenshot.
Please ask on Github: \e[94m\e[4mhttps://github.com/Botspot/pi-apps/issues/new/choose\e[24m\e[93m
Or on Discord: \e[94m\e[4mhttps://discord.gg/RXSTvaUvuu\e[0m" | tee -a "$logfile"
#set the app's status to 'corrupted' if the diagnostics determine the error is NOT internet or system or package, AND if the app is a script-type app
if ! [[ "$(log_diagnose "$logfile" | head -n1)" =~ ^(system|internet|package)$ ]] && [ "$(app_type "$app")" == standard ];then
echo "corrupted" > "${DIRECTORY}/data/status/${app}"
fi
format_logfile "$logfile" #remove escape sequences from logfile
mv "$logfile" "$(echo "$logfile" | sed 's+-incomplete-+-fail-+g')" #rename logfile to indicate it was unsuccessful
fi
#if the app is a package, set its status to whatever dpkg thinks it is
if [ "$(app_type "$app")" == package ];then
refresh_pkgapp_status "$app"
fi
#exit the manage script with the same exit-code of the app's script
exit $exitcode
elif [ "$1" == 'update' ];then #The manage script no longer handles updates. This mode is only for backwards-compatibility and uses the updater script
warning "The manage script ONLY updates apps, and this mode has been replaced by the updater script.
If you want to update Pi-Apps from the command-line, please use:
~/pi-apps/updater cli-yes"
app="$2"
if [ -z "$app" ];then
error "For this operation, you must specify which app to operate on."
elif [ ! -d "${DIRECTORY}/apps/$app" ];then
error "${DIRECTORY}/apps/$app does not exist!"
fi
#get functions from updater script
source "${DIRECTORY}/updater" source
check_repo
updatable_apps="$2"
updatable_files=''
update_now_cli
elif [ "$1" == 'check-all' ];then #The manage script no longer handles updates. This mode is only for backwards-compatibility and uses the updater script
warning "The manage script ONLY updates apps, and this mode has been replaced by the updater script.
If you want to update Pi-Apps from the command-line, please use:
~/pi-apps/updater cli-yes"
#get functions from updater script
source "${DIRECTORY}/updater" source
check_repo
get_updatable_apps
elif [ "$1" == 'update-all' ];then #The manage script no longer handles updates. This mode is only for backwards-compatibility and uses the updater script
warning "The manage script ONLY updates apps, and this mode has been replaced by the updater script.
If you want to update Pi-Apps from the command-line, please use:
~/pi-apps/updater cli-yes"
#get functions from updater script
source "${DIRECTORY}/updater" source
check_repo
updatable_apps="$(get_updatable_apps)"
updatable_files=''
update_now_cli
else
error "Invalid mode. ($1) Allowed values: 'install', 'multi-install', 'install-if-not-installed', 'uninstall', 'multi-uninstall', 'update', 'update-all', 'check-all', or 'daemon'."
fi