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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
system($cmd) == 0 or
|
|
|
|
die "failed to run command $cmd\n";
|
|
|
|
}
|
|
|
|
|