-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·90 lines (76 loc) · 1.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#/bin/bash
set -e -u -o pipefail
cwd="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
dotfiles_dir="${cwd}/src"
dotfiles_old_dir="${cwd}/src.bak"
backup() {
echo "====== Backing up any existing dotfiles."
cd ${dotfiles_dir}
# For each dotfile in our folder, retrieve the currently running version from the system instead.
for dotfile in $(find . -type f); do
if [ -e ${HOME}/${dotfile} ]; then
mkdir -p $(dirname ${dotfiles_old_dir}/$dotfile)
cp -v "${HOME}/${dotfile}" "${dotfiles_old_dir}/${dotfile}"
fi
done
}
install() {
echo "====== Checking for junk left by previous runs of this tool."
if [[ -d ${dotfiles_old_dir} ]]; then
prompt=$(cat <<-EOF
Folder ${dotfiles_old_dir} exists. Your current dotfiles cannot be backed up without overwriting it.
By typing yes, you will delete the backup directory. By typing no, you will merge the backup directory with your current dotfiles.
Which one do you choose? [Y/n]
EOF
)
read -p "${prompt}" response
[[ -z "${response:+x}" ]] && response='Y'
case $response in
[yY][eE][sS]|[yY])
echo "deleting"
rm -r ${dotfiles_old_dir}
;;
esac
fi
mkdir -p ${dotfiles_old_dir}
backup
echo "====== The way is cleared, copy all new files"
rsync -avr "${dotfiles_dir}/" "${HOME}"
# Display warning
read -p "====== Your old dotfiles are located inside ${dotfiles_old_dir}. Would you like to keep them? [y/N] " response
[[ -z "${response:+x}" ]] && response='N'
case $response in
[nN][oO]|[nN])
echo "====== Deleting backup..."
rm -r ${dotfiles_old_dir}
;;
esac
if ! grep "bashrc.after" "${HOME}/.bashrc" >/dev/null; then
echo "appending .bashrc.after to .bashrc"
cat >> "${HOME}/.bashrc" <<-"EOF"
if [ -f ~/.bashrc.after ]; then
. ~/.bashrc.after
fi
EOF
fi
echo "====== Setup done."
}
usage() {
echo "Usage:"
echo "./setup.sh install"
echo "./setup.sh backup"
echo "./setup.sh help"
exit 1
}
[[ $# -eq 1 ]] || usage
case $1 in
install)
install
;;
backup)
backup
;;
*)
usage
;;
esac