#!/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