summaryrefslogtreecommitdiff
path: root/mprev
blob: cb1f9443bb347e51a95c8e43ac07ccb15196c29e (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use Audio::MPD q{0.19.0};

=head1 NAME

mprev - reverse the mpd playlist

=head1 SYNOPSIS

mprev [host]

=head1 DESCRIPTION

B<mprev> reverses mpd's playlist. That's all. The currently playing
song doesn't change. The song you heard last will be the next song to
play.

If the hostname is omitted, the MPD_HOST environment variable will be used.

=head1 AUTHOR

Copyright 2007 Joey Hess <joey@kitenet.net>

Licensed under the GNU GPL version 2 or higher.

http://kitenet.net/~joey/code/mpdtoys

=cut

if (@ARGV) {
	$ENV{MPD_HOST}=shift;
}

my $mpd=Audio::MPD->new(conntype => "reuse");
my $pl=$mpd->playlist;
my $current=$mpd->current;
if (! $current) {
	die "playlist seems to be empty\n";
}
my $pos=$mpd->current->pos;
my $id=$mpd->current->id;
my $past=0;
my @list=$pl->as_items;
foreach my $song (reverse @list) {
	if ($past) {
		$pl->moveid($song->id, $#list);
	}
	elsif ($id eq $song->id) {
		$past=1;
	}
	else {
		$pl->moveid($song->id, $pos++);
	}
}