forked from reorx/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplement.sh
executable file
·299 lines (241 loc) · 5.76 KB
/
implement.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
#
# Author: reorx
#
# Usage
# commands:
# initialize
# do everything on a just installed computer, use with -f to
# reset all and cover existings.
# set ,arg
# configure ,arg
# Global variables
# Change this varible to choose the targets you want
ALL_DOTFILES=( zsh vim git python moc wget tmux )
IMPL_DOTFILES=( zsh vim git python wget tmux )
LINESHIFT=" "
INITED_FILE=".inited"
# Determine system
# Supported systems: mac, ubuntu
if [ "$(uname)" == "Darwin" ]; then
OS="mac"
else
# release_info=$(cat /etc/*-release)
# if $release_info match 'ubuntu'
OS="ubuntu"
# OS="unknown"
fi
# TODO colorful print
# Functions
function ln2home() {
SOURCE="$PWD/$1"
if [ "$2" ]; then
TARGET="$HOME/$2"
else
TARGET="$HOME/.$1"
fi
if [ -e "$TARGET" ]; then
if [ $FORCE_SET ]; then
echo "$TARGET exists, force override it"
BACKUP="$HOME/.$1.dotfilebak"
echo "Backup to $BACKUP"
# To ensure BACKUP is no exist, or may cause link chain recurive
if [ -e "$BACKUP" ]; then
rm -rf $BACKUP
fi
mv $TARGET $BACKUP
else
echo "Unlinked file $TARGET exists, no force, skip"
return 0
fi
elif [ -h "$TARGET" ]; then
echo "$TARGET is a broken link, rm it"
rm -f $TARGET
fi
echo "Create link $TARGET -> $SOURCE"
ln -s $SOURCE $TARGET
}
function installed() {
if type -P $1 &>/dev/null; then
echo 1
else
return "0"
fi
}
function install() {
if [ "$OS" == "mac" ]; then
if ! type "brew" > /dev/null; then
echo "You must have brew installed on your mac to \
be able to install $1 by command"
exit
fi
elif [ "$OS" == "ubuntu" ]; then
# release_info=$(cat /etc/*-release)
# if $release_info match 'ubuntu'
if ! type "apt-get" > /dev/null; then
echo "No apt-get? WTF!"
exit
fi
sudo apt-get install $1
else
echo "Sorry, does not support linux dist other than ubuntu yet"
fi
}
function install_if_not_exist() {
if [ ! $(installed $1) ]; then
install $1
fi
}
function impl_zsh() {
install_if_not_exist zsh
ln2home shell-colors
ln2home oh-my-zsh
ln2home oh-my-zsh-custom
ln2home zshrc
ln2home zshrc_fzf
ln2home zshrc_$OS .zshrc_os
ln2home z
}
function impl_vim() {
install_if_not_exist vim
install_if_not_exist ctags
ln2home vim
ln2home vimrc
}
function impl_git() {
ln2home gitconfig
ln2home gitignore_global
}
function impl_python() {
ln2home pythonrc.py
}
function impl_moc() {
install_if_not_exist moc
ln2home moc
}
function impl_wget() {
install_if_not_exist wget
ln2home wgetrc
}
function impl_tmux {
ln2home tmux.conf
}
function conf_zsh() {
local user="$(whoami)"
read -p "Do you want to set to this user $user(yourself) [yY]?" -n 1
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
elif [ $REPLY ]; then
echo
read -p "input username: " user
fi
sudo usermod -s /bin/zsh $user
}
function conf_vim() {
#(
#vim +BundleInstall +qall
#)
vim -c "execute \"BundleInstall\" | q | q"
}
function update_git() {
git pull
git submodule init
git submodule update
}
function show_help() {
cat <<EOF
Usage: implement.sh [-i | -s name | -c name | -h]
require option arguments
non-option arguments is not needed
Options:
-i : Initialize everything, for virgin system.
-u : Update all dotfiles implementations.
-s : Set dotfiles specially.
-c : Configure with <name>.
-h : Show help information.
EOF
}
function check_repos() {
if [ ! -e $INITED_FILE ]; then
echo "init repos first"
update_git
touch $INITED_FILE
# If this is done before command `update_git`,
# there will be no need to redo update,
# set a flag for later use.
FIRST_TIME="true"
fi
}
function set_dotfiles() {
for i in ${IMPL_DOTFILES[@]}; do
echo "Setting $i's dotfiles"
eval "impl_$i"
done
}
# Preparations
if [ ! $(installed git) ]; then
echo 'git must be installed !'
exit
fi
cd "$(dirname "$0")"
# Main
while getopts ":hTiuxs:c:" opt; do
case $opt in
h)
show_help
exit
;;
T)
echo "self test" >&2
exit
;;
i)
check_repos
echo "Initialize"
echo "Step 1. Set dotfiles"
set_dotfiles
read -p "Do you want to configure zsh to your default shell [yY]?" -n 1
if [[ $REPLY =~ ^[Yy]$ || ! $REPLY ]]; then
conf_zsh
fi
read -p "Do you want to configure vim and its bundles [yY]?" -n 1
if [[ $REPLY =~ ^[Yy]$ || ! $REPLY ]]; then
conf_vim
fi
echo "\nAll done!"
exit
;;
u)
FORCE_SET="true"
echo "Update dotfiles"
set_dotfiles
exit
;;
s)
echo "Set $OPTARG"
FORCE_SET="true"
eval "impl_$OPTARG"
exit
;;
c)
echo "Configure $OPTARG"
eval "conf_$OPTARG"
exit
;;
x)
echo "Delete all backup files"
rm -f $HOME/.*.dotfilebak
exit
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
esac
done
echo "Please input an option"
show_help