-
Notifications
You must be signed in to change notification settings - Fork 12
/
restore_dotfiles.sh
executable file
·49 lines (43 loc) · 1.55 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
48
49
#!/usr/bin/env bash
BACKUP_DIR="${1:-"${HOME}/.dotfiles/backups/latest"}"
if [[ ! -d "${BACKUP_DIR}" ]]; then
echo "Backup directory \"${BACKUP_DIR}\" does not exist." >&2
exit 1
fi
echo "Restore dotfiles in backup directory \"${BACKUP_DIR}\"."
DOTFILES=(
.zshrc .dotfiles/.zshrc
.gemrc .dotfiles/.gemrc
.bash_profile .dotfiles/.bash_profile
.bashrc .dotfiles/.bashrc
.vimrc .dotfiles/.vimrc
.tmux.conf .dotfiles/.tmux.conf
.tmux.conf.local .dotfiles/.tmux.conf.local
.dotfiles/.tmux.conf.user
.gitconfig .dotfiles/.gitconfig
.gitignore_global .dotfiles/.gitignore_global
.condarc .dotfiles/.condarc
.gdbinit .dotfiles/.gdbinit
.Xresources .dotfiles/.Xresources
.dotfiles/utilities.sh
)
cd "${HOME}"
for file in "${DOTFILES[@]}"; do
if [[ -f "${BACKUP_DIR}/${file}" || -d "${BACKUP_DIR}/${file}" ]]; then
backup_prefix="$(dirname "${BACKUP_DIR}/${file}")"
prefix="$(dirname "${HOME}/${file}")"
file="$(basename "${file}")"
if diff -EB "${backup_prefix}/${file}" "${prefix}/${file}" &>/dev/null; then
echo "The file \"${file}\" in \"${backup_prefix}\" is same as the one in \"${prefix}\". Skip."
else
echo "Restore \"${file}\" from \"${backup_prefix}\" to \"${prefix}\"."
cp -rf "${backup_prefix}/${file}" "${prefix}/"
if [[ "${prefix}" == "${HOME}/.dotfiles" ]]; then
if diff -EB "${file}" ".dotfiles/${file}" &>/dev/null; then
echo "The file \"${file}\" in \"${HOME}\" is same as the one in \"${HOME}/.dotfiles\". Make symbolic link."
ln -sf ".dotfiles/${file}" .
fi
fi
fi
fi
done