-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpdignore.sh~
79 lines (63 loc) · 3.45 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
72
73
74
75
76
77
#!/bin/bash
scriptname=$(realpath "$0")
function editscript(){ if [ "$1" = "edit" ] || [ "$1" = "nano" ]; then (/usr/bin/nano "$scriptname"); exit; fi ; }
editscript "$1"
#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
if [[ -f "$mpdconf" ]] # defines the location of your mpd music directory; this is defined in the mpd.conf file found at /etc/mpd.conf by default
then
musicdir="$(grep -v "^#" "$mpdconf" | grep -v "^$" | grep music_directory)"
musicdir="${musicdir%*\"}"
musicdir="${musicdir#*\"}"
musicdir="${musicdir%/}"
else
musicdir="/library/music" # manually code the location if /etc/mpd.conf doesn't exist.
fi
if [[ -f "$mpdconf" ]] # defines the location of your mpd playlist directory; this is defined in the mpd.conf file found at /etc/mpd.conf by default
then
pldir="$(grep -v "^#" "$mpdconf" | grep -v "^$" | grep playlist_directory)"
pldir="${pldir%*\"}"
pldir="${pldir#*\"}"
pldir="${pldir%/}"
else
pldir="/var/lib/mpd/playlists" # manually code the location if /etc/mpd.conf doesn't exist.
fi
if [[ -f "$mpdconf" ]] # defines the location of your mpd logfile path; this is defined in the mpd.conf file found at /etc/mpd.conf by default
then
mpdlog="$(grep -v "^#" "$mpdconf" | grep -v "^$" | grep log_file)"
mpdlog="${mpdlog%*\"}"
mpdlog="${mpdlog#*\"}"
mpdlog="${mpdlog%/}"
else
mpdlog="/var/log/mpd/mpd.log" # manually code the location if /etc/mpd.conf doesn't exist.
fi
watchfile="$pldir/$watchfile" # sets full path of watchfile
ignoredm3u="$pldir/$ignoredm3u" # sets full path of ignoredm3u
printf "###############################################################entering while loop ################################################################"
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
if [[ "$song" =~ \[ ]] || [[ "$song" =~ \] ]] #individual square bracket characters must be escaped such that they will be recognized
then
song=$(sed -e 's/\\\]/\]/g' -e 's/\\\[/\[/g' -e 's/\[/\\\[/g' -e 's/\]/\\\]/g' <<< "$song" )
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" >> "$ignoredm3u"
printf '%s\n' "$(date '+%b %d %H:%M : player: ignored ')\"$line\"" | cat >> "$mpdlog"
done < "$watchfile"
echo "#last run `date`" >> "$ignoredm3u"
> "$watchfile" # clears the watchfile after run
chmod g+w "$ignoredir"/.mpdignore "$ignoredm3u" "$watchfile" # grants group access to the watchfile, ignoredm3u, and the created/updated .mpdignore file
chown "$mpduser":"$mpdgroup" "$ignoredir"/.mpdignore "$ignoredm3u" "$watchfile" # changes ownership of the above files to mpd and its goup (e.g., so the ignored playlist is readable by mpd)