-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.sh
executable file
·171 lines (150 loc) · 2.94 KB
/
bootstrap.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
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
#!/usr/bin/env bash
# Exit on error
set -e
# Source global variables
if [ -f ".vars" ]; then
. ./.vars
fi
# see: https://stackoverflow.com/questions/3572030/bash-script-absolute-path-with-os-x#comment64141768_3572030
function abspath() {
cd "$(dirname -- "$1")"
pwd -P
}
DOTFILES=$(abspath "$(dirname -- "$0")")
DRY_RUN=false
function templatize() {
local template
template="$(abspath "${BASH_SOURCE[1]}")/$1"
eval "cat <<EOF
$(<"$template")
EOF
" 2>/dev/null
}
function link_file() {
local from
from=$(abspath "${BASH_SOURCE[1]}")/$1
local to=$2
printf "Linking '%s' to '%s'\n" "$from" "$to"
if [ ! -d "$(dirname -- "$to")" ]; then
mkdir -p "$(dirname -- "$to")"
fi
if ! $DRY_RUN; then
ln -sf "$from" "$to"
fi
}
function insert_line() {
local query=$1
local line=$2
local file=$3
printf "Inserting '%s' into '%s'\n" "$line" "$file"
if ! $DRY_RUN; then
grep -q "$query" "$file" || echo "$line" >>"$file"
fi
}
function command_exist() {
command -v "$1" >/dev/null 2>&1
}
function require() {
local cmd=$1
command_exist "$cmd" || (printf "Command '%s' not found\n" "$cmd" && false)
}
function _install() {
if ! $DRY_RUN; then
echo "Installing/updating dotfiles..."
else
echo "Dry run, not installing/updating dotfiles..."
fi
echo
for src in "$DOTFILES"/*/; do
local src=${src%*/}
local install="$src/install.sh"
if [ -f "$install" ]; then
echo "===== Installing" "$src" "dotfiles..."
# shellcheck source=/dev/null
. "$install"
echo
fi
case "$OSTYPE" in
darwin*)
local install="$src/install_darwin.sh"
if [ -f "$install" ]; then
echo "===== Installing" "$src" "dotfiles for darwin..."
# shellcheck source=/dev/null
. "$install"
echo
fi
;;
linux*)
local install="$src/install_linux.sh"
if [ -f "$install" ]; then
echo "===== Installing" "$src" "dotfiles for linux..."
# shellcheck source=/dev/null
. "$install"
echo
fi
;;
esac
done
echo "Done installing/updating dotfiles"
echo
}
function _usage() {
echo "Usage: $0 [options] [command]"
echo
echo "Commands:"
echo " install Install dotfiles"
echo " help Show this help message and exit"
if [ -d "$DOTFILES/scripts" ]; then
for s in "$DOTFILES"/scripts/*.sh; do
local s=${s%*/}
local s=${s##*/}
local s=${s%.sh}
echo " $s"
done
fi
echo
echo "Options:"
echo " -h Show this help message and exit"
echo " -d Dry run"
}
function _main() {
while getopts ":hd" opt; do
case $opt in
h)
_usage
exit 0
;;
d)
DRY_RUN=true
;;
\?)
echo "Invalid option: -$OPTARG" >&2
_usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
local cmd=$1
case $cmd in
install)
_install
;;
help)
_usage
;;
*)
if [ -n "$cmd" ]; then
if [ -f "$DOTFILES/scripts/$cmd.sh" ]; then
# shellcheck source=/dev/null
. "$DOTFILES/scripts/$cmd.sh"
exit 0
fi
echo "Invalid command: $cmd" >&2
fi
_usage
exit 1
;;
esac
}
_main "$@"