-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall
executable file
·466 lines (426 loc) · 14.8 KB
/
install
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env bash
# netj's dotfiles installer
# Author: Jaeho Shin <[email protected]>
# Created: 2006-05-21
set -eu
DotfilesRoot=$(cd "$(dirname "$(test -L "$0" && readlink "$0" || echo "$0")")" >/dev/null; pwd -P)
### oneliner installer: bash <(u=git.io/netj.files; curl -fsSL $u || wget -nv -O- $u)
# some shorthands first
notOnCloud() {
# a quick way to check whether the system is an instance on the cloud
case $(hostname -f 2>/dev/null) in
# AWS EC2 seems to use special hostnames
*.ec2.internal) return 1 ;;
esac
true
}
has() { type "$1" &>/dev/null; }
running() { pgrep "$1" &>/dev/null; }
# we're in a oneliner if this isn't running off of a git repo
if ! [[ -e "$DotfilesRoot"/.git ]]; then
echo >&2 "### netj's dotfiles installer ###"
# make sure git is there
has git ||
if ! notOnCloud && has apt-get; then
# automatically install git on Debian/Ubuntu cloud instances
(
set -x
sudo apt-get install -y git || {
# retry once after an update
sudo apt-get update
sudo apt-get install -y git
}
)
else
echo >&2 "ERROR: git must be installed first"
exit 2
fi
# clone git repo to the right place unless the currently running script is the one inside the right git
[[ -d "$DotfilesRoot"/.git ]] &&
[[ $(cd "$DotfilesRoot" && git config --get remote.origin.url) = *github.com[:/]netj/dotfiles.git ]] || {
[[ -x ~/.dotfiles/install ]] ||
git clone https://github.com/netj/dotfiles.git ~/.dotfiles
~/.dotfiles/install
exec -l bash -l
}
fi
### some vocabularies first ###################################################
Home=$(cd >/dev/null; pwd -P)
# install symlinks
# determine relative path to DotfilesRoot from Home
dotdots() {
tr / '\n' <<<"$1" |
sed -n '/^$/d; s/.*/../p' |
tr '\n' /
}
if [[ $DotfilesRoot != $Home/.dotfiles ]]; then
if [[ $DotfilesRoot = $Home* ]]; then
echo >&2 "WARNING: ~${DotfilesRoot#$Home}: not a standard location. (~/.dotfiles recommended)"
dotfilesRel=${DotfilesRoot#$Home/}
else
echo >&2 "WARNING: $DotfilesRoot: not under home directory ($Home)"
dotfilesRel=$DotfilesRoot
fi
else
dotfilesRel=.dotfiles
fi
DotfilesSkipList="$DotfilesRoot/skip"
install() {
local dst=$1
local src=${2:-$dst}
local nosymlink=${3:-false}
local nobackup=${4:-false}
local n=$(shname "$dst")
# compute relative path
local rel=
case $dst in
*/*) # need relative path to DotfilesRoot if path has depth
local dd=$(dirname "$dst")
mkdir -p ~/"$dd"
dd=$(cd ~/"$dd" >/dev/null && pwd -P)
dd=${dd#$Home/}
case $dotfilesRel in
/*)
# no $rel prefix needed for an absolute path
;;
*)
rel=$(dotdots "$dd")
;;
esac
;;
esac
# check if our link is already there
if [ ~/"$dst" -ef "$DotfilesRoot/$src" ]; then
run ${n}_before "$@"
echo "$dst installed"
run ${n}_after "$@"
else
if grep -qxF "$dst" "$DotfilesSkipList" &>/dev/null; then
echo "$dst skipped"
return
fi
while read -p "install $dst? [yNxq] " -n1; do
[ -z "$REPLY" ] || echo
case "$REPLY" in
[Yy]) # yes, install
run ${n}_before "$@"
# backup previous one
! [ -e ~/"$dst" ] || $nobackup || mv -iv ~/"$dst" ~/"$dst"~
# create a symlink or a hardlink
if $nosymlink; then
ln -fv "$src" ~/"$dst"
else
ln -snfv "$rel$dotfilesRel/$src" ~/"$dst"
fi
run ${n}_after "$@"
break
;;
[Nn]|"") # no, skip
break
;;
[Xx]|"") # skip for future
echo "$dst" >>"$DotfilesSkipList"
echo "$dst skipped. To undo, run: rm $DotfilesSkipList"
sleep 1
break
;;
[Qq]) # quit, abort installation
exit 0
;;
*) # pardon me?
;;
esac
done
fi
}
shname() {
local n=$1
# sanitize name
n=${n#.}
n=${n//[^A-Za-z0-9_]/_}
echo "$n"
}
run() { [ "`type -t $1 2>/dev/null`" != function ] || (cd; "$@"); }
safeln() {
local src=$1; shift
local tgt=
for tgt in "$@"; do
if [ -e "$tgt" ]; then
[ "`readlink "$tgt"`" != "$src" ] || continue
mv -iv "$tgt" "$tgt~"
fi
[ -e "$tgt" ] || ln -sfnv "$src" "$tgt"
done
}
### essential stuffs for cloud instance #######################################
notOnCloud ||
# automatically install some essential stuffs when running on cloud instances
if has apt-get; then # only on Debian/Ubuntu instances for now
echo >&2 "# cloud instance detected, suggesting essential tools to install..."
(
set -x
sudo apt-get install \
bash-completion \
vim \
tmux \
mosh \
make \
curl \
#
) || true
fi
###############################################################################
# core
bash_after() {
(cd
# migrate from old style if needed
if [[ -L .bashrc && -L .bash_profile &&
$(readlink .bashrc) = .bash/profile ]]; then
rm -fv .bash{rc,_{profile,logout}} .bpmrc
fi
# ensure .bashrc is there
[[ -e .bashrc ]] ||
cp -v .bash/rc.default.$(uname) .bashrc ||
cp -v .bash/rc.default .bashrc ||
touch .bashrc
# migrate .bash_local if exists
if [[ -e .bash_local ]]; then
{
echo
cat .bash_local
} >>.bashrc
mv -iv .bash_local .bash_local~
fi
# keep both rc file variants in sync
safeln .bashrc .bash_profile
# install bpm
[[ -e .bash/plugins/bpm.sh ]] ||
git clone https://github.com/netj/bpm.git .bash/plugins
# enable a few essential bpm plugins
plugins=(
bash_completion
alias_completion
environment
prompt
xtermtitle
try+repeat
auto-tmux
tmux-cwd-title
tmux-cwd-sync
git-helper
logout-clear
)
[ -d .bash/plugins/enabled ] ||
. .bash/plugins/bpm.sh enable "${plugins[@]}"
# have bpm load in every interactive shell sessions
grep -q '^\. ~/\.bash/plugins/bpm\.sh' .bashrc || {
echo
echo '. ~/.bash/plugins/bpm.sh'
echo '###############################################################################'
} >>.bashrc
)
}
install .bash
install .inputrc
! has tmux ||
install .tmux.conf
if has vim; then
install .vimrc
if has make; then
vim_after() { make -C .vim; }
install .vim
fi
fi
if has git; then
git config --global color.ui auto
git config --global pull.rebase false
git config --global init.defaultBranch main
# do you even rerere? https://hackernoon.com/fix-conflicts-only-once-with-git-rerere-7d116b2cec67
git config --global rerere.enabled true
git config --global rerere.autoupdate true
git_excludesfile_after() {
git config --global core.excludesfile ~/.git-excludesfile
}
install .git-excludesfile
# no work possible w/o git + diff-so-fancy
# See: https://github.com/so-fancy/diff-so-fancy#readme
if has diff-so-fancy; then
git config --global core.pager "diff-so-fancy | less --tabs=4 -RFX"
git config --global color.diff-highlight.oldNormal "red bold"
git config --global color.diff-highlight.oldHighlight "red bold 52"
git config --global color.diff-highlight.newNormal "green bold"
git config --global color.diff-highlight.newHighlight "green bold 22"
git config --global color.diff.meta "yellow"
git config --global color.diff.frag "magenta bold"
git config --global color.diff.commit "yellow bold"
git config --global color.diff.old "red bold"
git config --global color.diff.new "green bold"
git config --global color.diff.whitespace "red reverse"
fi
# merge.tool
for tool in opendiff meld; do
if has $tool; then
git config --global merge.tool $tool
break
fi
done
if has ctags; then
# Update tags file after commit/checkout/merge/rebase
# See: http://tbaggery.com/2011/08/08/effortless-ctags-with-git.html
git_template_after() {
git config --global init.templatedir ~/.git-template
}
install .git-template
fi
# TODO shared .git/rr-cache in .git-template
fi
! top -v 2>&1 | grep -q 'procps version' ||
install .toprc
if has ssh && has make; then
ssh_Makefile_after() {
# install ssh config blocks
for c in .dotfiles/.ssh/config.d/*; do
install ${c#.dotfiles/}
done
install .ssh/gcp-start-iap-tunnel-ssh-proxy-magic.sh
install .ssh/aws-ssm-ssh-proxy-magic.sh
# backup original and enable everything if this is the first time
if ! [ -d .ssh/config.enabled ]; then
! [ -e .ssh/config ] || mv -iv .ssh/config .ssh/config.orig
make -C .ssh enable
fi
# generate .ssh/config
make -C .ssh config
}
install .ssh/Makefile
fi
! has screen ||
install .screenrc
if has emacs && has make; then
emacs_d_elisp_Makefile_after() { make -C .emacs.d/elisp; }
install .emacs
install .emacs.d/elisp/Makefile
fi
# Mac OS X
case $(uname) in Darwin)
install .zprofile
running-app() { running "$@"; } # XXX workaround for osascript's intermittent slowness
#running-app() { [ true = "$(osascript 2>/dev/null -e '
# tell application "System Events" to exists process "'"$1"'"')" ]; }
mdfind-has() { [ -n "$(mdfind "$@" | head -1)" ]; }
has-executable() { mdfind-has 'kMDItemContentType == public.unix-executable
&& kMDItemFSName == "'"$1"'"'; }
# TODO Mac/Tweaks.sh
# DefaultKeyBinding.dict
# See: http://hints.macworld.com/article.php?story=20060317045211408
# See: http://www.hcs.harvard.edu/~jrus/site/cocoa-text.html
install Library/KeyBindings/DefaultKeyBinding.dict Mac/KeyBindings/DefaultKeyBinding.dict true
# install Karabiner settings (formerly known as KeyRemap4MacBook, prior to version 10.0.0)
if running-app Karabiner; then
for xml in Mac/Karabiner/*.xml; do
[ -e "$xml" ] || continue
Library_Application_Support_Karabiner_private_xml_after() {
local x="$(
app=/Applications/Karabiner.app
if [ -d "$app" ]; then echo $app
else mdfind Karabiner.app | head -n 1
fi)/Contents/Library/bin/karabiner"
! [ -x "$x" ] || "$x" reloadxml
}
install Library/"Application Support"/"${xml#Mac/}" "$xml"
done
fi
# install sleepwatcher scripts
if has sleepwatcher || running sleepwatcher; then # || has-executable sleepwatcher; then
install .sleep Mac/sleepwatcher/sleep
install .wakeup Mac/sleepwatcher/wakeup
fi
Library_Scripts_MonitorLoginAttempts_command_after() {
if ! [ -x Library/Scripts/watch-notes ]; then
[ -d "$DotfilesRoot"/Mac/watch-notes/.git ] ||
git clone https://github.com/netj/watch-notes.git "$DotfilesRoot"/Mac/watch-notes
cd "$DotfilesRoot"/Mac/watch-notes
make
command install watch-notes ~/Library/Scripts/
fi
}
Library_Scripts_TakeOneLoginShot_sh_after() {
if ! [ -x Library/Scripts/LocateMe ]; then
[ -d "$DotfilesRoot"/Mac/LocateMe/.git ] ||
git clone https://github.com/netj/LocateMe.git "$DotfilesRoot"/Mac/LocateMe
cd "$DotfilesRoot"/Mac/LocateMe
if ! xcodebuild; then
mkdir -p build/Release
clang -o build/Release/LocateMe LocateMe.m -framework foundation -framework corelocation
fi
command install build/Release/LocateMe ~/Library/Scripts/
fi
}
# install shell/python/perl scripts
for s in Mac/Scripts/{*.{command,tool,sh,py,pl},Applications/ControlPlane/*.sh}; do
[ -e "$s" ] || continue
install Library/"${s#Mac/}" "$s" true true
done
# install applescripts
if running-app "BetterTouchTool" || running-app "FastScripts"; then
cd "$DotfilesRoot"
scptd=Mac/Scripts/compiled/
mkdir -p "$scptd"
compilescpt() {
local o=$1 s="$2"
cd "$DotfilesRoot"
local n=$(shname "$o")
run ${n}_before "$@"
! [ "$o" -nt "$s" ] || return 0
echo "$o: compiling ${s##*/}"
mkdir -p "$(dirname "$o")"
xattr -w com.apple.TextEncoding 'UTF-8;134217984' "$s"
osacompile -x -o "$o" "$s"
}
installscpt() {
local src=$1
local srcrel=${src#Mac/Scripts/}
local obj="$scptd${srcrel%.applescript}".scpt
local dst=Library/Scripts/"${obj#$scptd}"
local n=$(shname "$dst")
[ x"$(type -t ${n}_before)" = x"function" ] ||
eval "${n}_before() { compilescpt '${obj//\'/\'\\\'\'}' '${src//\'/\'\\\'\'}'; }" # TODO escape obj and src
install "$dst" "$obj" true true
}
Mac_Scripts_compiled_Applications_Finder_lselect_scpt_before() {
cd "$DotfilesRoot"
! [ -e Mac/Scripts/Applications/Finder/lselect.applescript ] || return 0
curl -RLo Mac/Scripts/Applications/Finder/lselect.applescript http://anoved.net/files/lselect.applescript
}
Library_Scripts_Applications_iTunes_iTunes_SED_scpt_before() {
cd "$DotfilesRoot"
! [ -e "$scptd"/Applications/iTunes/iTunes\ SED.scpt ] || return 0
curl -RLO http://aurelio.googlecode.com/files/iTunes_SED-1.1.zip
ditto -xk iTunes_SED-*.zip "$scptd"/Applications/iTunes/
rm -f iTunes_SED-*.zip
}
for s in Mac/Scripts/{*,Applications/{*/*,Finder/lselect,iTunes/iTunes\ SED}}.applescript; do
[ -e "$s" ] || continue
installscpt "$s"
done
if running-app "BetterTouchTool"; then
# install AppleScript wrapper for shell scripts (e.g., for Better Touch Tool)
for s in Mac/Scripts/*.sh; do
dst=Library/"${s#Mac/}"
[ -e ~/"$dst" ] || continue
wrpr="Mac/Scripts/Applications/BetterTouchTool${s#Mac/Scripts}.applescript"
[ -e "$wrpr" ] || {
mkdir -p "$(dirname "$wrpr")"
echo 'do shell script "~/'\'${dst//\'/\'\\\'\'}\''"' >"$wrpr"
}
installscpt "$wrpr"
done
fi
fi
;; esac
# GUI
if has make && { [ -n "${DISPLAY:-}" ] || running WindowServer; }; then
fonts_d_after() { make -C .fonts.d; }
install .fonts.conf
install .fonts.d Fonts
fi