-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup
executable file
·107 lines (85 loc) · 1.93 KB
/
setup
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env perl
# This is for setting up dotfiles from a cloned repo. The assumption
# is that ~/dotfiles is the repo and this is a sane linux install.
# This script will iterate the ~/dotfiles contents and, for each file
# it finds, it'll replace ~/$file with a symlink to ~/dotfiles/$file.
# Directories are handled similarly.
#
# Originals are preserved as $file.orig
use strict;
use warnings;
my $home = $ENV{HOME};
my $repo = 'dotfiles';
my $verbose = $ENV{VERBOSE};
my %skip_dir = (
'.git' => 1,
);
chdir "$home" or die "$!";
if (not -d "$repo") {
die "please clone the repo and stash it in $home/$repo";
}
chdir "$repo" or die "$!";
opendir D, '.';
my @items = grep { /^\.[^.]/ } readdir D;
closedir D;
for my $item (@items) {
print "$item\n" if $verbose;
}
my @dirs = grep { -d $_ } @items;
my @files = grep { -f $_ } @items;
if ($verbose) {
print "dirs: ", join ', ', @dirs;
print "\n";
print "files: ", join ', ', @files;
print "\n";
}
for my $file (@files) {
handle_file($file);
}
for my $dir (@dirs) {
handle_dir($dir);
}
# hack for ssh
handle_file('.ssh/authorized_keys');
exit;
sub handle_file {
my ($file) = @_;
chdir $home or die "$!";
## if it's already the right link, skip it
if (-l "$file") {
my $target = readlink "$file";
if ($target eq "$repo/$file") {
warn "$file already linked to $target. skipping.\n";
return;
}
}
## move file if it exists
if (-e "$file") {
if (not -e "$file.orig") {
if (rename "$file", "$file.orig") {
print "renamed $file -> $file.orig\n";
}
else {
warn "$file.orig exists. skipping $file.\n";
return;
}
}
}
## make the link
if (symlink "$repo/$file", "$file") {
print "symlink $file -> $repo/$file\n";
}
else {
warn "symlink failed!\n";
if (-e "$file.orig") {
if (rename "$file.orig", "$file") {
print "rolled back $file.orig -> $file\n";
}
}
}
}
## TODO: finish this later
sub handle_dir {
my ($dir) = @_;
}
__END__