1c479f38ea6315e5a4208e504051b67ef5b7a207
[id3fs.git] / bin / id3fs-index
1 #!/usr/bin/perl -w
2 # Ian Beckwith <ianb@erislabs.net>
3 #
4
5 use lib '/home/ianb/projects/id3fs/id3fs/lib'; # FIXME: remove
6 use strict;
7 use Getopt::Long qw(Configure);
8 use ID3FS::DB;
9 use File::Find;
10 use vars qw($me);
11 $me=($0=~/(?:.*\/)?(.*)/)[0];
12
13 my $verbose=0;
14 my $help=0;
15 my $basedir=undef;
16 my $dbpath=undef;
17 my @extensions=qw(mp3); # ogg flac); # FIXME
18
19 Configure(qw(bundling no_ignore_case));
20 my $optret=GetOptions(
21     "verbose|v"      => \$verbose,
22     "quiet|q"        => sub { $verbose=0; },
23     "help|h"         => \$help,
24     "dir|d=s"        => \$basedir,
25     "database|f=s"   => \$dbpath,
26     "extensions|e=s" => sub { @extensions=split(/\s+|\s*,\s*/, $_[1]); },
27     );
28
29 usage() if(!@ARGV || !$optret || $help);
30
31 if(@ARGV > 1 && !defined($basedir))
32 {
33     die("$me: --basedir must be specified if multiple paths are supplied\n");
34 }
35
36 my $db=ID3FS::DB->new($me, $dbpath, $basedir, $ARGV[0]);
37 $db->last_update(time());
38
39 while(my $path=shift)
40 {
41     File::Find::find( {wanted => \&wanted, follow => 1, no_chdir => 1}, $path);
42 }
43
44 sub wanted
45 {
46     my $ext='';
47     if(/.*\.(.*)/) { $ext=lc($1); }
48     if(-d)
49     {
50         print("$_\n") if $verbose;
51     }
52     elsif(-f && scalar(grep({ $ext eq lc($_);} @extensions)))
53     {
54         s/^\.\///;
55         $db->add($_);
56     }
57 }
58
59 sub usage
60 {
61     die("Usage: $me [-vqh] [-d basedir] [-f dbpath] [-e mp3,ogg,flac] [--] DIR...\n".
62         " -v|--verbose\t\t\tVerbose\n".
63         " -q|--quiet\t\t\tQuiet (default)\n".
64         " -d|--dir=PATH\t\t\tBase directory of source files (default: ARGV[0])\n".
65         " -f|--database=FILE\t\tPath to database file (default: basedir/.id3fs)\n".
66         " -e|--extensions=EXT1,EXT2\tFile extensions to index (default: mp3, ogg, flac)\n".
67         " -h|--help\t\t\tThis help\n".
68         " --\t\t\t\tEnd of options\n");
69 }
70
71 __END__
72
73 =head1 NAME
74
75 id3fs-index - Add files to id3fs index
76
77 =head1 SYNOPSIS
78
79 B<id3fs-index> [B<-vqh>] S<[B<-d >I<basedir>]> S<[B<-f >I<dbpath>]> S<[B<-e >I<mp3,ogg,flac>]> [B<-->] [I<DIR>...]
80
81 =head1 DESCRIPTION
82
83 Extracts id3 tags from mp3 files (and comment tags from ogg and flac
84 files) and adds them to a sqlite database, ready for mounting
85 with L<id3fsd(8)>.
86
87 =head1 OPTIONS
88
89 =over 4
90
91 =item B<-v>
92
93 Enable verbose operation.
94
95 =item B<-q>
96
97 Quiet (no output). This is the default.
98
99 =item S<B<-d >I<PATH>> | S<B<--dir=>I<PATH>>
100
101 Specify base directory of source files. All files will be indexed
102 relative to this point.
103
104 If not specified, defaults to the first non-option argument on the
105 command line. Note that to avoid ambiguities, if more than one
106 directory is specified on the command line, the base directory must
107 be specified explicitly.
108
109 All files indexed must be under the base directory.
110
111 =item S<B<-f >I<FILE>> | S<B<--database=>I<FILE>>
112
113 Database file to use. If not specified, defaults to
114 a hidden file called B<".id3fs"> under the base directory.
115
116 =item S<B<-e >I<EXT1,EXT2>> | S<B<--extensions=>I<EXT1,EXT2>>
117
118 File extensions to consider when indexing.
119 Defaults to B<.mp3>, B<.ogg> and B<.flac>.
120
121 =item B<-h>
122
123 Show a short help message.
124
125 =item B<-->
126
127 End of options.
128
129 =back
130
131 =head1 EXAMPLES
132
133 Index all files in the current directory:
134
135     id3fs-index .
136
137 Index current directory, printing each subdirectory as it recurses
138 into it:
139
140     id3fs-index -v .
141
142 Just index some sub-directories:
143
144     id3fs-index -d . dir1 dir2
145
146 Store the database in a custom location:
147
148     id3fs-index -f ~/.id3fs/index.sqlite .
149
150 Only index .mp3 and .flac files:
151
152     id3fs-index -e mp3,flac .
153
154 =head1 BUGS
155
156 Please report any found to ianb@erislabs.net
157
158 =head1 SEE ALSO
159
160 L<id3fsd(8)>
161
162 =head1 AUTHOR
163
164 Ian Beckwith <ianb@erislabs.net>
165
166 =head1 AVAILABILITY
167
168 The latest version can be found at:
169
170 B<http://erislabs.net/ianb/projects/id3fs/>
171
172 =head1 COPYRIGHT
173
174 Copyright 2010 Ian Beckwith <ianb@erislabs.net>
175
176 This program is free software: you can redistribute it and/or modify
177 it under the terms of the GNU General Public License as published by
178 the Free Software Foundation; either version 3 of the License, or
179 (at your option) any later version.
180
181 This program is distributed in the hope that it will be useful,
182 but WITHOUT ANY WARRANTY; without even the implied warranty of
183 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
184 GNU General Public License for more details.
185
186 You should have received a copy of the GNU General Public License
187 along with this program.  If not, see <http://www.gnu.org/licenses/>.
188
189 =cut