1#!/usr/bin/perl -w 2# $Id: update-music-symlinks,v 1.5 2003/12/12 06:30:10 ianb Exp $ 3# ianb@nessie.mcc.ac.uk 20030531 4# v2 20030926 ditch tracks, and just use cp -s 5# v3 20031106 use config file, make more general, part of mp3-archive-tools 6 7use File::Find; 8use Cwd; 9use strict; 10 11my $me=($0=~/(?:.*\/)?(.*)/)[0]; 12my $verbose=0; 13my %dirs=parsecfg(); 14 15while($_=shift) 16{ 17 if(/^-v/) { $verbose=1; } 18 elsif(/^-q/) { $verbose=0; } 19 elsif(/^-h/) { usage(); } 20 else { usage(); } 21} 22 23foreach my $dir (keys %dirs) 24{ 25 updatedir($dir,@{$dirs{$dir}}); 26} 27 28# yuck. spose I could use closures. 29use vars qw($curto); 30$curto=undef; 31 32sub updatedir 33{ 34 my($to,@from)=@_; 35 36 print "Updating $to...\n" if $verbose; 37 $curto=$to; 38 print " checking for files\n" if $verbose; 39 -e $to and &File::Find::find(\&checknofiles,$to); 40 print " deleting old directory\n" if $verbose; 41 system("rm -rf $to") and die("$me: cannot remove links dir $to: $@\n"); 42 mkdir $to or die "$me: cannot mkdir $to: $!\n"; 43 my @globs=map { '"'.$_.'"'."/*"; } @from; 44 print " creating links\n" if $verbose; 45 my $cmd="cp -r -s ".join(" ",@globs)." $to"; 46 system($cmd) and die("error running cp: $@\n"); 47} 48 49sub checknofiles 50{ 51 unless(defined($curto)) { $curto=''; } 52 if((!-l) && -f) 53 { 54 die("$me: File $_ in links directory $curto - aborting\n"); 55 } 56} 57 58 59sub parsecfg 60{ 61 unless(exists($ENV{HOME})) 62 { 63 die("$me: \$HOME does not exist - aborting\n"); 64 } 65 unless(open(RC,"$ENV{HOME}/.music-symlinks")) 66 { 67 die("$me: cannot open .music-symlinks\n". 68 "See update-music-symlinks(1) for details on configuring this file\n"); 69 } 70 my %hash=(); 71 my $dir=undef; 72 my $line=0; 73 while(<RC>) 74 { 75 $line++; 76 chomp; 77 next if(/^\s*$/); 78 next if(/^\s*\#/); 79 if(/^Dir:\s+(.*)/i) 80 { 81 $dir=$1; 82 } 83 else 84 { 85 unless(defined($dir)) 86 { 87 die("$me: parse error in .music-symlinks: expecting \"Dir: \" at line $line\n"); 88 } 89 s/^\s+//; 90 s/\s+$//; 91 push(@{$hash{$dir}},$_); 92 } 93 } 94 close RC; 95 return %hash; 96} 97 98sub usage 99{ 100 die("Usage: $me [-v] [-q] [-h]\n". 101 " -v\tVerbose.\n". 102 " -q\tQuiet (default).\n". 103 " -h\tThis help.\n"); 104} 105 106__DATA__ 107 108=head1 NAME 109 110update-music-symlinks - maintain a directory of symlinks to music 111 112=head1 SYNOPSIS 113 114B<update-music-symlinks> [I<-v>] [I<-q>] [I<-h>] 115 116=head1 DESCRIPTION 117 118update-music-symlinks creates and maintains a link farm pointing to 119all your music in various places. If you have music spread over 120multiple disks/partitions, or distributed over various directories, 121this enables you to browse it all in a single tree. 122 123A config file F<.music-symlinks> is used to specify where to find 124music and where to link it. See below for the file format. 125 126B<WARNING:> Directories that the link farm is created in are B<deleted> 127first. Just in case, the directories are scanned and the program is 128aborted if anything is found in there other than files and symlinks. 129 130=head1 OPTIONS 131 132=over 4 133 134=item B<-v> 135 136Verbose 137 138=item B<-q> 139 140Quiet (no output). This is the default 141 142=item B<-h> 143 144Show a brief usage message 145 146=back 147 148=head1 CONFIGURATION 149 150The configuration for update-music-symlinks is stored in F<$HOME/.music-symlinks>. 151 152The format is simple. Lines starting with B<#> are comments. Blank 153lines are ignored. 154 155To specify a directory to create links in, start a line with "Dir: " 156(the space is important) then the name, eg: 157 158 Dir: /home/ianb/music/albums 159 160All non-comment lines up to the following "B<Dir: >" line are treated as directories 161to create links to within that directory. 162 163=head1 EXAMPLE 164 165A complete example F<.music-symlinks> follows: 166 167 # beware, /home/ianb/music/albums will be deleted! 168 Dir: /home/ianb/music/albums 169 /home/ianb/music/essential 170 /home/ianb/music/cache 171 /home/ianb/music/cut/albums 172 /mnt/bigdisk/music/albums 173 174 # beware, /home/ianb/music/tracks will also be deleted. 175 Dir: /home/ianb/music/tracks 176 /mnt/bigdisk/tracks 177 /home/ianb/music/cut/tracks 178 179=head1 ENVIRONMENT 180 181=over 4 182 183=item B<$HOME> 184 185Used to find F<.music-symlinks> 186 187=back 188 189=head1 BUGS 190 191None known. Please report any found to ianb@nessie.mcc.ac.uk 192 193=head1 SEE ALSO 194 195L<mp3-archive-tools(1)>, L<mp3lint(1)> 196 197=head1 AUTHOR 198 199Ian Beckwith <ianb@nessie.mcc.ac.uk> 200 201=head1 AVAILABILITY 202 203update-music-symlinks is part of the mp3-archive-tools package. 204 205The latest version can be found at: 206 207B<http://nessie.mcc.ac.uk/~ianb/projects/mp3-archive-tools/> 208 209=head1 COPYRIGHT 210 211Copyright 2003 Ian Beckwith <ianb@nessie.mcc.ac.uk> 212 213This program is free software; you can redistribute it and/or modify 214it under the terms of the GNU General Public License as published by 215the Free Software Foundation; either version 2 of the License, or (at 216your option) any later version. 217 218This program is distributed in the hope that it will be useful, but 219WITHOUT ANY WARRANTY; without even the implied warranty of 220MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 221General Public License for more details. 222 223You should have received a copy of the GNU General Public License 224along with this program; if not, write to the Free Software 225Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 226 227=cut 228 229 230 231