-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·108 lines (87 loc) · 2.48 KB
/
setup.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
#!/bin/bash
error() {
echo "$@" 1>&2
exit 1
}
setup() {
# update package lists
sudo apt-get update
}
install_packages() {
xargs -a <(grep '^\s*[^#]' "${1}") -- sudo apt-get install -y
}
usage() {
cat <<EOF
Usage: ${0##*/} [options] module ...
automatically set up system 'module' using configured dotfiles
options:
-c checkout repo submodules before taking any action
-f force overwrite a dotfile that already exists
EOF
}
bad_usage() {
usage 1>&2
exit 1
}
main() {
# parse args
local opts="fc"
local getopt_out=$(getopt --name "${0##*/}" \
--options "${opts}" -- "${@}") &&
eval set -- "${getopt_out}" ||
bad_usage
# init flags
local checkout=false
local force=false
# read flag values
while [ ${#} -ne 0 ]; do
case ${1} in
-c) checkout=true; shift;;
-f) force=true; shift;;
--) shift; break;;
esac
done
# get module names
local modules=( "$@" )
[ ${#modules[@]} -eq 0 ] && bad_usage
# get base dir location
local base_dir="${HOME}/dotfiles"
local target_dir="$base_dir/.."
# setup system
local global_pkg_file="$base_dir/packages"
setup && install_packages "$global_pkg_file" ||
error "system setup failed"
# checkout submodules if needed
if $checkout; then
git submodule update --init --recursive
fi
# set up modules
for module in "${modules[@]}"; do
# ensure valid module
[ -d "$module" ] || error "module: $module does not exist"
# run setup script if any
local setup_script="${module}_setup.sh"
if [ -x "$setup_script" ]; then
"$base_dir/$setup_script" || error "script failed: $setup_script"
fi
# install package requiements if any
local package_file="${module}_packages"
if [ -e "$package_file" ]; then
install_packages "$package_file" ||
error "package installation failed"
fi
# if force set, delete any possible conflicts
if $force; then
for file in $(ls -a "$module"); do
[ "$file" = "." -o "$file" = ".." ] && continue;
local path="$target_dir/$file"
echo "warning, removing: $file"
rm -rf "$path"
done
fi
# stow the module
stow "$module" || error "stow failed for module: $module"
done
return 0
}
main "$@"