summaryrefslogtreecommitdiff
path: root/ar/.local/bin/dmenudelmusic
blob: 2efbafa6d189a266cb77523945f16b23c60bd7eb (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
#!/bin/sh

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

# Using POSIX-compliant methods for file selection
selected_filename=$(find "$music_dir" -type f | awk -F/ '{print $NF}' | dmenu -i -l 20 -p "Select a file to delete:") || exit 1

selected_file="$music_dir/$selected_filename"

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

# Confirmation dialog without using echo -e
confirm=$(printf "Yes\nNo" | dmenu -i -p "Delete $selected_filename and update mpc?")

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

# More portable sed command without -i and updating mpc
if grep -v "$video_id" "$music_txt" >"${music_txt}.tmp" && mv "${music_txt}.tmp" "$music_txt"; then
    # Search and remove the filename from playlists
	for playlist in "$playlist_dir"/*.m3u; do
		[ -e "$playlist" ] || continue
		if grep -q "$selected_filename" "$playlist"; then
			grep -v "$selected_filename" "$playlist" > "${playlist}.tmp" && mv "${playlist}.tmp" "$playlist"
			# Remove empty lines
			sed -i '/^$/d' "$playlist"
		fi
	done

    # Delete the music file
    rm "$selected_file"
	mpc update >/dev/null
	notify-send " Success to delete:" "$selected_filename"
else
	notify-send "❌ An error occurred."
fi