shell/mpd/bin/cleanup-music: refactor

This commit is contained in:
Henrik Lissner 2017-04-19 14:20:27 -04:00
parent afcd61a20f
commit 1db6924d0b

View file

@ -1,28 +1,28 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Delete all songs that are in the delete-pls playlist, and cleans up any empty folders # I frequently discover some of my music is simply awful. So I file them away in
# it leaves behind. # my 1-star.m3u playlist. This script reads those songs, deletes them, and
# cleans up any empty folders it leaves behind.
#
# Usage: cleanup-music
set -e set -e
shopt -s nullglob shopt -s nullglob
playlistdir=$(grep playlist_directory "$HOME/.config/mpd/mpd.conf" | awk '{ print $2; }') musicdir=$(awk '/music_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
playlistdir="${playlistdir//\"/}" musicdir="${musicdir/#\~/$HOME}"
playlistfile="${playlistdir/#\~/$HOME}/1-star.m3u" playlistdir=$(awk '/playlist_directory/ { print gensub(/"/, "", "g", $2); }' "$HOME/.config/mpd/mpd.conf")
songs=$(cat "$playlistfile") playlistdir="${playlistdir/#\~/$HOME}"
playlistfile="${playlistdir}/1-star.m3u"
# Delete designated songs # Delete designated songs
IFS=" IFS=$'\n'
"
declare -a to_delete=() declare -a to_delete=()
for song in "${songs[@]}"; do while read -r song; do
fpath="$HOME/music/$song" songpath="${musicdir}/${song}"
if [[ -f $fpath ]]; then [[ -f $songpath ]] && to_delete+=("$songpath")
to_delete+=("$fpath") done < "$playlistfile"
# echo "Deleting: $song"
# rm -f "$fpath"
fi
done
if (( ${#to_delete[@]} == 0 )); then if (( ${#to_delete[@]} == 0 )); then
echo "Nothing to delete" echo "Nothing to delete"
@ -36,7 +36,7 @@ read -r confirm
case $confirm in case $confirm in
y|Y) y|Y)
echo "Deleting..." echo "Deleting..."
for file in ${to_delete[@]}; do for file in "${to_delete[@]}"; do
rm -fv "$file" rm -fv "$file"
done done
;; ;;
@ -45,8 +45,8 @@ case $confirm in
esac esac
:>"$playlistfile" :>"$playlistfile"
# Remove duplicates in playlists # Remove duplicates across all playlists
for playlist in "$playlistdir"/*.m3u; do for playlist in ${playlistdir}/*.m3u; do
awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp" awk '!seen[$0]++' "${playlist}" > "${playlist}.tmp"
if [[ -f "${playlist}.tmp" ]]; then if [[ -f "${playlist}.tmp" ]]; then
rm -f "$playlist" rm -f "$playlist"