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