#!/usr/bin/perl -w

use strict;
use File::Find;

my $mount = "/home/etilem/pipod";

mount_Ipod($mount) or die;

my @files;

find_audio($mount, \@files);

play_randomly(\@files);

### funcs

sub play_randomly {

    my $f_ref = shift or die;

    qx!which mplayer! or die "can't find mplayer on your machine";

    system("mplayer $f_ref->[rand(@$f_ref)]") while(1);

}

sub find_audio {

    my $mt = shift or die;
    my $f_ref = shift or die;

    find (  sub { push @$f_ref, $File::Find::name if (/m4a$/); }, 
            $mt
    );

    return $f_ref;

}

sub mount_Ipod {

    my $m = shift or die;

    for (qx!cat /etc/mtab!) {
	    chomp;
	    next unless s#^(/dev/sd\w2).*#$1#;
        system("sudo umount $_");
    }

    for (qx!cat /proc/partitions!) {
	chomp;
	return !system("sudo mount /dev/$_ $m") if s#.*(sd\w2)$#$1#;
    }

}

=head1 NAME

B<pipod.pl>

=head1 DESCRIPTION

Mount and randomly play Ipod m4a files.

Beware: this script assumes that you have only 1 scsi device (sda or sdb or sdc...).

=head1 COPYRIGHT

Copyright 2000-2007 Etienne LEMEE <coding AT etilem DOT net>

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

=cut
