-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.pl
executable file
·265 lines (190 loc) · 4.74 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/perl
use warnings;
use strict;
#========================================================================#
=pod
=head1 NAME
install.pl - Install links from this directory to ~/.emacs.d/
=head1 OPTIONS
B<install.pl> [-h|--help] [-p|--pretend] [-v|--verbose] [--dir=<DIR>]
=head1 SYNOPSIS
Create links from the current directory to the ~/.emacs.d/ directory
to install the emacs configuration.
Will refuse to run if we're already in the .emacs.d directory.
With I<--pretend> don't actually create the links but will print any
warnings that would have been printed.
With I<--verbose> print more information about what is being done.
The destination directory can be changed from ~/.emacs.d/ using the
I<--dir> option.
=cut
#========================================================================#
use Cwd 'abs_path';
use File::Basename;
use Getopt::Long;
use Pod::Usage;
my $script_dir = dirname (abs_path ($0));
my $emacs_dir = undef;
my $verbose = 0;
my $pretend = 0;
my $help = 0;
GetOptions ("verbose|v" => \$verbose,
"pretend|p" => \$pretend,
"help|h" => \$help,
"dir|d=s" => \$emacs_dir);
if ($help)
{
pod2usage ({-exitval => 0});
exit(1);
}
if (not defined $emacs_dir)
{
$emacs_dir = $ENV{HOME} . "/.emacs.d";
}
if (abs_path ($script_dir) eq abs_path ($emacs_dir))
{
pod2usage ({-exitval => 1});
}
if (not (-d $emacs_dir))
{
if ($verbose)
{
print "Creating '$emacs_dir'\n";
}
if (not $pretend)
{
mkdir $emacs_dir or
die "Failed to create directory '$emacs_dir': $!";
}
}
if ($verbose)
{
print "Installing into '$emacs_dir'\n";
}
my @dont_install = load_dont_install_list ($script_dir);
install ($emacs_dir, $script_dir, @dont_install);
#========================================================================#
=pod
=head1 METHODS
The following methods are defined in this script.
=over 4
=cut
#========================================================================#
=pod
=item B<install>
Currently undocumented.
=cut
sub install {
my $dest = shift;
my $src = shift;
my @dont_install = @_;
opendir my $dh, $src or
die "Failed to open directory '$src': $!";
my @contents = readdir $dh;
closedir $dh or
die "Failed to close directory '$src': $!";
foreach my $file (@contents)
{
next if ($file =~ m/^\./);
next if ($file =~ m/~$/);
next if (is_file_in_dont_install_list ($file, @dont_install));
my $old = $src . "/" . $file;
my $new = $dest . "/" . $file;
if (-l $new)
{
my $link = readlink $new or
die "Failed to readlink '$new': $!";
if (abs_path ($link) eq abs_path ($old))
{
if ($verbose)
{
print "File '$file' already installed.\n";
}
}
else
{
warn "File '$new' already exists, can't install '$file'\n";
}
}
elsif (-d $old and -d $new)
{
# The new directory already exists, recursively install into it.
install ($new, $old, @dont_install);
}
else
{
if ($verbose)
{
print "Creating link '$old' -> '$new'\n";
}
if (not $pretend)
{
symlink $old, $new or
die "Failed to create symlink '$old' -> '$new': $!";
}
}
}
}
#========================================================================#
=pod
=item B<is_file_in_dont_install_list>
Currently undocumented.
=cut
sub is_file_in_dont_install_list {
my $file = shift;
my @list = @_;
foreach my $re (@list)
{
if ($file =~ m/^$re$/)
{
return 1;
}
}
return 0;
}
#========================================================================#
=pod
=item B<load_dont_install_list>
Currently undocumented.
=cut
sub load_dont_install_list {
my $dir = shift;
my $dont_install_file = $dir . "/no-install";
my @no_install = ();
if (-f $dont_install_file)
{
if ($verbose)
{
print "Loading don't install file from: $dont_install_file\n";
}
open my $fh, $dont_install_file or
die "Failed to open '$dont_install_file': $!";
while (<$fh>)
{
chomp;
if ($verbose)
{
print " Regexp: ^$_\$\n";
}
push @no_install, $_;
}
if ($verbose)
{
print "All loaded.\n\n";
}
close $fh or
die "Failed to close '$dont_install_file': $!";
}
elsif ($verbose)
{
print "No don't install file at: $dont_install_file\n";
print "...that's ok though, installing everything...\n\n";
}
return @no_install;
}
#========================================================================#
#========================================================================#
=pod
=back 4
=head1 AUTHOR
Andrew Burgess, 14 Jun 2011
=cut