Repository navigation
Expand file tree
/
Copy pathbanish
More file actions
executable file
·94 lines (85 loc) · 1.65 KB
/
Copy pathbanish
File metadata and controls
executable file
·94 lines (85 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl -w
# Banish something from one directory to a special mirrored directory structure.
use Cwd;
use File::Copy;
our $originaldir = "/media/music";
our $subdir = "/media/music-norand";
our $verbose=0;
main();
sub main
{
my ($files, $pretend) = handle_args();
my @sfiles = map{sanitize_file($_);} @$files;
my %plan = build_plan(@sfiles);
if($pretend) {exit;}
foreach my $file (keys %plan)
{
ensure_dirfor($plan{$file});
move($file, $plan{$file});
}
}
sub ensure_dirfor($;)
{
my ($tfile) = @_;
$tfile =~ /^(.*)\/([^\/])+$/;
my $dir = $1;
if(! defined $dir)
{
die "Internal regex error\n";
}
if($verbose)
{print "Making sure directory [$dir] exists\n";}
system("mkdir -p $dir");
}
sub build_plan(@)
{
my @files = @_;
my %plan;
foreach my $file (@files)
{
my $newfn = $file;
$newfn =~ s/$originaldir/$subdir/;
$newfn =~ s/fromdad//;
$newfn =~ s/\/\//\//g;
if(($newfn eq $file) || (! -f $file) )
{
die "File $file doesn't appear to be in the origin dir $originaldir\n";
}
$plan{$file} = $newfn;
if($verbose)
{
print "PLAN: move [$file] => [$newfn]\n";
}
}
return %plan;
}
sub sanitize_file($;)
{ # Turn a file into a full path/file
my ($fn) = @_;
if($fn !~ /^\//)
{
$fn = getcwd() . '/' . $fn;
}
if($verbose) {print "Mapped [" . $_[0] . "] -> [$fn]\n";}
return $fn;
}
sub handle_args
{
my (@files, $pretend);
$pretend=0;
local $_;
if(@ARGV < 1)
{die "usage: banish [-v][-p] files..\n";}
foreach my $arg (@ARGV)
{
if($arg eq '-v')
{$verbose=1;next;}
if($arg eq '-p')
{$pretend=1;next;}
if(($arg !~ /^-/) && ($arg !~ /^\.\.?$/) )
{push(@files, $arg);next;}
else
{die "Didn't recognize arg [$arg]\n";}
}
return (\@files, $pretend);
}