-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup
executable file
·114 lines (90 loc) · 3.07 KB
/
setup
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
#!/usr/bin/env bash -eu
COLOR_OFF='\033[0m'
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
pushd `dirname $0` > /dev/null
SCRIPT_PATH=`pwd`
popd > /dev/null
pushd $HOME > /dev/null
backup_existing_file() {
if [ ! -e "$1.bak" ] ; then
mv $1 $1.bak
echo -e "${CYAN}A backup [$1.bak] is created.${COLOR_OFF}"
return 0
fi
i=1
while [ -e "$1.bak.$i" ] ; do
let i=$i+1
done
mv $1 $1.bak.$i
echo -e "${CYAN}A backup [$1.bak.$i] is created.${COLOR_OFF}"
return 0
}
symlink_existence_message() {
echo
echo -e "${YELLOW}A symbolic link [$1] (points to $(readlink "$1")) already exists.${COLOR_OFF}"
}
symlink_overwrite_confirmation() {
read -p "Are you really sure you want to overwrite it? (Y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] ; then
rm "$1"
else
echo
echo -e "${RED}Setup is aborted.${COLOR_OFF}"
exit 1
fi
}
link_items=(.vim .vimrc)
for item in "${link_items[@]}"; do
if [ "$(readlink "$HOME/$item")" ]; then
symlink_existence_message "$HOME/$item"
symlink_overwrite_confirmation "$HOME/$item"
fi
if [ -e "$HOME/$item" ] ; then
backup_existing_file "$HOME/$item"
fi
ln -s "$SCRIPT_PATH/$item" "$HOME/$item"
echo -e "${GREEN}A symbolic link [$HOME/$item] is created.${COLOR_OFF}"
done
if [ -e "$HOME/.vimrc.preset" ] ; then
echo
if cmp -s "$SCRIPT_PATH/.vimrc.preset.sample" "$HOME/.vimrc.preset" ; then
echo -e "${CYAN}A user's local preset file [$HOME/.vimrc.preset] has not been modified from the default.${COLOR_OFF}"
else
echo -e "${YELLOW}A user's local preset file [$HOME/.vimrc.preset] has been modified.${COLOR_OFF}"
echo -e "Please review the differences between two files by the following command and manually apply updates:${COLOR_OFF}"
echo "diff $SCRIPT_PATH/.vimrc.preset.sample $HOME/.vimrc.preset"
fi
else
cp "$SCRIPT_PATH/.vimrc.preset.sample" "$HOME/.vimrc.preset"
echo -e "${GREEN}A preset file [$HOME/.vimrc.preset] is copied.${COLOR_OFF}"
fi
if hash nvim 2>/dev/null; then
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:=$HOME/.config}
if [ ! -d "$XDG_CONFIG_HOME" ] ; then
mkdir -p $XDG_CONFIG_HOME
echo -e "${GREEN}[$XDG_CONFIG_HOME] is created.${COLOR_OFF}"
fi
if [ "$(readlink "$XDG_CONFIG_HOME/nvim")" ]; then
symlink_existence_message "$XDG_CONFIG_HOME/nvim"
symlink_overwrite_confirmation "$XDG_CONFIG_HOME/nvim"
fi
if [ -e "$XDG_CONFIG_HOME/nvim" ] ; then
backup_existing_file "$XDG_CONFIG_HOME/nvim"
fi
ln -s "$SCRIPT_PATH/.vim" "$XDG_CONFIG_HOME/nvim"
echo -e "${GREEN}A symbolic link [$XDG_CONFIG_HOME/nvim] is created.${COLOR_OFF}"
if [ "$(readlink "$XDG_CONFIG_HOME/nvim/init.vim")" ]; then
symlink_existence_message "$XDG_CONFIG_HOME/nvim/init.vim"
symlink_overwrite_confirmation "$XDG_CONFIG_HOME/nvim/init.vim"
fi
if [ -e "$XDG_CONFIG_HOME/nvim/init.vim" ] ; then
backup_existing_file "$XDG_CONFIG_HOME/nvim/init.vim"
fi
ln -s "$SCRIPT_PATH/.vimrc" "$XDG_CONFIG_HOME/nvim/init.vim"
echo -e "${GREEN}A symbolic link [$XDG_CONFIG_HOME/nvim/init.vim] is created.${COLOR_OFF}"
fi
popd > /dev/null