#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
use Mac::iTunes;
use Getopt::Std;
use Mac::Errors '$MacError';
use Mac::Glue  ':glue' ;



our $VERSION=0.1;
$Getopt::Std::STANDARD_HELP_VERSION="true";
my %opts;
getopts('n:', \%opts) ;
HELP_MESSAGE()  && die  if !$opts{n};


my $playlist_name = $opts{n};
my $glue = Mac::Glue->new('iTunes');
my $library =$glue->obj(library_playlist => 1) ;
my @tracks;
for my $track ( $glue->obj(tracks => playlist =>  $playlist_name)->get )
{
  
  my $loc = $track->prop("location")->get ;
  my $dbid = $track->prop("Database ID")->get;
  warn "Bad track. No DB ID."  and next    if  !$dbid;
  for my $t ( $library->obj(tracks => whose("Database ID" => equals => $dbid)))
  {
    $t->delete() ;
    die $MacError if ($! && $MacError);
  } 
  if (-f $loc ){
    unlink($loc) or warn $!;
  }
} 

exit;

## GetOpts::Std uses this

sub HELP_MESSAGE{
  print qq{
$0  deletes all the songs in a playlist from your both your library and disk. Does not delete the playlist itself.  I use it on a cron job to remove tracks I never want to listen to again. I put them in a playlist, the next morning they disappear.
Usage:
$0 -n *required* playlist name

};
}

## GetOpts::Std uses this

sub VERSION_MESSAGE{
  print qq{
$0 $VERSION
2004  Nathan McFarland
Licenced under the Artistic Licence
};
}


__END__

=head1 NAME

delete_tracks - Deletes Tracks from Playlist.

=head1 SYNOPSIS

delete_tracks -n "To Delete"

=head1 DESCRIPTION

Deletes all the songs in a playlist from your both your library and disk. Does not delete the playlist itself.  I use it on a cron job to remove tracks I never want to listen to again. I put them in a playlist, the next morning they disappear.

=head1 AUTHOR

Nathan McFarland,  E<lt>nathan.nmcfarland@nmcfarl.orgE<gt>, http://nmcfarl.org

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

=cut
