forked from cowboy/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdotfiles
executable file
·329 lines (291 loc) · 8.93 KB
/
dotfiles
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
#!/usr/bin/env bash
[[ "$1" == "source" ]] || \
echo 'Dotfiles - "bondezbond" Ezbon Jacob forked from Cowboy" Ben Alman - http://benalman.com/'
if [[ "$1" == "-h" || "$1" == "--help" ]]; then cat <<HELP
Usage: $(basename "$0")
See the README for documentation.
https://github.com/cowboy/dotfiles
Copyright (c) 2014 "Cowboy" Ben Alman
Licensed under the MIT license.
http://benalman.com/about/license/
HELP
exit; fi
###########################################
# GENERAL PURPOSE EXPORTED VARS / FUNCTIONS
###########################################
# Where the magic happens.
export DOTFILES=~/.dotfiles
# Logging stuff.
function e_header() { echo -e "\n\033[1m$@\033[0m"; }
function e_success() { echo -e " \033[1;32m✔\033[0m $@"; }
function e_error() { echo -e " \033[1;31m✖\033[0m $@"; }
function e_arrow() { echo -e " \033[1;34m➜\033[0m $@"; }
# For testing.
function assert() {
local success modes equals actual expected
modes=(e_error e_success); equals=("!=" "=="); expected="$1"; shift
actual="$("$@")"
[[ "$actual" == "$expected" ]] && success=1 || success=0
${modes[success]} "\"$actual\" ${equals[success]} \"$expected\""
}
# OS detection
function is_osx() {
[[ "$OSTYPE" =~ ^darwin ]] || return 1
}
function is_ubuntu() {
[[ "$(cat /etc/issue 2> /dev/null)" =~ Ubuntu ]] || return 1
}
function get_os() {
for os in osx ubuntu; do
is_$os; [[ $? == ${1:-0} ]] && echo $os
done
}
# Remove an entry from $PATH
# Based on http://stackoverflow.com/a/2108540/142339
function path_remove() {
local arg path
path=":$PATH:"
for arg in "$@"; do path="${path//:$arg:/:}"; done
path="${path%:}"
path="${path#:}"
echo "$path"
}
# Display a fancy multi-select menu.
# Inspired by http://serverfault.com/a/298312
function prompt_menu() {
local exitcode prompt choices nums i n
exitcode=0
if [[ "$2" ]]; then
_prompt_menu_draws "$1"
read -t $2 -n 1 -sp "To edit this list, press any key within $2 seconds. "
exitcode=$?
echo ""
fi 1>&2
if [[ "$exitcode" == 0 ]]; then
prompt="Toggle options (Separate options with spaces, ENTER when done): "
while _prompt_menu_draws "$1" 1 && read -rp "$prompt" nums && [[ "$nums" ]]; do
_prompt_menu_adds $nums
done
fi 1>&2
_prompt_menu_adds
}
function _prompt_menu_iter() {
local i sel state
local fn=$1; shift
for i in "${!menu_options[@]}"; do
state=0
for sel in "${menu_selects[@]}"; do
[[ "$sel" == "${menu_options[i]}" ]] && state=1 && break
done
$fn $state $i "$@"
done
}
function _prompt_menu_draws() {
e_header "$1"
_prompt_menu_iter _prompt_menu_draw "$2"
}
function _prompt_menu_draw() {
local modes=(error success)
if [[ "$3" ]]; then
e_${modes[$1]} "$(printf "%2d) %s\n" $(($2+1)) "${menu_options[$2]}")"
else
e_${modes[$1]} "${menu_options[$2]}"
fi
}
function _prompt_menu_adds() {
_prompt_menu_result=()
_prompt_menu_iter _prompt_menu_add "$@"
menu_selects=("${_prompt_menu_result[@]}")
}
function _prompt_menu_add() {
local state i n keep match
state=$1; shift
i=$1; shift
for n in "$@"; do
if [[ $n =~ ^[0-9]+$ ]] && (( n-1 == i )); then
match=1; [[ "$state" == 0 ]] && keep=1
fi
done
[[ ! "$match" && "$state" == 1 || "$keep" ]] || return
_prompt_menu_result=("${_prompt_menu_result[@]}" "${menu_options[i]}")
}
# Given strings containing space-delimited words A and B, "setdiff A B" will
# return all words in A that do not exist in B. Arrays in bash are insane
# (and not in a good way).
# From http://stackoverflow.com/a/1617303/142339
function setdiff() {
local debug skip a b
if [[ "$1" == 1 ]]; then debug=1; shift; fi
if [[ "$1" ]]; then
local setdiffA setdiffB setdiffC
setdiffA=($1); setdiffB=($2)
fi
setdiffC=()
for a in "${setdiffA[@]}"; do
skip=
for b in "${setdiffB[@]}"; do
[[ "$a" == "$b" ]] && skip=1 && break
done
[[ "$skip" ]] || setdiffC=("${setdiffC[@]}" "$a")
done
[[ "$debug" ]] && for a in setdiffA setdiffB setdiffC; do
echo "$a ($(eval echo "\${#$a[*]}")) $(eval echo "\${$a[*]}")" 1>&2
done
[[ "$1" ]] && echo "${setdiffC[@]}"
}
# If this file was being sourced, exit now.
[[ "$1" == "source" ]] && return
###########################################
# INTERNAL DOTFILES "INIT" VARS / FUNCTIONS
###########################################
# Initialize.
init_file=$DOTFILES/caches/init/selected
function init_files() {
local i f dirname oses os opt remove
dirname="$(dirname "$1")"
f=("$@")
menu_options=(); menu_selects=()
for i in "${!f[@]}"; do menu_options[i]="$(basename "${f[i]}")"; done
if [[ -e "$init_file" ]]; then
# Read cache file if possible
IFS=$'\n' read -d '' -r -a menu_selects < "$init_file"
else
# Otherwise default to all scripts not specifically for other OSes
oses=($(get_os 1))
for opt in "${menu_options[@]}"; do
remove=
for os in "${oses[@]}"; do
[[ "$opt" =~ (^|[^a-z])$os($|[^a-z]) ]] && remove=1 && break
done
[[ "$remove" ]] || menu_selects=("${menu_selects[@]}" "$opt")
done
fi
prompt_menu "Run the following init scripts?" $prompt_delay
# Write out cache file for future reading.
rm "$init_file" 2>/dev/null
for i in "${!menu_selects[@]}"; do
echo "${menu_selects[i]}" >> "$init_file"
echo "$dirname/${menu_selects[i]}"
done
}
function init_do() {
e_header "Sourcing $(basename "$2")"
source "$2"
}
# Copy files.
function copy_header() { e_header "Copying files into home directory"; }
function copy_test() {
if [[ -e "$2" && ! "$(cmp "$1" "$2" 2> /dev/null)" ]]; then
echo "same file"
elif [[ "$1" -ot "$2" ]]; then
echo "destination file newer"
fi
}
function copy_do() {
e_success "Copying ~/$1."
cp "$2" ~/
}
# Link files.
function link_header() { e_header "Linking files into home directory"; }
function link_test() {
[[ "$1" -ef "$2" ]] && echo "same file"
}
function link_do() {
e_success "Linking ~/$1."
ln -sf ${2#$HOME/} ~/
}
# Copy, link, init, etc.
function do_stuff() {
local base dest skip
local files=($DOTFILES/$1/*)
[[ $(declare -f "$1_files") ]] && files=($($1_files "${files[@]}"))
# No files? abort.
if (( ${#files[@]} == 0 )); then return; fi
# Run _header function only if declared.
[[ $(declare -f "$1_header") ]] && "$1_header"
# Iterate over files.
for file in "${files[@]}"; do
base="$(basename $file)"
dest="$HOME/$base"
# Run _test function only if declared.
if [[ $(declare -f "$1_test") ]]; then
# If _test function returns a string, skip file and print that message.
skip="$("$1_test" "$file" "$dest")"
if [[ "$skip" ]]; then
e_error "Skipping ~/$base, $skip."
continue
fi
# Destination file already exists in ~/. Back it up!
if [[ -e "$dest" ]]; then
e_arrow "Backing up ~/$base."
# Set backup flag, so a nice message can be shown at the end.
backup=1
# Create backup dir if it doesn't already exist.
[[ -e "$backup_dir" ]] || mkdir -p "$backup_dir"
# Backup file / link / whatever.
mv "$dest" "$backup_dir"
fi
fi
# Do stuff.
"$1_do" "$base" "$file"
done
}
# Enough with the functions, let's do stuff.
export prompt_delay=5
# Ensure that we can actually, like, compile anything.
if [[ ! "$(type -P gcc)" ]] && is_osx; then
e_error "XCode or the Command Line Tools for XCode must be installed first."
exit 1
fi
# If Git is not installed, install it (Ubuntu only, since Git comes standard
# with recent XCode or CLT)
if [[ ! "$(type -P git)" ]] && is_ubuntu; then
e_header "Installing Git"
sudo apt-get -qq install git-core
fi
# If Git isn't installed by now, something exploded. We gots to quit!
if [[ ! "$(type -P git)" ]]; then
e_error "Git should be installed. It isn't. Aborting."
exit 1
fi
# Initialize.
if [[ ! -d $DOTFILES ]]; then
# $DOTFILES directory doesn't exist? Clone it!
new_dotfiles_install=1
prompt_delay=15
e_header "Downloading dotfiles"
git clone --recursive git://github.com/${github_user:-bondezbond}/dotfiles.git $DOTFILES
cd $DOTFILES
elif [[ "$1" != "restart" ]]; then
# Make sure we have the latest files.
e_header "Updating dotfiles"
cd $DOTFILES
prev_head="$(git rev-parse HEAD)"
git pull
git submodule update --init --recursive --quiet
if [[ "$(git rev-parse HEAD)" != "$prev_head" ]]; then
e_header "Changes detected, restarting script"
exec "$0" "restart"
fi
fi
# Add binaries into the path
[[ -d $DOTFILES/bin ]] && PATH=$DOTFILES/bin:$PATH
export PATH
# Tweak file globbing.
shopt -s dotglob
shopt -s nullglob
# Create caches dir and init subdir, if they don't already exist.
mkdir -p "$DOTFILES/caches/init"
# If backups are needed, this is where they'll go.
backup_dir="$DOTFILES/backups/$(date "+%Y_%m_%d-%H_%M_%S")/"
backup=
# Execute code for each file in these subdirectories.
do_stuff "copy"
do_stuff "link"
do_stuff "init"
# Alert if backups were made.
if [[ "$backup" ]]; then
echo -e "\nBackups were moved to ~/${backup_dir#$HOME/}"
fi
# All done!
e_header "All done!"