-
Notifications
You must be signed in to change notification settings - Fork 0
/
links.pl
60 lines (60 loc) · 1.61 KB
/
links.pl
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
#! /usr/bin/perl
use strict;
use warnings;
# yes, this is disgustingly quick and dirty
chomp(my $dir = `pwd`);
if (substr($dir, 0, length($ENV{HOME}) + 1) eq "$ENV{HOME}/") {
substr($dir, 0, length($ENV{HOME}) + 1, '');
}
$dir .= '/';
print "repo = $dir\nhome = $ENV{HOME}\n";
my ($d, $f);
print "scan for additions\n";
opendir $d, '.' or die "where am i?";
FILE:
while (defined ($f = readdir $d)) {
for (qw(. .. .git .gitignore links.pl LICENSE README.md)) {
next FILE if $_ eq $f;
}
next FILE if $f =~ /^\.#/ || $f =~ /(~|\.swp)$/; # editor temp files
if (! -e "$ENV{HOME}/$f") {
if (@ARGV) {
warn "symlink $f\n";
symlink "$dir/$f", "$ENV{HOME}/$f" or die "symlink: $!";
} else {
print "would symlink $f\n";
}
}
}
closedir $d;
my $l;
print "scan for removals\n";
opendir $d, $ENV{HOME} or die "where are you?";
OLD:
while (defined ($f = readdir $d)) {
next OLD if $f eq '.' or $f eq '..';
if (defined ($l = readlink("$ENV{HOME}/$f")) &&
substr($l, 0, length($dir)) eq $dir) {
next OLD if -e $f;
if (@ARGV) {
warn "remove $f symlink\n";
unlink "$ENV{HOME}/$f" or die "unlink: $!";
} else {
print "would unlink $f\n";
}
}
elsif (-e $f) {
if (@ARGV == 1 && $ARGV[0] eq 'FORCE') {
(my $bf = $f) =~ s/^\.//;
$bf .= ".orig";
if (-e "$ENV{HOME}/$bf") {
die "can't back up $f to $bf";
}
warn "managing $f; backup at $ENV{HOME}/$bf\n";
rename "$ENV{HOME}/$f", "$ENV{HOME}/$bf" or die "rename: $!";
symlink "$dir$f", "$ENV{HOME}/$f" or die "symlink: $!";
} else {
print "$f is unmanaged\n";
}
}
}