fix for paths with a backflash in them
[soepkiptng.git] / soepkiptng_delete
blob767c8a9562c2abcc011439f82d85a8d62d5c4a2b
1 #!/usr/bin/perl
2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 # $Id$
5 ############################################################################
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License, version 2, as
8 # published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # A copy of the GNU General Public License is available on the World Wide Web
16 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
17 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 ############################################################################
21 use Cwd 'abs_path';
22 use DBI;
23 use Getopt::Std;
25 # find program directory
26 $_ = $0;
27 while(-l) {
28 my $l = readlink or die "readlink $_: $!\n";
29 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
31 m|(.*)/|;
32 my $progdir = abs_path($1);
34 require "$progdir/soepkiptng.lib";
36 getopts('fqhxc:');
38 read_configfile(\%conf, $opt_c);
40 $ENV{PATH} = "$progdir/bin:$ENV{PATH}";
42 if($opt_h) {
43 print <<EOF;
44 Usage: soepkiptng_delete [-fqc] [dir]
46 Soepkiptng_delete deletes files from the SoepkipTNG database that do not exist
47 anymore.
49 -f : delete entries even if they still exist
50 -q : quiet
51 -x : permanently delete entries that are not present
53 EOF
54 exit;
57 $dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
58 $conf{db_user}, $conf{db_pass}) or die "can't connect to database";
60 $sth = $dbh->prepare("SELECT filename FROM song WHERE present AND filename like ?");
61 $filename = ($ARGV[0]? abs_path($ARGV[0]) : "/") . "/%";
62 $filename =~ s/\\/\\\\/g;
63 $sth->execute($filename);
65 while(($filename) = $sth->fetchrow_array) {
66 if(!$opt_f) { -s $filename and next; }
67 warn "Deleting $filename from database.\n" unless $opt_q;
68 if($opt_x) {
69 $dbh->do("DELETE FROM song WHERE filename=?", undef, $filename);
70 } else {
71 $dbh->do("UPDATE song SET present=0 WHERE filename=?", undef, $filename);
76 $dbh->disconnect();