-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install.pl
executable file
·153 lines (132 loc) · 4.07 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
#!/usr/bin/env perl
use warnings;
use strict;
use Cwd qw(realpath);
use File::Basename;
use File::Compare;
use File::Path qw(make_path);
if (-e $ENV{"HOME"} . '/.zshrc.local') {
print("\x1b[31m");
print("Detected a pre-XDG installation! The following update is potentially destructive and makes no attempt at a migration.\n");
print("\n");
print("More information: https://github.com/mscharley/dotfiles/pull/2\n");
print("\n");
print("To revert your last pull, press Ctrl-C to quit and then run `git reset --hard HEAD\@{1}`.\n");
print("Press any other key to continue...\n");
print("\x1b[0m");
getc();
}
sub get_filename {
my ($file) = (@_, $_);
$file =~ s/\.(?:example)$//;
$file =~ s!%!/!g;
return $file;
}
sub replace_file {
my ($src_file, $target_file) = @_;
system("rm", "-rf", $target_file);
link_file($src_file, $target_file)
}
sub link_file {
my ($src, $target) = @_;
make_path(dirname($target));
my $is_example = $src =~ /\.example$/;
if ($is_example && -d $src) {
my $example_folder = $src;
print("creating folder $target\n");
make_path($target);
my @example_files = glob($src . '/{*,.??*}');
for (@example_files) {
next if m!/.gitkeep$!;
my $example_target = $_ =~ s/^\Q${example_folder}\E/${target}/r;
print("copying $example_target\n");
system('cp', '-v', $_, $example_target);
}
}
elsif ($is_example) {
if (-l $target) {
print("removing old link $target\n");
system('rm', '-f', $target);
}
print("copying $target\n");
system('cp', '-v', $src, $target);
}
else {
print("linking $target\n");
system('ln', '-vs', realpath($src), $target);
}
}
my $replace_all = 0;
sub process_files {
my ($files, $src_prefix, $prefix) = @_;
for (@$files) {
my $is_example = /\.example$/;
my $target_file = get_filename();
my $target = $target_file =~ s/^\Q${src_prefix}\E/${prefix}/r;
if ($is_example && -e $target) {
# Do nothing if an example was already installed.
print("exists $target\n");
}
elsif (!$is_example && -l $target) {
if (readlink($target) eq realpath($_)) {
print("identical $target\n");
}
else {
print("replace link $target_file\n");
replace_file($_, $target);
}
}
elsif (-e $target) {
if (compare($target, $_) == 0) {
print("identical $target\n");
}
elsif ($replace_all) {
print("replace $target\n");
replace_file($_, $target);
}
else {
print("overwrite $target? [ynaq] ");
chomp(my $in = <STDIN>);
if ($in eq 'a') {
$replace_all = 1;
replace_file($_, $target);
}
elsif ($in eq 'y') { replace_file($_, $target) }
elsif ($in eq 'q') { exit(1) }
else { print("skipping $target\n") }
}
}
else {
link_file($_, $target);
}
}
}
my $xdgBin = $ENV{"XDG_BIN_HOME"} || $ENV{"HOME"} . "/.local/bin";
my $xdgCache = $ENV{"XDG_CACHE_HOME"} || $ENV{"HOME"} . "/.cache";
my $xdgConfig = $ENV{"XDG_CONFIG_HOME"} || $ENV{"HOME"} . "/.config";
my $xdgData = $ENV{"XDG_DATA_HOME"} || $ENV{"HOME"} . "/.local/share";
my @files = glob('home/{*,.??*}');
my @xdgBin = glob('xdg-bin/{*,.??*}');
my @xdgConfig = glob('xdg-config/{*,.??*}');
my @xdgData = glob('xdg-data/{*,.??*}');
process_files(\@files, 'home/', $ENV{"HOME"} . "/.");
process_files(\@xdgBin, 'xdg-bin', $xdgBin);
process_files(\@xdgConfig, 'xdg-config', $xdgConfig);
process_files(\@xdgData, 'xdg-data', $xdgData);
my $uname = `uname -s`;
chomp($uname);
my @imports = glob('imports/' . $uname . '/*');
for my $import (@imports) {
if (-x $import) {
system($import);
}
}
# Cleanup SSH file permissions
system('chmod', 'go-rwx', $ENV{"HOME"} . "/.ssh");
# Update XDG configuration
open(XDG_CONFIG, '>', $ENV{"HOME"} . "/.xdg") or die("Unable to update XDG configuration");
print XDG_CONFIG "XDG_BIN_HOME=\"$xdgBin\";\n";
print XDG_CONFIG "XDG_CACHE_HOME=\"$xdgCache\";\n";
print XDG_CONFIG "XDG_CONFIG_HOME=\"$xdgConfig\";\n";
print XDG_CONFIG "XDG_DATA_HOME=\"$xdgData\";\n";
close(XDG_CONFIG);