forked from surtarso/bash-media-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmm-dir_cleaner
executable file
·146 lines (126 loc) · 4.85 KB
/
bmm-dir_cleaner
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
#! /bin/bash
## TODO:
## -add GUI/no-gui
## -dynamic format to keep (currently only mp3)
## Load enviroment variables
if [ -f $HOME/bmm/.env ]; then
. $HOME/bmm/.env
else
echo -e "FATAL ERROR: .env not found.$\n
Use bmm-setup_enviroment to create one.\nAborting...";
exit 1;
fi
# Warn the user about the script's actions.
cli_menu() {
while true; do
echo -e "
${ERROR}Bash Media Manager - Empty Dir Cleaner${NC}
${WARN}This script will ${ERROR}DELETE${WARN} folders without ${ERROR}.mp3${WARN} files inside them.
It assumes ${OK}Artist(metadata)/Album(mp3+metadata)${WARN} hierarchy.
${ERROR}Make sure all your audio is converted to ${ERROR}MP3${WARN} before continuing.
${OK}Please select an option:${NC}
1. Start the cleaning process
2. Run 'bmm-converter_mp3' to convert audio to MP3 and continue
3. Abort the script"
echo -n -e "${OK} Enter the number of your choice: ${NC} "
read -r choice
case "$choice" in
1)
echo "Starting the cleaning process..."
remove_dirs
break
;;
2)
echo "Running 'bmm-converter_mp3'..."
eval bmm-converter_mp3 -a
echo "Starting the cleaning process..."
remove_dirs
break
;;
3)
echo "Aborted."
exit 1
;;
*)
echo "Invalid option. Please select a valid option."
;;
esac
done
}
remove_dirs () {
# Show banner
if ! command -v $BANNER >> /dev/null; then echo -e "${WARN}BASH MEDIA MANAGER${NC}"; else eval $BANNER; fi
echo -e "${HEAD}Dir Cleaner${NC}"
echo '----------------------------------------------------------'
dir_check "$AUDIOROOT"
# Enter music root dir
cd "$AUDIOROOT" || { echo 'ERROR: Could not enter directory.'; exit 1; };
echo -e "${ERROR}Deleting folders with no MP3...${NC}"
echo '----------------------------------------------------------'
# Capture start time
start_time=$(date +%s)
assume_yes=false # Flag for ALL in y/n/a
deleted_count=0 # Counter for deleted directories
# Search for folder with no mp3 in them
# Does not delete nested folders (artist(no mp3)/album(mp3))
while IFS= read -r -d '' D; do
v=$(find "$D" -iname '*.mp3')
case "$v" in
"" )
echo -e "->${WARN} $D ${ERROR}has no MP3!${NC}"
ls -A "$D" # List the directory contents
if [ "$assume_yes" = true ]; then
choice="y" # Automatically assume Y for subsequent directories
else
read -p "Do you want to delete the directory '$D'? (y/n/a): " choice < /dev/tty
fi
case "$choice" in
[Yy]* )
sudo rm -rf "$D" # DELETE THE DIRECTORY!
echo -e "${ERROR}Directory deleted.${NC}"
# Append the deleted directory and its contents to the log file
((deleted_count++)) # Increment the deleted count
;;
[Nn]* )
echo -e "${OK}Directory not deleted.${NC}"
;;
[Aa]* )
echo -e "${WARN}Automatically assuming ${OK}'yes'${WARN} for all subsequent directories.${NC}"
assume_yes=true
;;
* )
echo -e "${ERROR}Invalid choice. ${WARN}Directory not deleted.${NC}"
;;
esac
;;
esac
done < <(find -depth -type d -print0)
# Capture end time
end_time=$(date +%s)
# Calculate elapsed time
elapsed_time=$((end_time - start_time))
if [ "$deleted_count" -gt 0 ]; then
echo -e "${WARN}A total of ${ERROR}$deleted_count${WARN} empty directories were deleted.${NC}"
else
echo -e "${OK}No directories were deleted.${NC}"
fi
echo -e "${OK}Done.${NC} Execution time: ${elapsed_time} seconds"
}
# START ARGS
if [ "$1" = '--convert' ] || [ "$1" = '-c' ]; then
echo '2' | cli_menu;
elif [ "$1" = '--auto' ] || [ "$1" = '-a' ]; then
echo '1' | cli_menu;
elif [ "$1" = '--help' ] || [ "$1" = '-h' ]; then
if ! command -v $BANNER >> /dev/null; then echo -e "${WARN}BASH MEDIA MANAGER${NC}"; else eval $BANNER; fi
echo -e "${HEAD}Dir Cleaner"
echo -e "${OK}Removes directories with no MP3's in them.\n${NC}"
echo -e "Use with no arguements to enter the menu."
echo -e "Usage: bmm-dir_cleaner [-arg] [--arg]\n"
echo -e " -c [--convert] -> Converts audio do mp3 before cleaning."
echo -e " -a [--auto] -> Just clean."
echo -e " -h [--help] -> This message screen."
exit 1
else
cli_menu;
fi