summaryrefslogtreecommitdiff
path: root/ar/.local/bin/dmenudelmusic
blob: 91994fbd9d7c26d683cb5bac8d2e791a7a185fa8 (plain)
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
#!/bin/sh

music_dir="${XDG_MUSIC_DIR:-${HOME}/Music}"
music_txt="${music_dir}/.music.txt"
playlist_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/mpd/playlists"

# Select the relative path. Sort the list so "first match" semantics in dmenu
# are deterministic — otherwise filesystem order is unstable and typing a
# substring (e.g. "성시경") can return whichever file find walked first.
# `-not -path '*/.*'` skips hidden files (.music.txt, etc.); name filter limits to audio.
selected_relpath=$(cd "$music_dir" && find . -type f -not -path '*/.*' \( -name '*.mp3' -o -name '*.m4a' -o -name '*.opus' -o -name '*.flac' -o -name '*.wav' -o -name '*.webm' \) | sed 's|^\./||' | LC_ALL=C.UTF-8 sort | dmenu -i -l 20 -p "Select a file to delete:") || exit 1
[ -n "$selected_relpath" ] || exit 1

selected_file="$music_dir/$selected_relpath"
selected_filename=${selected_relpath##*/}

[ -f "$selected_file" ] || {
	notify-send "❌ File not found: $selected_relpath"
	exit 1
}

# Extracting YouTube video ID
video_id=$(strings "$selected_file" | grep 'watch?v=' | sed 's/.*watch?v=\([a-zA-Z0-9_-]*\).*/\1/' | head -1)
[ -n "$video_id" ] || {
	notify-send "❌ No YouTube video ID found in file: $selected_filename"
	exit 1
}

# Confirmation dialog
confirm=$(printf "Yes\nNo" | dmenu -i -p "Delete $selected_relpath and update mpc?")

[ "$confirm" = "Yes" ] || {
	notify-send "❌ Operation cancelled."
	exit 0
}

# Remove video_id line from .music.txt (fixed-string match)
if grep -Fv "$video_id" "$music_txt" >"${music_txt}.tmp" && mv "${music_txt}.tmp" "$music_txt"; then
	# Remove the relative path from playlists (-F avoids regex pitfalls with [], (), etc.)
	for playlist in "$playlist_dir"/*.m3u; do
		[ -e "$playlist" ] || continue
		if grep -Fq "$selected_relpath" "$playlist"; then
			grep -Fv "$selected_relpath" "$playlist" > "${playlist}.tmp" && mv "${playlist}.tmp" "$playlist"
			sed -i '/^$/d' "$playlist"
		fi
	done

	# Delete the actual music file
	if rm "$selected_file"; then
		mpc update >/dev/null
		notify-send " Success to delete:" "$selected_relpath"
	else
		notify-send "❌ Failed to remove file: $selected_file"
	fi
else
	notify-send "❌ An error occurred."
fi