-
Notifications
You must be signed in to change notification settings - Fork 184
/
install.sh
executable file
·136 lines (118 loc) · 4.27 KB
/
install.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
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
#!/usr/bin/env bash
blue='\e[1;34m'
red='\e[1;31m'
white='\e[0;37m'
dotfiles_repo_dir=$(pwd)
backup_dir="$HOME/.dotfiles.orig"
dotfiles_home_dir=(.zsh .aliases .bash_profile .bashrc .dircolors .editorconfig .exports
.functions .gemrc .ripgreprc .wgetrc .Xresources .zshrc)
dotfiles_xdg_config_dir=(.alacritty .dunst .htop .i3 .i3blocks .picom .rofi .tmux)
# Print usage message.
usage() {
local program_name
program_name=${0##*/}
cat <<EOF
Usage: $program_name [-option]
Options:
--help Print this message
-i Install all config
-r Restore old config
EOF
}
install_dotfiles() {
# Backup config.
if ! [ -f "$backup_dir/check-backup.txt" ]; then
mkdir -p "$backup_dir/.config"
cd "$backup_dir" || exit
touch check-backup.txt
# Backup to ~/.dotfiles.orig
for dots_home in "${dotfiles_home_dir[@]}"
do
env cp -rf "$HOME/${dots_home}" "$backup_dir" &> /dev/null
done
# Backup some folder in ~/.config to ~/.dotfiles.orig/.config
for dots_xdg_conf in "${dotfiles_xdg_config_dir[@]//./}"
do
env cp -rf "$HOME/.config/${dots_xdg_conf}" "$backup_dir/.config" &> /dev/null
done
# Backup again with Git.
if [ -x "$(command -v git)" ]; then
git init &> /dev/null
git add -u &> /dev/null
git add . &> /dev/null
git commit -m "Backup original config on $(date '+%Y-%m-%d %H:%M')" &> /dev/null
fi
# Output.
echo -e "${blue}Your config is backed up in ${backup_dir}\n" >&2
echo -e "${red}Please do not delete check-backup.txt in .dotfiles.orig folder.${white}" >&2
echo -e "It's used to backup and restore your old config.\n" >&2
fi
# Install config.
for dots_home in "${dotfiles_home_dir[@]}"
do
env rm -rf "$HOME/${dots_home}"
env ln -fs "$dotfiles_repo_dir/${dots_home}" "$HOME/"
done
# Install config to ~/.config.
mkdir -p "$HOME/.config"
for dots_xdg_conf in "${dotfiles_xdg_config_dir[@]}"
do
env rm -rf "$HOME/.config/${dots_xdg_conf[*]//./}"
env ln -fs "$dotfiles_repo_dir/${dots_xdg_conf}" "$HOME/.config/${dots_xdg_conf[*]//./}"
done
echo -e "${blue}New dotfiles is installed!\n${white}" >&2
echo "There may be some errors when Terminal is restarted." >&2
echo "Please read carefully the error messages and make sure all packages are installed. See more info in README.md." >&2
echo "Note that the author of this dotfiles uses dev branch in some packages." >&2
echo -e "If you want to restore your old config, you can use ${red}./install.sh -r${white} command." >&2
}
uninstall_dotfiles() {
if [ -f "$backup_dir/check-backup.txt" ]; then
for dots_home in "${dotfiles_home_dir[@]}"
do
env rm -rf "$HOME/${dots_home}"
env cp -rf "$backup_dir/${dots_home}" "$HOME/" &> /dev/null
env rm -rf "$backup_dir/${dots_home}"
done
for dots_xdg_conf in "${dotfiles_xdg_config_dir[@]//./}"
do
env rm -rf "$HOME/.config/${dots_xdg_conf}"
env cp -rf "$backup_dir/.config/${dots_xdg_conf}" "$HOME/.config" &> /dev/null
env rm -rf "$backup_dir/.config/${dots_xdg_conf}"
done
# Save old config in backup directory with Git.
if [ -x "$(command -v git)" ]; then
cd "$backup_dir" || exit
git add -u &> /dev/null
git add . &> /dev/null
git commit -m "Restore original config on $(date '+%Y-%m-%d %H:%M')" &> /dev/null
fi
fi
if ! [ -f "$backup_dir/check-backup.txt" ]; then
echo -e "${red}You have not installed this dotfiles yet.${white}" >&2
exit 1
else
echo -e "${blue}Your old config has been restored!\n${white}" >&2
echo "Thanks for using my dotfiles." >&2
echo "Enjoy your next journey!" >&2
fi
env rm -rf "$backup_dir/check-backup.txt"
}
main() {
case "$1" in
''|-h|--help)
usage
exit 0
;;
-i)
install_dotfiles
;;
-r)
uninstall_dotfiles
;;
*)
echo "Command not found" >&2
exit 1
esac
}
main "$@"