-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplmgr
executable file
·146 lines (133 loc) · 3.67 KB
/
plmgr
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh
#
# NAME
#
# plmgr - a media player agnostic playlist manager
#
# DESCRIPTION
#
# plmgr is a media player agnostic playlist manager. Its only
# purpose is to add/delete the currently playing track from a playlist
# file.
#
# SYNOPSIS
#
# plmgr [-r] [<playlist>...]
# plmgr [-r] -a
# plmgr -e [-a] [<playlist>]
#
# -r remove song from all the playlists
# -a autoselect playlist based on artist name
# -e edit playlist in $EDITOR
#
# DEPENDENCIES
#
# DeaDBeeF, iconv with //TRANSLIT
#
set -u
: ${PLMGR_PLAYLIST_DIR:="${HOME}/music/playlists"}
: ${PLMGR_PLAYLIST_DEFAULT:="miscellaneous.m3u"}
mkdir -p "$PLMGR_PLAYLIST_DIR"
die() {
printf >&2 "plmgr: %s\n" "$*"
exit 1
}
remove="no"
autoartist="no"
edit="no"
while getopts ":aehr" opt
do
case "$opt" in
a) autoartist="yes" ;;
e) edit="yes" ;;
h) sed -n '/^# NAME$/, /^$/ { s/^# \{0,1\}//; p }' <"$0"
exit ;;
r) remove="yes" ;;
:) die "-${OPTARG} requires an argument" ;;
\?) die "-${OPTARG} is not a valid option" ;;
esac
done
shift $((OPTIND - 1))
audio=$(deadbeef --nowplaying %F 2>/dev/null)
test "$audio" = "nothing" && die "no song playing"
if test "$autoartist" = "yes"
then
# Canonicalize artist name and form a safe lowercase filename.
artist=$(deadbeef --nowplaying %a 2>/dev/null |
iconv -c -f 'UTF-8' -t 'ASCII//TRANSLIT' |
sed -e "s/ [fF][eE][aA][tT][ .:-]\{1,\}.*\$//
s/([^)]*)//g
s/{[^}]*}//g
s/\[[^]]*\]//g
s/['\"]*//g
s/^[^[:alnum:]]\{1,\}//
s/[^[:alnum:]]\{1,\}\$//
s/[^[:alnum:]._-]\{1,\}/_/g
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
s/^the_//")
if test "$artist"
then
set -- "${artist}.m3u"
else
die "unable to parse artist info"
fi
elif test $# -eq 0
then
set -- "$PLMGR_PLAYLIST_DEFAULT"
fi
test "$edit" = "yes" && exec "$EDITOR" -- "${PLMGR_PLAYLIST_DIR}/${1}"
for m3u
do
m3u="${PLMGR_PLAYLIST_DIR}/${m3u}"
if test "$remove" = "yes"
then
# We could've used sed to do an "in place" replace. But that would
# involve ugly escaping of $m3u to avoid regex interpretation.
if grep -q -F "$audio" "$m3u" 2>/dev/null
then
{ # This should be OK since grep -F will (almost?) never fail.
rm -f "$m3u"
grep -v -F "$audio" >"$m3u"
} <"$m3u"
printf >&2 "song removed from '%s'\n" "$m3u"
else
printf >&2 "song not in '%s'\n" "$m3u"
fi
else
if grep -q -F "$audio" "$m3u" 2>/dev/null
then
printf >&2 "song already in '%s'\n" "$m3u"
else
printf "%s\n" "$audio" >>"$m3u"
printf >&2 "song added to '%s'\n" "$m3u"
fi
fi
done
#
# Elementary Bash completion function for plmgr(1).
#
# _plmgr() {
# local cur="${COMP_WORDS[COMP_CWORD]}"
# local prev="${COMP_WORDS[COMP_CWORD - 1]}"
# local opts="-a -r"
#
# COMPREPLY=()
#
# if [[ "$cur" = -* ]]
# then
# COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
# else
# # Make sure that `-a' isn't present in the arguments.
# for arg in "${COMP_WORDS[@]}"
# do
# [[ "$arg" = "-a" ]] && return 0
# done
#
# COMPREPLY=( $(CDPATH= cd "$PLMGR_PLAYLIST_DIR"
# compgen -A file -X "!*.m3u" -- "$cur") )
# fi
#
# return 0
# }
# complete -F _plmgr plmgr
#