-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdignore.sh
executable file
·72 lines (50 loc) · 2.87 KB
/
mpdignore.sh
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
#!/bin/bash
scriptname=$(realpath "$0")
function editscript(){ if [ "$1" = "edit" ] || [ "$1" = "nano" ]; then (/usr/bin/nano "$scriptname"); exit; fi ; }
editscript "$1"
####################################################
#This block is now defined in mpdignore.functions
#pldir="/var/lib/mpd/playlists" # defines playlist directory; default = /var/lib/mpd/playlists
#watchfile=".mpdignore.m3u" # defines the watched playlist; default = .mpdignore.m3u
#mpdconf="/etc/mpd.conf" # defines mpd.conf location; default = /etc/mpd.conf
#mpduser="mpd" # defines the user mpd service runs as; default = mpd
#mpdgroup="media" # defines the group mpd service runs as; default = media
#ignoredm3u="mpdignored.m3u" # defines the playlist of songs ignored by mpdignore; default = mpdignored.m3u
####################################################
. mpdignore.functions
[[ ! "$musicdir" ]] && getmpdmusicdir
[[ ! "$mpdpldir" ]] && getmpdpldir
[[ ! "$mpdlog" ]] && getmpdlog
watchpath="$mpdpldir/$watchfile" # sets full path of watchfile
ignoredpath="$mpdpldir/$ignoredm3u" # sets full path of ignoredm3u
while read -r line; do
song="${line##*/}"
songdir="${line%/*}"
ignoredir="$musicdir/$songdir"
if [[ -f "$ignoredir"/.mpdignore ]]; then
cp "$ignoredir"/.mpdignore "$ignoredir"/.mpdignore~
fi
### mpd does not recognize file names with square brackets in .mpdignore files and the
### individual square bracket characters must be escaped such that they will be recognized
# if [[ "$song" =~ \[ ]] || [[ "$song" =~ \] ]]; then
# song=$(sed -e 's/\\\]/\]/g' -e 's/\\\[/\[/g' -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' <<< "$song" )
# fi
### Found out that it also fails to recognize file names with '\' in them!
# if [[ "$song" =~ \[ ]] || [[ "$song" =~ \] ]] || [[ "$song" =~ \\ ]]; then
# song=$(sed -e 's/\\\]/\]/g' -e 's/\\\[/\[/g' -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' \
# -e 's/\\([^\\]/\\\\\1/g' <<< "$song")
# fi
if [[ "$song" =~ \[ ]] || [[ "$song" =~ \] ]] || [[ "$song" =~ \\ ]]; then
song=$(echo "$song" | -e 's/\\\([^\\]\)/\\\\\1/g' sed -e 's/\[/\\[/g' -e 's/\]/\\]/g')
fi
printf "%s\n# added on %s\n" "$song" "$(date)" >> "$ignoredir"/.mpdignore
printf '%s has been added to .mpdignore in %s\n' "$song" "$ignoredir"
printf '%s\n' "$line" >> "$ignoredpath"
printf '%s\n' "$(date '+%b %d %H:%M : player: ignored ')\"$line\"" | cat >> "$mpdlog"
done < "$watchpath"
printf '#last run %s\n' "$(date)" >> "$ignoredpath"
#: > "$watchpath" # clears the watchfile after run
#
#chmod g+w "$ignoredir"/.mpdignore "$ignoredpath" "$watchpath" # grants group access to the watchfile, ignoredm3u, and the created/updated .mpdignore file
#chown "$mpduser":"$mpdgroup" "$ignoredir"/.mpdignore "$ignoredpath" "$watchpath" # changes ownership of the above files to mpd and its goup (e.g., so the ignored playlist is readable by mpd)
rm "$watchpath"