2011-08-08 07:56:33 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
use Getopt::Std qw(getopts);
|
|
|
|
|
|
|
|
my %opts;
|
2011-08-10 06:59:16 +00:00
|
|
|
getopts("dm:", \%opts) or usage();
|
2011-08-08 07:56:33 +00:00
|
|
|
|
|
|
|
my $mode = $opts{m};
|
|
|
|
|
2011-08-10 06:59:16 +00:00
|
|
|
if ($opts{d}) {
|
|
|
|
shell("mkdir -p @ARGV");
|
|
|
|
exit;
|
2011-08-08 07:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (@ARGV < 2) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
my $dst = pop;
|
|
|
|
|
|
|
|
my @src = @ARGV;
|
|
|
|
|
|
|
|
my $target_dir;
|
|
|
|
|
|
|
|
if (@src > 1 || $dst =~ m{/$}) {
|
|
|
|
$target_dir = $dst;
|
|
|
|
|
|
|
|
} elsif (-d $dst) {
|
|
|
|
$target_dir = $dst;
|
|
|
|
|
|
|
|
} elsif ($dst =~ m{(.+)/}) {
|
|
|
|
$target_dir = $1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$target_dir = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!-d $target_dir) {
|
|
|
|
shell("mkdir -p $target_dir");
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:15:48 +00:00
|
|
|
if (-f $dst) {
|
|
|
|
shell("rm $dst");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for my $f (@src) {
|
|
|
|
if (-f $f) {
|
|
|
|
(my $name = $f) =~ s{.*/}{}g;
|
|
|
|
my $target = "$target_dir/$name";
|
|
|
|
if (-f $target) {
|
|
|
|
shell("rm $target");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 07:56:33 +00:00
|
|
|
shell("cp @src $dst");
|
|
|
|
|
|
|
|
if (-f $dst) {
|
2011-08-10 06:59:16 +00:00
|
|
|
if (defined $mode) {
|
|
|
|
chmod oct($mode), $dst or
|
|
|
|
die "failed to change mode of $dst to $mode.\n";
|
|
|
|
}
|
2011-08-08 07:56:33 +00:00
|
|
|
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $src (@src) {
|
|
|
|
my $name;
|
|
|
|
|
|
|
|
if ($src =~ m{/([^/]+)$}) {
|
|
|
|
$name = $1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$name = $src;
|
|
|
|
}
|
|
|
|
|
2011-08-10 06:59:16 +00:00
|
|
|
if (defined $mode) {
|
|
|
|
my $target = "$target_dir/$name";
|
|
|
|
chmod oct($mode), $target or
|
|
|
|
die "failed to change mode of $target to $mode.\n";
|
|
|
|
}
|
2011-08-08 07:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub usage {
|
2011-08-10 06:59:16 +00:00
|
|
|
die "Usage: install [-d] [-m <attrs>] <src>... <dst>\n";
|
2011-08-08 07:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub shell {
|
|
|
|
my $cmd = shift;
|
2014-08-18 05:15:48 +00:00
|
|
|
#warn $cmd;
|
2011-08-08 07:56:33 +00:00
|
|
|
system($cmd) == 0 or
|
|
|
|
die "failed to run command $cmd\n";
|
|
|
|
}
|
|
|
|
|