-
Notifications
You must be signed in to change notification settings - Fork 16
/
apply.sh
executable file
·128 lines (109 loc) · 2.75 KB
/
apply.sh
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
#! /usr/bin/env bash
#
# Author: Bert Van Vreckem <bert.vanvreckem@gmail.com>
#
#/ Usage: SCRIPTNAME [OPTIONS]...
#/
#/ Install the dotfiles on the current host using GNU Stow. The files are kept
#/ in separate directories with paths relative to the home directory where they
#/ should be installed. You can pick which directories to install by
#/ enumerating
#/
#/ OPTIONS
#/ -h, --help
#/ Print this help message
#/ -n, --no
#/ Print what would be installed without actually executing
#/
#{{{ Bash settings
# abort on nonzero exitstatus
set -o errexit
# abort on unbound variable
set -o nounset
# don't hide errors within pipes
set -o pipefail
#}}}
#{{{ Variables
IFS=$'\t\n' # Split on newlines and tabs (but not on spaces)
script_name=$(basename "${0}")
script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# Debug info ('on' to enable)
readonly debug='on'
# File containing the directories to install on this host
stow_dirs="$(hostname).dirs"
# If `yes`, run stow with the --no option
noop=no
readonly script_name script_dir stow_dirs
#}}}
main() {
process_args "${@}"
if ! which stow &> /dev/null; then
error "Command 'stow' not found, install it first!"
exit 1
fi
if [ ! -f "${stow_dirs}" ]; then
error "I have no rules for this host."
error "Create file '${stow_dirs}' with a list of directories to install"
exit 1
fi
while read -r dir; do
[ "${dir}" = '' ] && break
log "Installing dir: ${dir}"
if [ "${noop}" = 'yes' ]; then
stow --target="${HOME}" --verbose=2 --no "${dir}"
else
stow --target="${HOME}" --verbose=2 "${dir}"
fi
done < "$(hostname).dirs"
}
#{{{ Helper functions
# Usage: process_args "${@}"
process_args() {
for arg in "${@}"; do
case "${arg}" in
-h|--help)
usage
exit 0
;;
-n|--no)
noop=yes
;;
-*)
error "Unknow option: ${arg}"
usage
exit 2
;;
*)
error "This script does not take arguments"
usage
exit 2
;;
esac
done
}
# Print usage message on stdout by parsing start of script comments
# The comment should start with #/ followed by either a newline or a space
usage() {
grep '^#/' "${script_dir}/${script_name}" | sed 's/^#\/\($\| \)//'
}
# Usage: log [ARG]...
#
# Prints all arguments on the standard output stream
log() {
printf 'ℹ️ \e[0;34m%s\e[0m\n' "${*}"
}
# Usage: debug [ARG]...
#
# Prints all arguments on the standard output stream,
# if debug output is enabled
debug() {
[ "${debug}" != 'on' ] || printf '🪲 \e[0;36mi%s\e[0m\n' "${*}"
}
# Usage: error [ARG]...
#
# Prints all arguments on the standard error stream
error() {
printf '🔥 \e[0;31m%s\e[0m\n' "${*}" 1>&2
}
#}}}
main "${@}"