-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·67 lines (54 loc) · 1.51 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
#!/bin/sh
set -e
# This setup.sh script requires:
# * git
# * sh
ROOT=$(cd "$(dirname "$0")"; git rev-parse --show-toplevel)
# To support dotfiles referencing other scripts and files
# without needing to copy them, we need a standard path
# they can reference. Let's standarize on "~/.dotfiles"
if [ "$ROOT" != "$HOME/.dotfiles" ]; then
echo "ERROR: you must clone the repo as ~/.dotfiles/" >&2
exit 1
fi
# Prepare backup directory
BACKUPS="$ROOT/backups/$(date +%s)/"
mkdir -p "$BACKUPS"
cd "$ROOT"
for module in configs/* configs-private/*; do
# not a module - skip
if [ ! -d "$module" ]; then
continue;
fi
# incomplete module - skip
if [ ! -f "$module/dotfiles" ]; then
continue;
fi
# run build script if provided
if [ -x "$module/build.sh" ]; then
echo "Building dotfiles for $module"
(cd "$module" && ./build.sh)
fi
echo "Installing dotfiles for $module"
# install dotfiles - back up existing ones
while read -r dotfile; do
MODULE="$ROOT/$module"
ORIG="$MODULE/$(echo "$dotfile" | cut -f1 -d:)"
DEST="$HOME/$(echo "$dotfile" | cut -f2 -d:)"
# if destination is unset, use default
if [ "$DEST" = "$HOME/" ]; then
DEST="$HOME/$ORIG"
fi
# if dotfile already exists, back it up
if [ -f "$DEST" ] || [ -L "$DEST" ] || [ -d "$DEST" ]; then
mv "$DEST" "$BACKUPS/"
fi
# if destination directory doesn't exist, create it
DESTDIR=$(dirname "$DEST")
if [ ! -d "$DESTDIR/" ]; then
mkdir -vp "$DESTDIR"
fi
# link the dotfile
ln -vs "$ORIG" "$DEST"
done < "$module/dotfiles"
done