#!/usr/local/bin/perl -w
# $Id: update-music-symlinks,v 1.5 2003/12/12 06:30:10 ianb Exp $
# ianb@nessie.mcc.ac.uk 20030531
# v2 20030926 ditch tracks, and just use cp -s
# v3 20031106 use config file, make more general, part of mp3-archive-tools

use File::Find;
use Cwd;
use strict;

my $me=($0=~/(?:.*\/)?(.*)/)[0];
my $verbose=0;
my %dirs=parsecfg();

while($_=shift)
{
	if(/^-v/)    { $verbose=1; }
	elsif(/^-q/) { $verbose=0; }
	elsif(/^-h/) { usage();    }
	else { usage(); }
}

foreach my $dir (keys %dirs)
{
	updatedir($dir,@{$dirs{$dir}});
}

# yuck. spose I could use closures.
use vars qw($curto);
$curto=undef;

sub updatedir
{
	my($to,@from)=@_;

	print "Updating $to...\n" if $verbose;
	$curto=$to;
	print " checking for files\n" if $verbose;
	-e $to and &File::Find::find(\&checknofiles,$to);
	print " deleting old directory\n" if $verbose;
	system("rm -rf  $to") and die("$me: cannot remove links dir $to: $@\n");
	mkdir $to or die "$me: cannot mkdir $to: $!\n";
	my @globs=map { '"'.$_.'"'."/*"; } @from;
	print " creating links\n" if $verbose;
	my $cmd="cp -r -s ".join(" ",@globs)." $to";
	system($cmd) and die("error running cp: $@\n");
}

sub checknofiles
{
	unless(defined($curto)) { $curto=''; }
	if((!-l) && -f)
	{
		die("$me: File $_ in links directory $curto - aborting\n");
	}
}


sub parsecfg
{
	unless(exists($ENV{HOME}))
	{
		die("$me: \$HOME does not exist - aborting\n");
	}
	unless(open(RC,"$ENV{HOME}/.music-symlinks"))
	{
		die("$me: cannot open .music-symlinks\n".
			"See update-music-symlinks(1) for details on configuring this file\n");
	}
	my %hash=();
	my $dir=undef;
	my $line=0;
	while(<RC>)
	{
		$line++;
		chomp;
		next if(/^\s*$/);
		next if(/^\s*\#/);
		if(/^Dir:\s+(.*)/i)
		{
			$dir=$1;
		}
		else
		{
			unless(defined($dir))
			{
				die("$me: parse error in .music-symlinks: expecting \"Dir: \" at line $line\n");
			}
			s/^\s+//;
			s/\s+$//;
			push(@{$hash{$dir}},$_);
		}
	}
	close RC;
	return %hash;
}

sub usage
{
	die("Usage: $me [-v] [-q] [-h]\n".
		" -v\tVerbose.\n".
		" -q\tQuiet (default).\n".
		" -h\tThis help.\n");
}

__DATA__

=head1 NAME

update-music-symlinks - maintain a directory of symlinks to music

=head1 SYNOPSIS

B<update-music-symlinks> [I<-v>] [I<-q>] [I<-h>]

=head1 DESCRIPTION

update-music-symlinks creates and maintains a link farm pointing to
all your music in various places. If you have music spread over
multiple disks/partitions, or distributed over various directories,
this enables you to browse it all in a single tree.

A config file F<.music-symlinks> is used to specify where to find
music and where to link it. See below for the file format.

B<WARNING:> Directories that the link farm is created in are B<deleted>
first. Just in case, the directories are scanned and the program is
aborted if anything is found in there other than files and symlinks.

=head1 OPTIONS

=over 4

=item B<-v>

Verbose

=item B<-q>

Quiet (no output). This is the default

=item B<-h>

Show a brief usage message

=back

=head1 CONFIGURATION

The configuration for update-music-symlinks is stored in F<$HOME/.music-symlinks>.

The format is simple. Lines starting with B<#> are comments. Blank
lines are ignored.

To specify a directory to create links in, start a line with "Dir: "
(the space is important) then the name, eg:

 Dir: /home/ianb/music/albums

All non-comment lines up to the following "B<Dir: >" line are treated as directories
to create links to within that directory.

=head1 EXAMPLE

A complete example F<.music-symlinks> follows:

 # beware, /home/ianb/music/albums will be deleted!	
 Dir: /home/ianb/music/albums
 /home/ianb/music/essential
 /home/ianb/music/cache
 /home/ianb/music/cut/albums
 /mnt/bigdisk/music/albums

 # beware, /home/ianb/music/tracks will also be deleted.
 Dir: /home/ianb/music/tracks
 /mnt/bigdisk/tracks
 /home/ianb/music/cut/tracks

=head1 ENVIRONMENT

=over 4

=item B<$HOME>

Used to find F<.music-symlinks>

=back

=head1 BUGS

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

=head1 SEE ALSO    

L<mp3-archive-tools(1)>, L<mp3lint(1)>

=head1 AUTHOR

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

=head1 AVAILABILITY

update-music-symlinks 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


	
