#!/usr/bin/perl 

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell
#
# This file is part of Audio::MPD
# Copyright (c) 2007-2009 Jerome Quelin, all rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
#

use strict;
use warnings;

use Audio::MPD;
use DB_File;
use Encode;
use Getopt::Euclid qw[ :minimal_keys ];
use Proc::Daemon;
use Time::HiRes    qw[ usleep ];


#
my $song     = 0; # song currently playing
my $playlist = 0; # playlist version
my $mpd = Audio::MPD->new;

Proc::Daemon::Init unless $ARGV{debug};

# fetch list of songs known by mpd.
my @files = $mpd->collection->all_pathes;


while (1) { # endless loop
    my $status;
    eval { $status = $mpd->status };
    next if $@; # error while peaking status

    # do playlist and/or current song have changed?
    next unless $status->playlist > $playlist
        || defined $status->song && $status->song != $song;
    debug("checking playlist...\n");

    # yup - update playlist & song.
    $playlist = $status->playlist;
    $song     = $status->song;

    # keep at most $ARGV{old} songs.
    if ( $song > $ARGV{old} ) {
        my $old = $song - $ARGV{old};
        debug( "need to remove $old songs\n" );
        eval { $mpd->playlist->delete(0) for 1..$old };
    }

    # add at most $ARGV{new} songs.
    my @pl = $mpd->playlist->as_items;
    if ( $#pl - $song < $ARGV{new} ) {
        my $new = $ARGV{new} - ( $#pl - $song );
        debug("need to add $new songs\n");

        my %ratings;
        my $istied =
            tie( %ratings, 'DB_File', $ARGV{ratings}, O_RDONLY )
            ? 1 : 0;

        PICK_ONE:
        for (1..$new) {
            my $random = encode('utf-8', $files[ rand @files ]);
            if ( $istied && exists $ratings{$random}
                 && $ratings{$random} != 0
                 && $ratings{$random} < $ARGV{min} ) {
                debug("rating too low: $ratings{$random} [$random]\n");
                redo PICK_ONE;
            }
            debug("adding [$random]\n");
            eval { $mpd->playlist->add( decode('utf-8', $random) ) };
            debug("error: $@\n") if $@;
        }
        untie %ratings if $istied;
    }

} continue {
    usleep $ARGV{sleep} * 1000 * 1000; # microseconds
}

exit; # should not be there...

sub debug {
    return unless $ARGV{debug};
    my ($msg) = @_;
    my ($s,$m,$h) = ( localtime(time) )[0,1,2,3,6];
    my $date = sprintf "%02d:%02d:%02d", $h, $m, $s;
    warn "$date $msg";
}

__END__


=head1 NAME

mpd-dynamic - a dynamic playlist for mpd


=head1 USAGE

    mpd-dynamic [options]


=head1 VERSION

This is mpd-dynamic version 0.4


=head1 DESCRIPTION

This program implements a dynamic playlist for MPD, build on top of the
C<Audio::MPD> perl module.

MPD (music player daemon) is a cool music player, but it lacks a dynamic
playlist. A dynamic playlist is a playlist that will change
automatically over time. In particular, it will remove already played
songs (keeping at most a given number of songs) and add new songs to the
playlist so it never fall short of songs.

Note that since mpd is a daemon needing no gui to work, C<mpd-dynamic> is
also a daemon. That is, it will fork and do all its work from the background.
This way, you can fire C<mpd> and C<mpd-dynamic> and forget completely
about your music (especially since C<mpd-dynamic> is a low-resource program):
it will just be there! :-)


=head1 OPTIONS

=head2 General behaviour

You can customize the usage of mpd-dynamic with the following options:


=over 4

=item -o[ld] <old>

Number of old tracks to keep in the backlog. Defaults to 10.

=for Euclid:
    old.type:     integer >= 0
    old.default:  10


=item -n[ew] <new>

Number of new tracks to keep in the to-be-played playlist. Defaults to 10.

=for Euclid:
    new.type:     integer > 0
    new.default:  10


=item -s[leep] <sleep>

Time spent sleeping (in seconds) before checking if playlist should be
updated. Default to 5 seconds.

=for Euclid:
    sleep.type:     number > 0
    sleep.default:  5


=item -d[ebug]

Run mpd-dynamic in debug mode. In particular, the program will not daemonize
itself. Default to false.


=item -e[ncoding] <encoding>

Print debug messages with this encoding. Since mpd-dynamic is meant to be a
silent daemon, this option will not be used outside of debug mode. Default
to C<utf-8>.

=for Euclid:
    encoding.type:     string
    encoding.default:  'utf-8'


=item --version

=item --usage

=item --help

=item --man

Print the usual program information


=back

Note however that those flags are optional: since C<mpd-dynamic> comes with
some sane defaults, you can fire C<mpd-dynamic> as is.


=head2 Ratings

You can also take advantage of ratings if you want. With those options, songs
need to have at least a given rating (or no rating yet) to be inserted: this
way, you will only listen to your favorite songs!

Note that if you supply a non-existant rating db-file, the rating mechanism
will be ignored. The following options control the rating mechanism:


=over 4

=item -r[atings] <ratings>

The path of a db file with the ratings per song. The keys are the song path
(relative to MPD root), and the value is an integer (the rating). Default to
C<~/.mpd/ratings.db>.

=for Euclid:
    ratings.type:     readable
    ratings.default:  "$ENV{HOME}/.mpd/ratings.db"


=item -m[in[imum]] <min>

The minimum rating for a song to be inserted in the playlist. Default to 4.

=for Euclid:
    min.type:     integer > 0
    min.default:  4


=back


=head1 AUTHOR

Jerome Quelin, C<< <jquelin@cpan.org> >>


=head1 COPYRIGHT & LICENSE

Copyright (c) 2007-2009 Jerome Quelin, all rights reserved.

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
