-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigctl
executable file
·439 lines (387 loc) · 13.4 KB
/
configctl
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
#!/bin/sh
# configctl - control config setup and installation
# Usage: ./configctl [ACTION]
# Control configuration file installation.
# ACTION is one of: install uninstall prepare migrate link misc preload precompile check.
#
# The 'install' action is equivalent to the 'prepare', 'migrate', 'link', 'misc', 'preload', and 'precompile' actions, in that order.
# The 'uninstall' action is unimplemented.
# The 'prepare' action creates directories.
# The 'migrate' action migrates old configurations.
# The 'link' action creates symlinks and hardlinks.
# The 'misc' action performs miscellaneous tasks, like touching files.
# The 'preload' action downloads resources from the network, and is dependent on 'link'. It is the only action requiring network access.
# The 'precompile' action precompiles zsh scripts, and is dependent on 'link' and 'preload'.
# The 'check' action searches for common binaries and reports if they are missing.
#
# Exit status is 0 upon success, nonzero upon error.
# Targets POSIX-compliant shells - no less, and no more.
export POSIXLY_CORRECT=
# VARIABLE DECLARATIONS
LINK_FILES=".bash_aliases .bash_logout .bash_profile .bashrc .byobu .emacs .gitconfig .gitignore .gitmodules .profile .reportbugrc .screenrc .selected_editor .zsh_favlist .zshrc.pre-oh-my-zsh bin .pam_environment .netscape .hgrc .zshrc .zprofile .offlineimaprc .gnus.el .caffrc .ispell_english .muttrc .msmtprc"
HARDLINKS=".pgpkey"
ZSH_PRECOMPILE=".zshrc .zprofile antigen/antigen.zsh .oh-my-zsh/lib/*.zsh .oh-my-zsh/themes/*.zsh-theme .oh-my-zsh/plugins/*/*.zsh .oh-my-zsh/oh-my-zsh.sh"
DIRS=".gnupg .ssh .aptitude .config .bin/bash_completion .bin/todotxt .bin/bin .emacs.d/lisp .todo .bazaar .local/share/applications .config/profanity/themes .mutt"
LINK_DIRS=".config/awesome .config/cower .config/terminator .config/profanity .mutt"
AFTER_DIRS=".gnupg/gpg.conf .ssh/config .aptitude/config .todo/config .bazaar/bazaar.conf .local/share/applications/firefox-nightly.desktop"
TOUCH_FILES=".fetchmail.log .msmtp.log .mutt/muttrc.mailboxes"
CONFIG_DIR=$(pwd)
TARGET_DIR="$HOME"
DEFAULT_ACTION=install
NUM_WARNINGS=0
NUM_MIGRATIONS=0
if [ -f $CONFIG_DIR/local-config ]; then
CONFIG_DATA="$(cat $CONFIG_DIR/local-config)"
else
CONFIG_DATA=''
fi
# UTILITY FUNCTIONS
logdbgr() {
echo "[\033[35mdbgr\033[0m] $1"
}
loginfo() {
echo "[\033[36minfo\033[0m] $1"
}
logwarn() {
echo "[\033[33mwarn\033[0m] $1"
NUM_WARNINGS=$(($NUM_WARNINGS + 1))
}
logfail() {
echo "[\033[31mfail\033[0m] $1"
echo "Finished unsuccessfully, with 1 failure, $NUM_WARNINGS warning(s), and 0 migrations."
exit $2
}
checkavailable() {
logdbgr "Checking for $1."
if command -v $1 2>&1 >/dev/null; then
loginfo "Detected $(type $1 | sed 's/is/as/')."
else
logfail "Failed to detect $1. Cannot $2."
fi
}
checkavailable_warn() {
logdbgr "Checking for $1."
if command -v $1 2>&1 >/dev/null; then
loginfo "Detected $(type $1 | sed 's/is/as/')."
else
logwarn "Failed to detect $1."
fi
}
checkssh() {
if [ ${1+x} ]; then
DESCRIPTION=" $1"
fi
if [ ${2+x} ]; then
KEYNAME="_$2"
fi
logdbgr "Checking for$DESCRIPTION SSH key..."
if [ ! -e "$TARGET_DIR"/.ssh/id_rsa$KEYNAME ]; then
logwarn "Could not find$DESCRIPTION SSH key."
else
loginfo "Found$DESCRIPTION SSH key at $TARGET_DIR/.ssh/id_rsa$KEYNAME."
fi
logdbgr "Done checking for$DESCRIPTION SSH key."
}
print_help() {
cat <<EOF
Usage: ./configctl [ACTION]
Control configuration file installation.
ACTION is one of: $ACTIONS.
The 'install' action is equivalent to the 'prepare', 'migrate', 'link', 'misc', 'preload', and 'precompile' actions, in that order.
The 'uninstall' action is unimplemented.
The 'prepare' action creates directories.
The 'migrate' action migrates old configurations.
The 'link' action creates symlinks and hardlinks.
The 'misc' action performs miscellaneous tasks, like touching files.
The 'preload' action downloads resources from the network, and is dependent on 'link'. It is the only action requiring network access.
The 'precompile' action precompiles zsh scripts, and is dependent on 'link' and 'preload'.
The 'check' action searches for common binaries and reports if they are missing.
Exit status is 0 upon success, nonzero upon error.
Version: configctl@$(git rev-parse --verify --short HEAD)
Copyright (C) 2015 Alex Jordan <alex@strugee.net>.
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://www.wtfpl.net/ for more details.
EOF
}
# ARGUMENT PARSING
ACTIONS="install uninstall prepare migrate link misc preload precompile check"
# Validate actions
if [ -n "$1" ]; then
for j in $ACTIONS; do [ $j = $1 ] && ACTION_VALID=; done
else
# The default action is automatically valid
ACTION_VALID=
fi
[ ${ACTION_VALID+x} ] || { print_help; exit 1; }
case "$1" in
'install')
logdbgr "Using action 'install'."
;;
'uninstall')
logdbgr "Using action 'uninstall'."
;;
'prepare')
logdbgr "Using action 'prepare'."
;;
'migrate')
logdbgr "Using action 'migrate'."
;;
'link')
logdbgr "Using action 'link'."
;;
'misc')
logdbgr "Using action 'misc'."
;;
'preload')
logdbgr "Using action 'preload'."
;;
'precompile')
logdbgr "Using action 'precompile'."
;;
'check')
logdbgr "Using action 'check'."
;;
*)
logdbgr "Using default action ('$DEFAULT_ACTION')."
ACTION=$DEFAULT_ACTION
;;
esac
[ -z $ACTION ] && ACTION=$1
for i in $@; do
true
done
# SANITY CHECKS
loginfo "Detected configuration directory as $CONFIG_DIR."
loginfo "Using target directory $TARGET_DIR."
# Error checking on target directory
logdbgr "Checking if target directory exists."
if ! [ -e "$TARGET_DIR" ]; then
logwarn "Target directory does not exist."
loginfo "Creating target directory $TARGET_DIR."
mkdir -p "$TARGET_DIR"
# TODO handle errors
fi
logdbgr "Checking if target directory is a directory."
if ! [ -d "$TARGET_DIR" ]; then
logfail "Fatal error: $TARGET_DIR is not a directory." 1
fi
checkavailable git "download resources from Git"
checkavailable curl "download static tarballs"
checkavailable emacs "download Emacs packages"
checkavailable patch "patch ZNC.el"
# Detect zsh
logdbgr "Checking for zsh."
if command -v zsh 2>&1 >/dev/null; then
loginfo "Detected $(type zsh | sed 's/is/as/')."
HAS_ZSH=0
else
logwarn "Failed to detect zsh. Precompilation will not occur."
HAS_ZSH=1
fi
logdbgr "Finished preparing."
logdbgr "Executing main script body."
# ACTION DEFINITIONS
action_prepare() {
loginfo "Creating configuration directories..."
for i in $DIRS; do
printf "Creating $i..."
mkdir -p "$TARGET_DIR"/$i
printf " done.\n"
done
loginfo "Done creating configuration directories."
}
action_migrate() {
loginfo "Migrating old configurations..."
if ls $TARGET_DIR/.emacs.d/elpa/git-commit-mode* > /dev/null 2>&1; then
printf "Detected obsolete git-commit-mode, removing..."
NUM_MIGRATIONS=$(($NUM_MIGRATIONS + 1))
rm -rf $TARGET_DIR/.emacs.d/elpa/git-commit-mode*
printf " done.\n"
fi
if ls $TARGET_DIR/.emacs.d/elpa/git-rebase-mode* > /dev/null 2>&1; then
printf "Detected obsolete git-rebase-mode, removing..."
NUM_MIGRATIONS=$(($NUM_MIGRATIONS + 1))
rm -rf $TARGET_DIR/.emacs.d/elpa/git-rebase-mode*
printf " done.\n"
fi
if [ $NUM_MIGRATIONS = 0 ]; then
echo "Nothing to migrate."
fi
loginfo "Done migrating old configurations."
}
action_link() {
loginfo "Installing symlinks..."
for i in $LINK_FILES; do
printf "Installing $i..."
if [ -L "$TARGET_DIR"/$i ]; then
printf " skipped.\n"
continue;
elif [ -e "$TARGET_DIR"/$i ]; then
printf " aborted.\n"
logwarn "Refusing to overwrite file $TARGET_DIR/$i."
continue;
fi
ln -s $CONFIG_DIR/$i "$TARGET_DIR"
printf " done.\n"
done
loginfo "Done installing symlinks."
loginfo "Installing hardlinks..."
for i in $HARDLINKS; do
printf "Installing $i..."
if [ $CONFIG_DIR/$i -ef "$TARGET_DIR"/$i ]; then
printf " skipped.\n"
continue;
elif [ -e "$TARGET_DIR"/$i ]; then
printf " aborted.\n"
logwarn "Refusing to overwrite file $TARGET_DIR/$i."
continue;
fi
ln $CONFIG_DIR/$i "$TARGET_DIR"
printf " done.\n"
done
loginfo "Done installing hardlinks."
loginfo "Installing configuration directory symlinks..."
logwarn "Installing configuration directory symlinks is unimplemented."
loginfo "Done installing configuration directory symlinks."
loginfo "Installing symlinks nested in a directory..."
logwarn "Installing symlinks nested in a directory is unimplemented."
loginfo "Done installing symlinks nested in a directory."
}
action_misc() {
loginfo "Touching files..."
for i in $TOUCH_FILES; do
printf "Touching $i..."
touch "$TARGET_DIR"/$i
printf " done.\n"
done
loginfo "Done touching files."
}
action_preload() {
loginfo "Cloning resources from git..."
# TODO: handle errors
# TODO: print logs for each clone operation
logdbgr "Cloning oh-my-zsh."
git clone 'git://github.com/robbyrussell/oh-my-zsh' "$TARGET_DIR"/.oh-my-zsh
logdbgr "Cloning Antigen."
git clone 'git://github.com/zsh-users/antigen.git' "$TARGET_DIR"/antigen
logdbgr "Cloning moz-git-tools."
git clone 'git://github.com/mozilla/moz-git-tools.git' "$TARGET_DIR"/.bin/moz-git-tools
logdbgr "Initializing moz-git-tools submodules."
logdbgr "Changing working directory to $TARGET_DIR/.bin/moz-git-tools."
cd "$TARGET_DIR"/.bin/moz-git-tools
git submodule update --init
cd $CONFIG_DIR
logdbgr "Changing working directory to $CONFIG_DIR."
loginfo "Done cloning resources from git."
loginfo "Downloading static tarballs..."
if ! [ -d "$TARGET_DIR"/.bin/todotxt/todo.txt_cli-2.10 ]; then
logdbgr "Downloading todo.txt-cli."
logdbgr "Changing working directory to $TARGET_DIR/.bin/todotxt."
cd "$TARGET_DIR"/.bin/todotxt
curl -L https://github.com/ginatrapani/todo.txt-cli/releases/download/v2.10/todo.txt_cli-2.10.tar.gz | gunzip | tar -x
# TODO: make this not hard-coded
# TODO: this doesn't handle already-existing links
chmod +x "$TARGET_DIR"/.bin/todotxt/todo.txt_cli-2.10/todo.sh
ln -s "$TARGET_DIR"/.bin/todotxt/todo.txt_cli-2.10/todo_completion "$TARGET_DIR"/.bin/bash_completion
ln -s "$TARGET_DIR"/.bin/todotxt/todo.txt_cli-2.10/todo.sh "$TARGET_DIR"/.bin/bin/todo.sh
logdbgr "Changing working directory to $CONFIG_DIR."
cd $CONFIG_DIR
else
logdbgr "Skipping todo.txt-cli due to already-existing directory."
fi
loginfo "Done downloading static tarballs."
loginfo "Ensuring that package.el is available..."
# TODO: make this actually check the result of (require 'package) instead of just testing version
# TODO: clean an old package.el if Emacs has been upgraded
if [ $(emacs --version | head -1 | cut -d ' ' -f 3 | cut -d . -f 1) -lt 24 -a ! -e "$TARGET_DIR"/.emacs.d/lisp/package.el ]; then
logdbgr "Emacs version is too old to be distributed with package.el."
logdbgr "Downloading package.el."
curl 'http://git.savannah.gnu.org/gitweb/?p=emacs.git;a=blob_plain;hb=ba08b24186711eaeb3748f3d1f23e2c2d9ed0d09;f=lisp/emacs-lisp/package.el' > "$TARGET_DIR"/.emacs.d/lisp/package.el
fi
loginfo "Done ensuring that package.el is available."
loginfo "Downloading Emacs packages..."
emacs --script .emacs
loginfo "Done downloading Emacs packages."
loginfo "Patching ZNC.el..."
cd "$TARGET_DIR"/.emacs.d/elpa/znc-*
curl https://github.com/rschuetzler/ZNC.el/commit/e58db9afef82957ffcea6a72977f5f2b4fb53d05.patch | patch
# FIXME
#emacs --eval "(byte-recompile-directory $TARGET_DIR/\.emacs\.d/elpa/znc-*/ 0)"
cd $CONFIG_DIR
loginfo "Done patching ZNC.el."
loginfo "Handling submodules..."
git submodule update --init
loginfo "Done handling submodules."
}
action_precompile() {
if [ $HAS_ZSH = 0 ]; then
loginfo "Precompiling zsh scripts..."
logdbgr "Changing working directory to $TARGET_DIR."
cd $TARGET_DIR
for i in $ZSH_PRECOMPILE; do
printf "Precompiling $i..."
zsh -c "zcompile $i"
printf " done.\n"
done
logdbgr "Changing working directory to $CONFIG_DIR."
cd $CONFIG_DIR
loginfo "Done precompiling zsh scripts."
else
logwarn "Not precompiling zsh scripts due to missing zsh."
fi
}
action_check() {
logdbgr 'Checking for standard utilities.'
checkavailable_warn sponge
checkavailable_warn ssh
checkavailable_warn dos2unix
checkavailable_warn pv
checkavailable_warn sl
checkavailable_warn nano
checkavailable_warn pkill
checkavailable_warn killall
checkavailable_warn tree
if ! [ $(echo "$CONFIG_DATA" | grep headless) ]; then
checkavailable_warn parcimonie
checkavailable_warn offlineimap
checkavailable_warn firefox-nightly
checkavailable_warn terminator
checkavailable_warn profanity
checkavailable_warn secret-tool
# TODO: check for the presence of Profanity keyring secrets
checkavailable_warn xclip
fi
if ! [ $(echo "$CONFIG_DATA" | grep no-nodejs) ]; then
checkavailable_warn node
checkavailable_warn npm
checkavailable_warn grunt
checkavailable_warn gulp
checkavailable_warn yo
checkavailable_warn yosay
checkavailable_warn nsp
fi
logdbgr 'Done checking for standard utilities.'
logdbgr 'Checking for SSH keys...'
if ! [ $(echo "$CONFIG_DATA" | grep no-ssh) ]; then
checkssh general-use
fi
checkssh GitHub github
logdbgr 'Done checking for SSH keys.'
}
# MAIN SCRIPT BODY
if [ $ACTION = 'install' ]; then
action_prepare
action_migrate
action_link
action_misc
action_preload
action_precompile
elif [ $ACTION = 'uninstall' ]; then
logfail 'Uninstallation is unimpemented.' 2
else
action_$ACTION
fi
echo "Finished successfully, with 0 failures, $NUM_WARNINGS warning(s), and $NUM_MIGRATIONS migrations."