-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore_dotfiles.sh
executable file
·47 lines (39 loc) · 1.16 KB
/
restore_dotfiles.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
#!/bin/bash
# Substitute all the dotfiles in the system to the ones saved in .dotifiles
# This is useful for recreating my system configuration
if [ "$(whoami)" = "root" ]; then
USER_HOME="$(eval echo "~${SUDO_USER}")"
else
USER_HOME="$HOME"
fi
dotfiles_root="$USER_HOME/.dotfiles/root"
# include hidden files in globbing
shopt -s dotglob
explore() {
for element in "$1"/*; do
if [[ "$element" == "$dotfiles_root"/*.old ]]; then
echo "Skipping: $element"
elif [ -f "$element" ]; then
process "$element"
elif [ -d "$element" ]; then
explore "$element"
fi
done
}
process() {
FILE_IN_REPO="$1"
# remove the prefix $dotfiles_root
FILE_IN_SYSTEM="${1/#$dotfiles_root/}"
echo "Linking $FILE_IN_REPO to $FILE_IN_SYSTEM"
if [ "$FILE_IN_SYSTEM" == "/etc/fstab" ]; then
echo "Refusing to symlink /etc/fstab"
else
ln -sf "$FILE_IN_REPO" "$FILE_IN_SYSTEM"
fi
}
explore "$dotfiles_root"
# /etc/fstab does not work as a symlink
echo "Restoring /etc/fstab as a copy instead of symlink..."
rm "/etc/fstab"
cp "$USER_HOME/.dotfiles/fstab" "/etc/fstab"
shopt -u dotglob