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 ############################################################################
22 use Cwd qw
'abs_path cwd';
24 # find program directory
27 my $l = readlink or die "readlink $_: $!\n";
28 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
31 $progdir = abs_path
($1);
33 unshift @INC, "$progdir/lib";
35 require "$progdir/soepkiptng.lib";
36 $ENV{PATH
} = "$progdir/bin:$ENV{PATH}";
43 our ($opt_c, $opt_h, $opt_n);
47 if($opt_h || @ARGV < 2) { die <<EOF; }
48 usage: soepkiptng_mv [-n] [-c configfile] <fromdir> <todir>
50 With this command you can rename a directory, and all entries in the
51 database will have their paths renamed accordingly.
54 -c configfile : override soepkiptng config file
55 -n : test mode, don't change anything
59 read_configfile
(\
%conf, $opt_c);
61 my $dbh = connect_to_db
(\
%conf);
63 my ($from, $to) = @ARGV;
66 die "$to exists, quitting\n";
69 my $absfrom = abs_path
($from);
70 my $absto = abs_path
($to);
73 warn "would rename $absfrom to\n $absto\n";
76 or die "rename $from -> $to: $!\n";
79 my $f = $dbh->selectall_hashref("SELECT id,filename FROM song WHERE filename LIKE ? AND present",
80 "id", undef, "$absfrom/%");
81 foreach my $id (sort { $f->{$a}->{filename
} cmp $f->{$a}->{filename
} } keys %{$f}) {
82 my $name = $f->{$id}->{filename
};
83 if(substr($name, 0, length("$absfrom/")) ne "$absfrom/") {
84 die "filename\n$name does not match\n$absfrom/% ???\n";
86 substr($name, 0, length("$absfrom")) = $absto;
88 warn "db: $f->{$id}->{filename}\n -> $name\n";
90 $dbh->do("UPDATE song SET filename=? WHERE id=?", undef, $name, $id)
91 or die "can't do sql command: " . $dbh->errstr . "\n";