-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_play.in
205 lines (189 loc) · 4.17 KB
/
transfer_play.in
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/sh
# $Log: transfer_play.in,v $
# Revision 1.10 2023-01-04 23:41:33+05:30 Cprogrammer
# fixed process_cmdline
#
# Revision 1.9 2022-11-25 21:17:02+05:30 Cprogrammer
# added --clearsrc, --cleardst to clear current playlist on source, destination
#
# Revision 1.8 2022-10-21 13:45:26+05:30 Cprogrammer
# added command line options --play, --no-rm
#
# Revision 1.7 2022-06-20 01:07:37+05:30 Cprogrammer
# use directories set in ./configure
#
# Revision 1.6 2021-02-14 18:43:07+05:30 Cprogrammer
# display list of valid hosts on wrong input
#
# Revision 1.5 2020-09-05 12:32:42+05:30 Cprogrammer
# clear playlist before adding
#
# Revision 1.4 2020-08-30 10:57:30+05:30 Cprogrammer
# use state_file definition in mpd.conf to figure out state_file directory
#
# Revision 1.3 2020-08-11 22:23:23+05:30 Cprogrammer
# use mpc to play and remove playlist
#
# Revision 1.2 2020-07-23 01:47:58+05:30 Cprogrammer
# removed incorrect check for tohost
#
# Revision 1.1 2020-07-22 15:50:47+05:30 Cprogrammer
# Initial revision
#
#
# $Id: transfer_play.in,v 1.10 2023-01-04 23:41:33+05:30 Cprogrammer Exp mbhangui $
#
get_mpd_conf_value()
{
grep "^$1" @sysconfdir@/mpd.conf|awk '{print $2}' | \
sed -e 's{"{{g'
}
get_mpd_state()
{
(
do_print=0
cat $state_dir/mpd_state.$1 | while read line
do
if [ " $line" = " playlist_begin" ] ; then
do_print=1
continue
fi
if [ " $line" = " playlist_end" ] ; then
break
fi
if [ $do_print -eq 1 ] ; then
echo $line
fi
done
) | sed -e 's{^.*:{{'
}
usage()
{
if [ -f /usr/bin/less ] ; then
MORE=/usr/bin/less
else
MORE=/usr/bin/more
fi
$MORE 1>&2 <<EOF
Usage: transfer_play [OPTION] source destination
Known values for OPTION are:
--play
transfer and start playing current playlist on destination
--clearsrc
clear current playlist from source
--cleardst
clear current playlist from destination
EOF
exit $1
}
process_cmdline()
{
while test $# -gt 0; do
case "$1" in
-*=*)
optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'`
optval=`echo "$1" | cut -d'=' -f1`
prog_args="$prog_args $optval=\"$optarg\""
;;
-*\>*)
optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*>//'`
optarg=">$optarg"
optval=`echo "$1" | cut -d'>' -f1`
prog_args="$prog_args $optval\"$optarg\""
;;
-*\<*)
optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*<//'`
optarg="<$optarg"
optval=`echo "$1" | cut -d'<' -f1`
prog_args="$prog_args $optval\"$optarg\""
;;
*)
optarg=
prog_args="$prog_args $1"
;;
esac
case "$1" in
--play)
do_play=1
shift
;;
--cleardst)
cleardst=1
shift
;;
--clearsrc)
clearsrc=1
shift
;;
--help)
usage 0
;;
esac
if [ $# -eq 2 ] ; then
srchost=$1
dsthost=$2
break
elif [ $# -lt 2 ] ; then
usage 0
fi
done
}
set -e
do_play=0
clearsrc=0
cleardst=0
process_cmdline "$@"
if [ -z "$srchost" -o -z "$dsthost" ] ; then
echo "Must specify source and destination hosts"
usage 1
fi
if [ " $dsthost" = " $srchost" ] ; then
echo "destination cannot be same as source" 1>&2
usage 1
fi
playlist_dir=$(get_mpd_conf_value playlist_directory)
if [ $? -ne 0 ] ; then
echo "could not get playlist directory from mpd.conf" 1>&2
exit 1
fi
music_dir=$(get_mpd_conf_value music_dir)
if [ $? -ne 0 ] ; then
echo "could not get music directory from mpd.conf" 1>&2
exit 1
fi
state_file=$(get_mpd_conf_value state_file)
state_dir=$(dirname $state_file)
if [ ! -f $state_dir/mpd_state.$srchost ] ; then
echo "No such host $srchost" 1>&2
echo "List of valid hosts"
ls $state_dir/mpd_state.*|cut -d. -f2
exit 1
fi
out=$(mktemp -t mpdevXXXXXXXXXX)
# get current playlist from $srchost
get_mpd_state $srchost > $out.m3u
if [ -s $out.m3u ] ; then
mv $out.m3u $playlist_dir
# clear current playlist on $dsthost
if [ $cleardst -eq 1 ] ; then
mpc --host="$dsthost" clear
fi
# create current playlist from $outb
outb=$(basename $out)
mpc --host=$dsthost load $outb
# delete playlist $outb.m3u from playlist directory
mpc --host=$dsthost rm $outb
if [ $do_play -eq 1 ] ; then
# start playing current playlist on $dsthost
mpc --host=$dsthost play
fi
# clear current playlist on $srchost
if [ $clearsrc -eq 1 ] ; then
mpc --host="$srchost" clear
fi
/bin/rm -f $out
else
/bin/rm -f $out $out.m3u
echo "empty current playlist on source" 1>&2
exit 1
fi