-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclean.sh
More file actions
executable file
·821 lines (740 loc) · 26.1 KB
/
Copy pathclean.sh
File metadata and controls
executable file
·821 lines (740 loc) · 26.1 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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
#!/bin/bash
# Author: Ana Alejandra Castillejo
# Description: Script to clean cache and temporary files for 42 students with Linux/Ubuntu
# Last Update: 27/08/2024
# List of default paths to clean along with their process name
declare -A DEF_PATHS_TO_CLEAN=(
["$HOME/.cache"]="none"
["$HOME/.var/app/com.google.Chrome/cache/"]="chrome"
["$HOME/.config/Code/Cache"]="code"
["$HOME/.config/Code/WebStorage/5/CacheStorage"]="code"
["$HOME/.config/Code/CachedData"]="code"
["$HOME/.config/GitKraken/Cache"]="gitkraken"
["$HOME/snap/slack/common/.cache"]="slack"
["$HOME/snap/slack/149/.config/Slack/Cache"]="slack"
["$HOME/snap/slack/153/.config/Slack/Cache"]="slack"
["$HOME/snap/slack/155/.config/Slack/Cache"]="slack"
["$HOME/snap/slack/158/.config/Slack/Cache"]="slack"
["$HOME/snap/slack/149/.config/Slack/Service\ Worker/CacheStorage"]="slack"
["$HOME/snap/slack/155/.config/Slack/Service\ Worker/CacheStorage"]="slack"
[$HOME/snap/slack/153/.config/Slack/Service\ Worker/CacheStorage]="slack"
["$HOME/snap/slack/158/.config/Slack/Service Worker/CacheStorage"]="slack"
["$HOME/snap/code/common/.cache"]="code"
["$HOME/snap/obsidian/common/.cache"]="obsidian"
["$HOME/snap/gitkraken/common/.cache"]="gitkraken"
["$HOME/francinette/temp"]="none"
["$HOME/.local/share/Trash"]="none"
# Add more paths to clean here with the same format use "none" as process name if not needed
)
# ---------------- INIT VARIABLES ---------------- #
CLEAN_DIR="$HOME/.42cleaner"
CONFIG_FILE="$CLEAN_DIR/clean.conf"
COLOR_FILE="$CLEAN_DIR/colors.bash"
# Space usage variables
total_freed=0
total_skipped=0
process_size=0
# Flags/mode variables
autoupdate=1
safe_mode=1
verbose=0
dry_run=0
interactive=0
list_only=0
force=0
colors=true
# At the end, stays to 1 if there is at least one path != skip/empty
need_clean=0
# Default configuration values
DEFAULT_AUTOUPDATE=1
DEFAULT_SAFE=1 # fixme reconsider this default value (makes -f not work) or delete condition where -s --> force=0
DEFAULT_VERBOSE=0
DEFAULT_DRY_RUN=0
DEFAULT_INTERACTIVE=0
DEFAULT_FORCE=0
DEFAULT_LIST_ONLY=0
DEFAULT_COLORS=true
# SOURCE FILES
if [ -f "$COLOR_FILE" ]; then
source "$COLOR_FILE"
else
echo -e "WARNING: Could not find $COLOR_FILE file.\nIf you want to improve output readability, update the script using --update"
read -r -p "Press enter to continue " yn
fi
# shellcheck source="$HOME/.42cleaner/clean.conf"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
fi
# ----------------------------------------------- #
# FUNCTIONS #
# ----------------------------------------------- #
# ---------------- CONFIGURATION FILE ---------------- #
# Update file with default configuration variables values
update_config_file() {
local append_conf
local var_name
echo "DEFAULT_AUTOUPDATE=$DEFAULT_AUTOUPDATE" > "$CONFIG_FILE"
append_conf=("SAFE" "VERBOSE" "DRY_RUN" "INTERACTIVE" "FORCE" "LIST_ONLY" "COLORS")
for var in "${append_conf[@]}"; do
var_name="DEFAULT_$var"
echo "$var_name=${!var_name}" >> "$CONFIG_FILE"
done
}
# Load defaults from configuration file if it exists or create it with default values
CONFIG_DIR=$(dirname "$CONFIG_FILE")
mkdir -p "$CONFIG_DIR"
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
autoupdate=$DEFAULT_AUTOUPDATE
safe_mode=$DEFAULT_SAFE
verbose=$DEFAULT_VERBOSE
dry_run=$DEFAULT_DRY_RUN
interactive=$DEFAULT_INTERACTIVE
force=$DEFAULT_FORCE
list_only=$DEFAULT_LIST_ONLY
colors=$DEFAULT_COLORS
else
touch "$CONFIG_FILE"
update_config_file
fi
# ---------------- ERROR HANDLER ---------------- #
put_error() {
local type="UNEXPECTED"
local name="$2"
local arg="$3"
local err_msg
if [[ -n $1 ]]; then type="$1"; fi
err_msg="${BRED}$type ERROR${NC}"
if [[ $type != "UNEXPECTED" ]]; then
if [[ $name == "EXC_FLAG" ]]; then
err_msg+=": Too many arguments for exclusive flag"
elif [[ $name == "REP_NOTFOUND" ]]; then
err_msg+=": Repository directory not found at $HOME"
err_msg+="\nPlease make sure the repository is correctly cloned and called '42cleaner'."
else
err_msg+=": Unexpected error $name"
fi
fi
if [[ -n $arg ]]; then err_msg+=" \`$arg\`"; fi
echo -e "$err_msg"
exit 1;
}
# ---------------- UPDATE FUNCTIONS ---------------- #
update_script() {
local repo_dir
local local_hash
local remote_hash
local update
update=1
repo_dir=$(find "$HOME" -type f -ipath '*/42cleaner/clean.sh*' | sort -n | head -n 1)
if [ -z "$repo_dir" ]; then put_error "UPDATE" "REP_NOTFOUND"; fi
cd $(dirname "$repo_dir") || exit
git fetch --quiet
#Compare the local and remote hashes (check if is up-to-date)
local_hash=$(git rev-parse HEAD)
remote_hash=$(git rev-parse @{u})
if [ "$local_hash" != "$remote_hash" ] || [ $1 == "force" ]; then
if [ $1 == "auto" ]; then
echo -en "${BMAGENTA}AUTOUPDATE: ${NC}"
echo -e "A new version of the script has been found"
read -r -p "Do you want to update the script? (y/n) " yn
case $yn in
[Yy]* ) ;;
* ) update=0; echo -e "The script has not been updated. Use flag: -a or --auto_update 0 to disable\n";;
esac
fi
if [ $update == 1 ]; then
git pull origin main --quiet || exit
if [ ! -d "$CLEAN_DIR" ]; then
mkdir -p "$CLEAN_DIR"
cp utils/clean.conf "$CLEAN_DIR"
fi
cp clean.sh "$CLEAN_DIR"
cp utils/colors.bash "$CLEAN_DIR"
echo -e "${GREEN}Update completed succesfully${NC}"
exit 0
fi
fi
}
# Update color variables value depending on user configuration
update_color_variables() {
if [ "$colors" == "true" ] || [ "$colors" == "1" ] && [[ -t 1 ]] && [ -f "$COLOR_FILE" ]; then
activate_color
else
colors=false
fi
}
update_color_variables
# ---------------- HELP MESSAGE FUNCTIONS ---------------- #
# Functions for displaying help message (option -h/--help)
# Print usage example depending on if there is an existing alias or not
print_usage() {
alias_name=$($SHELL -ic alias | grep "='\$HOME/.42cleaner/clean.sh" | awk -F'=' '{print $1}' )
if [ -z "$alias_name" ]; then
echo "./clean.sh"
else
echo "$alias_name"
fi
}
print_help() {
repo_path=$(find "$HOME" -type d -name '42cleaner' | sort | awk 'NR==1{print}')
clean_cmd=$(print_usage)
echo -e "${BOLD}DESCRIPTION${NC}"
echo -e "\tThis script cleans cache and temporary files in Linux/Ubuntu"
echo -e "\tIt helps to free up disk space and maintain system performance"
echo -e "${BOLD}USAGE${NC}"
echo -e "\t $clean_cmd [options]"
echo -e "${BOLD}OPTIONS${NC}"
echo -e "\t-h, --help"
echo -e "\t\tDisplay this help message."
echo -e "\t-u, --update"
echo -e "\t\tForces script update from the repository."
echo -e "\t-v, --verbose"
echo -e "\t\tVerbose mode."
echo -e "\t\tShow files deleted/to delete and their sizes."
echo -e "\t-n, --dry-run"
echo -e "\t\tDry run mode (Also enables verbose mode)"
echo -e "\t\tOnly show what would be deleted without actually deleting anything."
echo -e "\t-i, --interactive"
echo -e "\t\tInteractive mode."
echo -e "\t\tAsk for confirmation before deleting each file or directory."
echo -e "\t-l, --list"
echo -e "\t\tList mode."
echo -e "\t\tONLY list all directories and files to be cleaned without deleting."
echo -e "\t-f, --force"
echo -e "\t\tForce mode."
echo -e "\t\tDelete cache without asking for confirmation of running processes."
echo -e "\t-s, --safe"
echo -e "\t\tSafe mode."
echo -e "\t\tTemporarily disables force mode and checks the running processes."
echo -e "\t-D [mode]"
echo -e "\t\tSet default mode of the script to the provided mode"
echo -e "\t\tExample: '$clean_cmd -D v' enables verbose mode by default"
echo -e "\t-U [mode]"
echo -e "\t\tUnset default mode of the script for the provided mode"
echo -e "\t\tExample: '$clean_cmd -u v' to disable verbose mode by default"
echo -e "\t-R"
echo -e "\t\tReset default modes of the script to the original values."
echo -e "\t-a, --auto_update [${GREEN}1${NC}|${RED}0${NC}]"
echo -e "\t\tEnable or disable automatic updates. Can also be enabled with '-D a' and disabled with '-U a'"
echo -e "\t--color [${GREEN}true${NC}|${RED}false${NC}]"
echo -e "\t\tEnable or disable color output."
echo -e "\t\tValid values are \`true\`, \`1\`, \`false\`, \`0\`."
echo -e "\t--set-default-color [${GREEN}true${NC}|${RED}false${NC}]"
echo -e "\t\tSet the default color output in the configuration file."
echo -e "\t\tValid values are \`true\`, \`1\`, \`false\`, \`0\`."
echo -e "${BOLD}CONFIGURATION${NC}"
echo -e "\tConfiguration file for modifying the default behaviour of the script"
echo -e "\tFile location: \`$HOME/.42cleaner/clean.conf\`."
echo -e "\tIn order to modify it you can:"
echo -e "\t\t1. Manually modify the file directly"
echo -e "\t\t2. Use the following options of $clean_cmd in the command line:"
echo -e "\t\t \`-D\`, \`-U\`, \`-R\` and \`--set-default-color\`"
echo -e "\t\tThey allow to change behaviour from the command line."
echo -e "${BOLD}MORE HELP${NC}"
echo -e "\tFor more information, please refer to the project's Documentation at:"
# Looking for the repository directory in $HOME and print the path if found
if [ -z "$repo_path" ]; then
echo -e "\tRepository not found locally. You can find the Documentation files online"
else
echo -e "\tLocally: $repo_path/docs/"
fi
echo -e "\tOnline: https://github.com/jandrana/42cleaner/blob/main/docs"
echo -e "${BOLD}SEE ALSO${NC}"
echo -e "\tThe /utils folder contains auxiliary files with additional functionalities."
echo -e "\tFiles: \`clean.conf\`, \`process_name.sh\` and \`find_cache.sh\`"
echo -e "\tFor more details, refer to their specific documentation at /docs"
echo -e "${BOLD}AUTHOR${NC}"
echo -e "\tDeveloped by: Jandrana"
echo -e "\t GitHub: https://github.com/jandrana"
echo -e "\t 42 user: ana-cast"
echo -e "${BOLD}COPYRIGHT${NC}"
echo -e "\tThis project is licensed under the MIT License."
}
# ---------------- STORAGE/SIZE RELATED FUNCTIONS ---------------- #
# Calculate color output depending on given number of bytes (size)
get_size_color() {
local size=$1
if [ "$size" -lt $((1024 * 1024)) ]; then # < 1MB
echo "${BLUE}"
elif [ "$size" -lt $((50 * 1024 * 1024)) ]; then # 1MB-50MB
echo "${GREEN}"
elif [ "$size" -lt $((100 * 1024 * 1024)) ]; then # 50MB-100MB
echo "${RED}"
else # > 100MB
echo "${MAGENTA}"
fi
}
# Print given size in a readable format with color
print_size_color() {
local size=$1
local size_color
size_color=$(get_size_color "$size")
echo -e "${size_color}$(numfmt --to=iec --suffix=B "$size")${NC}"
}
print_size() {
local size=$1
echo -e "${BOLD}$(numfmt --to=iec --suffix=B "$size")${NC}"
}
# Get storage usage of home directory in a readable format
get_storage_usage() {
df -h "$HOME" | awk 'NR==2 {print $4}'
}
# Print storage usage of home directory
print_storage_usage() {
echo -e "\tAvailable space in $HOME: ${BOLD}$(get_storage_usage)${NC}"
}
# Function to get the size of a given path
get_path_size() {
local path=$1
if [ -e "$path" ]; then
du -sb "$path" | awk '{print $1}'
else
echo 0
fi
}
# Function to sort and print an array of paths by their size from biggest to smallest
print_paths_sorted() {
local indent="$1"
shift
local paths=("$@")
local sorted
# Print sorted paths with their sizes
mapfile -t sorted < <(sort_paths_by_size "${paths[@]}")
for path in "${sorted[@]}"; do
echo -e "$indent$(print_size "$(get_path_size "$path")")\t$path"
done
}
# Function to return an array after sorting given array of paths by their size from biggest to smallest
sort_paths_by_size() {
local paths=("$@")
local sorted_paths
declare -A path_sizes
# Calculate sizes of paths
for path in "${paths[@]}"; do
path_sizes["$path"]="$(get_path_size "$path")"
done
# Sort paths by size and handle spaces correctly
while IFS= read -r line; do
sorted_paths+=("$line")
done < <(for path in "${!path_sizes[@]}"; do
echo "${path_sizes[$path]}:$path"
done | sort -t: -nrk1 | cut -d: -f2-)
# Return sorted paths
printf "%s\n" "${sorted_paths[@]}"
}
get_process_decision() {
local proc_msg
if [[ ${#running_processes[@]} != 0 ]]; then
echo -en "${BORANGE}SAFE MODE:${NC}"
echo -e " It is recommended to close ${BORANGE}${!running_processes[*]}${NC} before cleaning cache"
fi
for process in "${!running_processes[@]}"; do
process_size=0
if [ -z "${process_decision[$process]}" ]; then
echo -e "${BORANGE} - $process${NC}${NC} is running: It is recommended to close it before cleaning its cache.${NC}"
IFS=';' read -ra paths <<< "${running_processes[$process]}"
for path in "${paths[@]}"; do
path_size=$(get_path_size "$path")
process_size=$((process_size + path_size))
done
# echo -e "\t${BOLD}SIZE\tPROCESS PATHS"
print_paths_sorted "\t" "${paths[@]}"
proc_msg="(${BOLD}TOTAL${NC}=$(print_size_color "$process_size"))"
while true; do
read -r -p " Do you want to delete paths related to ${BOLD}$process${NC}? $proc_msg (y/n) " yn
case $yn in
[Yy]* ) process_decision["$process"]="yes"; break ;;
[Nn]* ) process_decision["$process"]="no"; break ;;
* ) echo "Please answer yes or no." ;;
esac
done
fi
done
}
assign_process_decision() {
local skipping
local cleaning
for process in "${!running_processes[@]}"; do
if [ "${process_decision[$process]}" == "no" ]; then
IFS=';' read -ra paths <<< "${running_processes[$process]}"
for path in "${paths[@]}"; do
DEF_PATHS_TO_CLEAN["$path"]="skip"
done
skipping+="$process "
else
IFS=';' read -ra paths <<< "${running_processes[$process]}"
for path in "${paths[@]}"; do
DEF_PATHS_TO_CLEAN["$path"]="delete"
done
cleaning+="$process "
fi
done
if [[ ${#running_processes[@]} != 0 ]]; then
echo -en "${BORANGE}SUMMARY: ${NC}"
if [[ -n $skipping ]]; then
echo -en "Skipping ${RED}$skipping${NC}"
fi
if [[ -n $cleaning ]]; then
echo -en "Cleaning ${GREEN}$cleaning${NC}"
fi
echo -e "\n"
fi
}
# Function to handle processes that are running and want to be cleaned
check_running_process() {
declare -A process_decision
declare -A running_processes
for path in "${!DEF_PATHS_TO_CLEAN[@]}"; do
if [ "${DEF_PATHS_TO_CLEAN[$path]}" == "skip" ] || [ "${DEF_PATHS_TO_CLEAN[$path]}" == "empty" ]; then
continue
fi
process="${DEF_PATHS_TO_CLEAN[$path]}"
if [ "$process" != "none" ] && pgrep -x "$process" > /dev/null; then
if [ -z "${running_processes[$process]}" ]; then
running_processes["$process"]="$path"
else
running_processes["$process"]+=";$path"
fi
fi
done
get_process_decision
assign_process_decision
return 0
}
# Function to delete files and folders and calculate freed space
clean_paths() {
local path=$1
local path_size_before
if [ -e "$path" ]; then
path_size_before=$(get_path_size "$path")
if [ "$dry_run" -eq 0 ]; then
rm -rf "$path"
fi
total_freed=$((total_freed + $path_size_before))
if [ "$verbose" -eq 1 ] && [ "$path_size_before" -gt 0 ]; then
echo -e "\t$(print_size_color "$path_size_before")\t$path"
fi
fi
}
put_new_default() {
local mode=$1
local opt="set"
if [ "$2" -eq 0 ]; then
opt="unset"
fi
echo -en "${ORANGE}Default mode"
if [[ "$mode" == *\ * ]]; then
echo -en "s"
fi
echo -e " $opt to:$mode"
}
# Array of options to be passed to getopt
SHORT_OPTIONS='hvnilfsa:D:U:Ru'
LONG_OPTIONS='help,verbose,dry-run,interactive,list,force,safe,update,color:,set-default-color:auto_update:'
# getopt reported failure
if ! OPTIONS=$(getopt -o $SHORT_OPTIONS --long $LONG_OPTIONS -n "${BRED}USAGE ERROR${NC}" -- "$@"); then
echo -e "Try '$(basename "$0") --help' for more information." 1>&2
exit 1
fi
eval set -- "$OPTIONS"
# Parse command/script flags
while true; do
case "$1" in
-D|-U)
if [ $# -ne 3 ]; then
put_error "USAGE" "EXC_FLAG" "$1"
fi
if [[ $1 == "-D" ]]; then
DEFAULT_VALUE=1
else
DEFAULT_VALUE=0
fi
# Set new defaults based on the provided modes
message=${NC};
if [[ "$2" == *a* ]]; then DEFAULT_AUTOUPDATE=$DEFAULT_VALUE; message+="\n - Automatic Updates"; fi
if [[ "$2" == *s* ]]; then DEFAULT_SAFE=$DEFAULT_VALUE; message+="\n - Safe"; fi
if [[ "$2" == *v* ]]; then DEFAULT_VERBOSE=$DEFAULT_VALUE; message+="\n - Verbose"; fi
if [[ "$2" == *n* ]]; then DEFAULT_DRY_RUN=$DEFAULT_VALUE; message+="\n - Dry-run"; fi
if [[ "$2" == *i* ]]; then DEFAULT_INTERACTIVE=$DEFAULT_VALUE; message+="\n - Interactive"; fi
if [[ "$2" == *f* ]]; then DEFAULT_FORCE=$DEFAULT_VALUE; message+="\n - Force"; fi
if [[ "$2" == *l* ]]; then DEFAULT_LIST_ONLY=$DEFAULT_VALUE; message+="\n - List-only"; fi
if [[ $message != "${NC}" ]]; then put_new_default "$message" $DEFAULT_VALUE;
else echo -e "${BORANGE}INFO:${NC} No valid modes provided, no changes made"; fi
update_config_file
exit 0
;;
-R)
if [ $# -ne 2 ]; then
put_error "USAGE" "EXC_FLAG" "$1"
fi
echo -e "${ORANGE}Resetting default modes to original values${NC}"
DEFAULT_AUTOUPDATE=1
DEFAULT_SAFE=1
DEFAULT_VERBOSE=0
DEFAULT_DRY_RUN=0
DEFAULT_INTERACTIVE=0
DEFAULT_FORCE=0
DEFAULT_LIST_ONLY=0
DEFAULT_COLORS=true
update_config_file
exit 0
;;
--color)
shift
case "$1" in
true|1|false|0)
colors=$1
update_color_variables
echo -e "Setting color output to ${GREEN}$1${NC}"
shift
;;
*)
echo -e "${RED}Invalid value for --color. Expected: true, 1, false, 0.${NC}"
exit 1
;;
esac
;;
--set-default-color)
if [ $# -ne 3 ]; then
put_error "USAGE" "EXC_FLAG" "$1"
fi
shift
case "$1" in
true|1|false|0)
colors=$1
update_color_variables
update_config_file
echo -e "Setting default color output to ${GREEN}$1${NC}"
shift
;;
*)
echo -e "${RED}Invalid value for --set-default-color. Valid values are true, 1, false, 0.${NC}"
exit 1
;;
esac
exit 0
;;
-a|--auto_update)
if [ $# -ne 3 ]; then
put_error "USAGE" "EXC_FLAG" "$1"
fi
shift
case "$1" in
false|0)
autoupdate=0
DEFAULT_AUTOUPDATE=$autoupdate
update_config_file
echo -e "Automatic updates disabled"
shift
;;
true|1)
autoupdate=1
DEFAULT_AUTOUPDATE=$autoupdate
update_config_file
echo -e "Automatic updates enabled"
shift
;;
*)
echo -e "${RED}Invalid value for --auto_update. Valid values are true, 1, false, 0.${NC}"
exit 1
;;
esac
exit 0
;;
-h|--help)
print_help
exit 0
;;
-u|--update)
echo -e "Forcing script to update"
update_script "force"
exit 0
;;
-v|--verbose)
verbose=1
shift
;;
-n|--dry-run)
dry_run=1
shift
;;
-i|--interactive)
interactive=1
verbose=1
shift
;;
-l|--list)
list_only=1
dry_run=1
shift
;;
-f|--force)
force=1
shift
;;
-s|--safe)
safe_mode=1
shift
;;
--)
shift
break
;;
\?)
echo -e "${BRED}USAGE ERROR${NC}: invalid option -- $OPTARG"
echo -e "Try '$(basename "$0") --help' for more information." 1>&2
exit 1
;;
esac
done
check_valid_paths() {
local empty=1
if [[ ${#DEF_PATHS_TO_CLEAN[@]} -ne 0 ]]; then
for path in "${!DEF_PATHS_TO_CLEAN[@]}"; do
if [[ ! -e $path ]] || [ "$(get_path_size "$path")" -lt $((1024 * 1024)) ]; then
DEF_PATHS_TO_CLEAN["$path"]="empty"
else
empty=0
fi
done
fi
if [[ $empty -eq 1 ]]; then
echo -e "${NOTE} No paths found for cleaning\n"
fi
}
if [ $autoupdate == 1 ]; then
update_script "auto"
fi
check_valid_paths
# Ensure that safe mode overrides force mode when enabled
if [ "$safe_mode" -eq 1 ]; then
force=0
fi
if [ "$dry_run" -eq 1 ] && [ "$list_only" -eq 0 ]; then
verbose=1
echo -e "${BRED}DRY-RUN:${NC} SIMULATION MODE ACTIVE. NO FILES WILL BE DELETED${RESET}\n"
fi
# Print the current storage available of home directory
if [ "$list_only" -eq 0 ]; then
before_cleaning=$(get_storage_usage)
if [ "$force" -eq 0 ]; then
check_running_process
fi
else
interactive=0
fi
# Create an array of the final paths to clean (excluding the ones marked as "skip")
# If interactive mode is enabled
# - Ask for confirmation before adding path to final_paths (want to delete?)
# - Yes: add path to the final paths to clean
# - No: mark path as "skip" in DEF_PATHS_TO_CLEAN
if [ $interactive -eq 1 ]; then
echo -e "${BBLUE}INTERACTIVE MODE:${NC} Please indicate which paths you want to delete:"
fi
for path in "${!DEF_PATHS_TO_CLEAN[@]}"; do
if [ "${DEF_PATHS_TO_CLEAN[$path]}" == "skip" ] || [ "${DEF_PATHS_TO_CLEAN[$path]}" == "empty" ]; then
continue
else
if [ "$interactive" -eq 1 ] && [ "${DEF_PATHS_TO_CLEAN[$path]}" != "delete" ]; then
while true; do
echo -en "\t"
read -r -p "Delete $path for $(print_size_color "$(get_path_size "$path")")? (y/n) " yn
case $yn in
[Yy]* ) final_paths_to_clean+=("$path"); break ;;
[Nn]* ) DEF_PATHS_TO_CLEAN["$path"]="skip"; break;;
* ) echo "Please answer yes or no." ;;
esac
done
else
final_paths_to_clean+=("$path")
fi
fi
done
if [ $interactive -eq 1 ]; then
echo -e ""
fi
# Use mapfile to correctly read paths with spaces
mapfile -t sorted_paths_list < <(sort_paths_by_size "${final_paths_to_clean[@]}")
if [[ ${#sorted_paths_list[@]} != 0 ]] && [[ -n "${sorted_paths_list[*]}" ]]; then # todo check 2nd condition
need_clean=1
fi
# Print heading line before printing paths in verbose and list only mode
if [ "$list_only" -eq 1 ]; then
echo -e "\n${BOLD}${MAGENTA}LIST ONLY:${NC}"
if [[ $need_clean -eq 0 ]]; then
echo -e "\t${BOLD}${MAGENTA}NO PATHS TO CLEAN${NC}"
else
echo -e "\t${BOLD}${MAGENTA}PATHS TO CLEAN${NC}"
fi
elif [ "$verbose" -eq 1 ]; then
echo -en "${BOLD}${MAGENTA}VERBOSE SUMMARY:\n\t"
if [[ $need_clean -eq 0 ]]; then
echo -en "NO PATHS "
fi
if [ "$dry_run" -eq 1 ]; then
echo -e "TO DELETE${NC}"
else
echo -e "DELETED${NC}"
fi
fi
# Clean paths in sorted order or list in list_only mode
if [ $need_clean -eq 1 ]; then
for path in "${sorted_paths_list[@]}"; do
if [ "$list_only" -eq 1 ] && [[ -e $path ]]; then
echo -e "\t$(print_size_color "$(get_path_size "$path")")${NC}\t$path"
else
clean_paths "$path"
fi
done
fi
# List skipped (not cleaned) paths in verbose mode + total size of skipped paths
# Skipped paths are those that have been marked as "skip":
# - User decision:
# - Paths that had a process running and user chose not to clean
# - Paths that user chose not to clean in interactive mode
# - Paths that which size is less than 1KB
mapfile -t sorted_all_paths < <(sort_paths_by_size "${!DEF_PATHS_TO_CLEAN[@]}")
if [ "$verbose" -eq 1 ]; then
if [ "$list_only" -eq 1 ]; then
echo -e "\n\t${BOLD}${MAGENTA}IGNORED PATHS < 1M${NC}"
for path in "${sorted_all_paths[@]}"; do
if [ "${DEF_PATHS_TO_CLEAN[$path]}" == "empty" ]; then
DEF_PATHS_TO_CLEAN[$path]="skip"
fi
done
else
echo -e "\n\t${BBLUE}SKIPPED${NC}"
fi
for path in "${sorted_all_paths[@]}"; do
if [[ -n "${DEF_PATHS_TO_CLEAN[*]}" ]] && [ "${DEF_PATHS_TO_CLEAN[$path]}" == "skip" ]; then
total_skipped=$((total_skipped + $(get_path_size "$path")))
echo -e "\t$(print_size_color "$(get_path_size "$path")")${NC}\t$path"
fi
done
if [ "$total_skipped" -eq 0 ]; then
echo -e "\tPATHS SMALLER THAN 1MB\n" # fixme NOT WORKING VERBOSE SKIPPED LIST (tested with flags -ni)
else
echo -e "\n\t$(get_size_color "$total_skipped")TOTAL SKIPPED: ${BOLD}$(print_size_color "$total_skipped")"
fi
fi
# Convert total freed to readable format
total_freed_read=$(print_size_color "$total_freed")
# Print total freed space and home storage available before/after cleaning
if [ "$list_only" -eq 0 ]; then
if [ $total_freed -ne 0 ]; then
echo -e "\t$(get_size_color "$total_freed")TOTAL CLEAN: ${BOLD}$total_freed_read\n"
echo -en "Total available space:"
if [ "$before_cleaning" != "$(get_storage_usage)" ]; then
echo -e "\n\t${BRED}BEFORE: ${NC}${before_cleaning}${NC}"
echo -ne "\t${BGREEN}AFTER: ${NC}"
fi
fi
fi
if [ $total_freed -eq 0 ] || [ "$list_only" -eq 1 ]; then
echo -en "\n Total available space:"
fi
echo -e " ${BOLD}$(get_storage_usage)\n${NC}"
if [ $dry_run -eq 1 ] && [ $list_only -eq 0 ]; then
echo -e "${BRED}DRY-RUN:${NC} END OF SIMULATION. NO FILES WERE DELETED"
fi