-
Notifications
You must be signed in to change notification settings - Fork 4
/
apply
executable file
·225 lines (208 loc) · 5.88 KB
/
apply
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
#!/bin/bash
set -euo pipefail
usage() {
cat <<EOF
Usage: ${0} [command] [flags] profile...
Flags:
--dry-run Show what will happen without making any changes.
--skip-pkgs Do not install packages from aptfile/brewfile/etc (implies --skip-custom-pkgs)
--skip-custom-pkgs Do not install profiles/*/pkgs/.
--skip-root Do not modify system files.
EOF
}
DRY_RUN=
SKIP_PKGS=
SKIP_CUSTOMPKGS=
SKIP_ROOT=
PROFILES=()
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run)
DRY_RUN=1
;;
--skip-pkgs)
SKIP_PKGS=1
SKIP_CUSTOMPKGS=1
;;
--skip-custompkgs)
SKIP_CUSTOMPKGS=1
;;
--skip-root)
SKIP_ROOT=1
;;
-h|--help)
usage
exit 0
;;
-*=)
echo "Error: unknown flag $1" >&2
exit 1
;;
default|dell-xps-15-9550|macbook-pro|pc|ubuntu-desktop|ubuntu-server)
PROFILES+=($1)
;;
*)
echo "Error: unknown profile $1" >&2
usage
exit 1
;;
esac
shift
done
if [[ "${#PROFILES[@]}" == 0 ]]; then
echo $"Error: At least one profile name required. Available profiles:
$(cd profiles; ls)
" >&2
usage
exit 1
fi
OS=$(uname -s | tr '[:upper:]' '[:lower:]') # darwin or linux
if [ -f /etc/lsb-release ]; then
. /etc/lsb-release # DISTRIB_ID=Ubuntu, DISTRIB_CODENAME=bionic, ...
fi
function symlink() {
if [[ "$1" == "--root" ]]; then
local run=sudo
local src=$2
local dst=$3
else
local run=
local src=$1
local dst=$2
fi
if [ -e "$dst" ]; then
if [[ -L "$dst" && "$(realpath $dst)" == "$src" ]]; then
return
fi
echo "${dst/#$HOME\//\~\/} already exists (and is not a symlink or points to a different file)" >&2
return 1
fi
if [[ "$DRY_RUN" == "1" ]]; then
echo "Skipped ${dst/#$HOME\//\~\/} linking (--dry-run)"
return
fi
echo "Linking ${dst/#$HOME\//\~\/}" >&2
$run mkdir -p $(dirname $dst)
$run ln -sf "$src" "$dst"
}
function copy() {
if [[ "$1" == "--root" ]]; then
local run=sudo
local src=$2
local dst=$3
else
local run=
local src=$1
local dst=$2
fi
if [ -e "$dst" ] && diff --color -u "$dst" "$src"; then # cmp --silent "$src" "$dst"
return
fi
if [[ "$DRY_RUN" == "1" ]]; then
echo "Skipped ${dst/#$HOME\//\~\/} copying (--dry-run)"
return
fi
echo "Copying ${dst/#$HOME\//\~\/}" >&2
$run mkdir -p $(dirname $dst)
$run cp -i "$src" "$dst"
}
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
function install_pkgs_aptfile() {
if [[ "$SKIP_PKGS" == "1" || ! -f $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/aptfile ]]; then
return
fi
local aptfile_opt=
if [[ "$DRY_RUN" == "1" ]]; then
aptfile_opt=--dry-run
fi
echo "> $DOTFILES_PROFILE_DIR/aptfile" >&2
bash $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/aptfile $aptfile_opt
}
function install_pkgs_brewfile() {
if [[ "$SKIP_PKGS" == "1" || ! -f $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/brewfile ]]; then
return
fi
if ! command -v brew >/dev/null; then
if [[ "$DRY_RUN" == "1" ]]; then
echo "dry-run: install brew" >&2
else
echo "> install brew" >&2
curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh | bash
fi
fi
if [[ "$DRY_RUN" == "1" ]]; then
echo "dry-run not supported: $DOTFILES_PROFILE_DIR/brewfile" >&2
else
echo "> $DOTFILES_PROFILE_DIR/brewfile" >&2
brew bundle install --file=$DOTFILES_DIR/$DOTFILES_PROFILE_DIR/brewfile
fi
}
function install_pkgs() {
if [[ "$SKIP_PKGS" == "1" ]]; then
return
fi
if [[ "${DISTRIB_ID:-}" == "Ubuntu" ]]; then
install_pkgs_aptfile
elif [[ "$OS" = "darwin" ]]; then
install_pkgs_brewfile
else
echo "Error: unsupported OS" >&2
exit 1
fi
}
function install_custom_pkgs() (
if [[ "$SKIP_CUSTOMPKGS" == "1" || ! -d $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/pkgs ]]; then
return
fi
echo "> $DOTFILES_PROFILE_DIR/pkgs/*" >&2
export PATH=$HOME/.bin:$HOME/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
local pkg_dir=$DOTFILES_DIR/$DOTFILES_PROFILE_DIR/pkgs
for f in $(ls $pkg_dir); do
if [[ "$f" == *.ondemand || "$f" == *.disabled ]]; then
echo "> skipped: $DOTFILES_PROFILE_DIR/pkgs/$f" >&2
continue
fi
cmd=$(eval "echo $(cat $pkg_dir/$f | grep '#@pkg.file=' | awk -F= '{print $2}')")
f_without_ext=${f%%.*}
cmd=${cmd:-$f_without_ext}
if [ ! -x $pkg_dir/$f ]; then
chmod +x $pkg_dir/$f
fi
if ! command -v $cmd >/dev/null && ! [ -e $cmd ]; then
if [[ "$DRY_RUN" == "1" ]]; then
echo "dry-run: $DOTFILES_PROFILE_DIR/pkgs/$f" >&2
continue
fi
echo "> $DOTFILES_PROFILE_DIR/pkgs/$f" >&2
$pkg_dir/$f
fi
done
)
function configure_root() {
if [[ "$SKIP_ROOT" == "1" || ! -f $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/root.apply.source ]]; then
return
fi
echo "> $DOTFILES_PROFILE_DIR/root.apply.source" >&2
DIR=$DOTFILES_DIR/$DOTFILES_PROFILE_DIR
source $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/root.apply.source
unset DIR
}
function configure_home() {
if [ ! -f $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/home.apply.source ]; then
return
fi
echo "> $DOTFILES_PROFILE_DIR/home.apply.source" >&2
DIR=$DOTFILES_DIR/$DOTFILES_PROFILE_DIR
source $DOTFILES_DIR/$DOTFILES_PROFILE_DIR/home.apply.source
unset DIR
}
for profile in ${PROFILES[@]}; do
DOTFILES_PROFILE=$profile
DOTFILES_PROFILE_DIR=profiles/$profile
echo "> profile: $profile" >&2
install_pkgs
install_custom_pkgs
configure_root
configure_home
done
echo OK >&2