#!/usr/local/bin/perl -w
# $Id: movemusic,v 1.7 2003/12/12 06:30:10 ianb Exp $
# Ian Beckwith <ianb@nessie.mcc.ac.uk>
# 20031027

use strict;
use MP3::Archive;
use Cwd;
use File::Copy;

use vars qw($me);
$me=($0=~/(?:.*\/)?(.*)/)[0];

my $archive=MP3::Archive->new;

use vars qw($ALBUM $TRACK $GUESS);

$ALBUM=0;
$TRACK=1;
$GUESS=2;
my $tracktype=$GUESS;
my $verbose=0;
my $dryrun=0;
my $doneargs=0;
my $donesomething=0;

FILE: while($_=shift)
{
	if(/^-/ && !$doneargs)
	{
		if   (/-v/) { $verbose=1;        }
		elsif(/-q/) { $verbose=0;        }
		elsif(/-d/) { $dryrun=1;         }
		elsif(/-a/) { $tracktype=$ALBUM; }
		elsif(/-t/) { $tracktype=$TRACK; }
		elsif(/-g/) { $tracktype=$GUESS; }
		elsif(/--/) { $doneargs=1;       }
		elsif(/-h/) { usage();           }
		else { usage(); }
		next FILE;
	}
	$donesomething=1;
	my $file=$_;

	unless(-e $file)
	{
		warn("$me: $file: not found\n");
		next;
	}
	
	my $filename=$archive->filepart($file);
	my $artist=$archive->artist($filename,$tracktype);
	my $album=$archive->album($filename,$tracktype);

	unless(defined($artist))
	{
		warn("$me: $file: cannot extract artist name\n");
		next FILE;
	}
	# disable test, in case its a non-album track
#	unless(defined($album))
#	{
#		warn("$me: $file: cannot extract album title\n");
#		next FILE;
#	}

	my $newpath=$archive->makepathname($file,$artist,$album,$tracktype);
	unless(defined($newpath))
	{
		warn("$me: $file: cannot contruct pathname\n");
		next FILE;
	}
	
	# despite docs File::Copy::move, if dest of destdir/destfilename.mp3,
	# moves to destdir/destfilename.mp3/destfilename.mp3
	# $newpath .= "/$filename";

	$newpath=~s,/+,/,g; # remove duplicate slashes
	my $newfile=$newpath ."/".$filename;
	$newfile=~s,/+,/,g; # remove duplicate slashes

#	print "newpath: $newpath newfile: $newfile\n";
#	print "artist: $artist album: ",defined($album)?$album:"<undef>","\n";
	
	my @parts=split(/\//,$newpath);
	my $mkpath='';
	my $newpart;
	$newpart=shift(@parts);
	while(defined($newpart))
	{
		next if($newpart eq '');
		$mkpath .= "/$newpart";
		unless(mkdir($mkpath))
		{
			unless($! =~ /exists/i)
			{
				warn "$me: cannot mkdir $mkpath: $!\n";
				next FILE;
			}
		}
	}
	continue
	{
		$newpart=shift(@parts);
	}

	if(-e $newfile)
	{
		warn("$me: $newfile: file exists, skipping\n");
		next FILE;
	}

	my $skip=0;
	unless($dryrun)
	{
		unless(move($file,$newpath))
		{
			$skip=1;
			warn("$me:$file: cannot rename to $newfile:$!\n");
			next FILE;
		}
	}

	if((!$skip) && ($verbose || $dryrun))
	{
		print("$file -> $newfile\n");
	}
}

unless($donesomething)
{
	usage();
}

sub usage
{
	die("Usage: $me [-v] [-q] [-d] [-a] [-t] [-g] file.mp3..\n".
		" -v\tVerbose.\n".
		" -q\tQuiet (default).\n".
		" -d\tDry run (show what would be done).\n".
		" -a\tTreat as album tracks.\n".
		" -t\tTreat as non-album tracks.\n".
		" -g\tGuess track type from file location (default).\n".
		" -h\tThis help.\n");
}
		
__END__


=head1 NAME

movemusic - move audio tracks to the correct directories

=head1 SYNOPSIS

B<movemusic> [I<-v>] [I<-q>] [I<-d>] [I<-a>] [I<-t>] [I<-g>] [I<-h>] [I<file.mp3>...]

=head1 DESCRIPTION

movemusic moves the filenames given on the commandline to the correct
directories, as specified in the config file F<.mp3archiverc>. For
details on configuring this file, see L<MP3::Archive::Config(3)>.

=head1 OPTIONS

=over 4

=item B<-v>

Enable verbose operation.

=item B<-q>

Quiet (no output). This is the default.

=item B<-d>

Dry run (don't move anything, just show what would be done)

=item B<-a>

Treat files as album tracks.

=item B<-t>

Treat files as non-album tracks.

=item B<-g>

Guess whether to treat files as album tracks or not, based on the
B<current> directory of the file. This is the default.

=item B<-h>

Show a short help message.

=back

=head1 BUGS

None known. Please report any found to ianb@nessie.mcc.ac.uk	

=head1 SEE ALSO    

L<MP3::Archive(3)>, L<MP3::Archive::Config(3)>,
L<mp3-archive-tools(1)>, L<mp3lint(1)>

=head1 AUTHOR

Ian Beckwith <ianb@nessie.mcc.ac.uk>

=head1 AVAILABILITY

movemusic is part of the mp3-archive-tools package.

The latest version can be found at:

B<http://nessie.mcc.ac.uk/~ianb/projects/mp3-archive-tools/>

=head1 COPYRIGHT

Copyright 2003 Ian Beckwith <ianb@nessie.mcc.ac.uk>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

=cut


