-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.pl
executable file
·129 lines (100 loc) · 2.81 KB
/
install.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env perl
#
# A little script which should run in dist perl and
# will symlink my dotfiles into $HOME.
#
# Possible TODO:
# - more cli parameters?
# - link-source=$HOME
#
use strict;
use warnings;
use 5.10.0;
use Term::ANSIColor;
use File::Basename;
use Cwd 'realpath';
use Env 'HOME';
use Getopt::Std;
our ($opt_d, $opt_r, $opt_h) = (0, 0);
getopts('drh');
$opt_r = !$opt_r;
if ($opt_h) {
print <<HELP;
Usage $0 [-h] [-dr]
Perform a symlink installation of dotfiles to the current users home directory.
-d: Dry run. Don't actually link or rename anything, just talk about it.
-h: Help. Display this help and exit.
-r: noRename cwd. Prevent the automatic renaming of the script current
execution directory.
HELP
exit;
}
sub color_say {
my $temp;
while (@_ > 0) {
# say scalar(@_), 'items in \@_. [0] is ', $_[0];
print color (shift @_) if (@_ > 1);
print shift @_ if (@_);
print color 'reset';
}
print "\n";
}
my $preferred_directory_name = '.dotfiles';
# establish where the script resides and
# where the links are going to be dropped
my $cwd = dirname realpath $0;
my $destination = $HOME;
# rename the working directory into something with a dot prefix
if (basename($cwd) ne $preferred_directory_name) {
my $new_cwd = dirname($cwd) . '/' . $preferred_directory_name;
color_say 'green', '# renaming'," $cwd to $new_cwd";
if ($opt_r && ! $opt_d) {
rename $cwd, $new_cwd;
$cwd = $new_cwd;
}
}
# rename current directory to hide it as a dotfile
rename "$cwd", basename($cwd) . "/.dotfiles";
# everything should be done relative to the script,
# not to the execute location
chdir($cwd);
# read in the excludes file
my $excludes = "";
my $excludes_exist = 1;
open my $exclude_file, "excludes" or $excludes_exist = 0;
if ($excludes_exist) {
$excludes = join(' ', map {chomp; $_} <$exclude_file>);
}
# fetch a list of the dotfiles in this folder
my @destinations = <.*>;
my $flags = '';
for my $file (@destinations) {
# skip . and ..
next if ($file =~ /^\.{1,2}$/);
# dont link pipes and fifos and other non-files
if ( ! (-d $file || -f $file)) {
color_say 'red', 'not a regular file: ', $file;
next;
}
# check to see if the file was explicitly excluded
if (index($excludes, $file) > -1) {
color_say 'cyan', '# skip: ', $file;
next;
}
# check to make sure the file doesn't already exist
if (-e "$destination/$file") {
if (-d "$destination/$file") {
$flags = "-r";
} else {
$flags = '';
}
color_say 'reset', "rm $flags $destination/$file", 'yellow', ' # file exists, remove and re-run';
next;
}
# link it, bro
color_say 'green', '# link: ', "$destination/$file to $cwd/$file";
if (! $opt_d) {
symlink "$cwd/$file", "$destination/$file" or color_say 'red', 'link failed :/';
}
}
print "\n";