-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·294 lines (260 loc) · 7.58 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
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
#!/bin/bash
REPO_PREFIX='https://raw.githubusercontent.com/masmu/my-dot/master'
TMP_PREFIX='/tmp'
ZSH_PLUGIN_REPOS=(
https://github.com/zsh-users/zsh-autosuggestions
https://github.com/zsh-users/zsh-syntax-highlighting
https://github.com/zdharma/history-search-multi-word
)
CPU_ARCHITECTURE="$(uname -m)"
function is_url() {
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
if [[ $1 =~ $regex ]]
then
echo "true"
else
echo ""
fi
}
function update_git_repo() {
if [ -d "$1" ]; then
echo "Updating $1:"
cd "$1"
git pull
else
echo "Repository '$1' not found!"
fi
}
function install_git_repo() {
if [ -d "$2" ]; then
update_git_repo "$2"
else
echo "Installing $1 ..."
git clone "$1" "$2"
fi
}
function install_symlink() {
[[ -L "$2" && -d "$1" ]] || {
echo "Creating symlink $1 ..."
ln -s "$1" "$2"
}
}
function download_file() {
if [[ ! -f "$2" || $FORCE || "$3" ]]; then
echo "Downloading file $2 ..."
curl -o "$2" "$1"
fi
}
function text_append() {
if [ -f "$1" ]; then
if [ "$(cat "$1" | grep -F "$2" | wc -l)" -eq 0 ]; then
echo "patching $1 ..."
echo "$3" >> "$1"
fi
else
echo "text_append: file '$1' not found!"
fi
}
function text_replace() {
if [ -f "$1" ]; then
echo "patching $1 ..."
sed -i -e "s/$2/$3/g" "$1"
else
echo "text_replace: file '$1' not found!"
fi
}
function text_remove() {
if [ -f "$1" ]; then
sed -i "/$2/d" "$1"
else
echo "text_remove: file '$1' not found!"
fi
}
function text_patch() {
if [[ -f "$1" ]]; then
if [[ $(is_url "$2") ]]; then
TMP_FILE="$TMP_PREFIX/$(basename $2)"
download_file "$2" "$TMP_FILE" true
SOURCE="$TMP_FILE"
else
SOURCE="$2"
fi
echo "patching $1 ..."
comm -23 <(sort "$SOURCE") <(sort "$1") >> "$1"
else
echo "text_patch: file '$1' not found!"
fi
}
function backup_if_exists() {
if [[ ! -a "$1.nobackup" && ! -a "$1.original" ]]; then
if [[ -a "$1" ]]; then
echo "creating backup of $1 ..."
cp -R "$1" "$1.original"
else
touch "$1.nobackup"
fi
fi
}
function restore_backup() {
if [[ -a "$1.original" ]]; then
echo "restoring backup of $1 ..."
rm -rf "$1"
mv "$1.original" "$1"
elif [[ -a "$1.nobackup" ]]; then
echo "removing $1 ..."
rm -rf "$1"
rm "$1.nobackup"
fi
}
function install_pkgs() {
sudo apt-get -y install curl zsh git byobu sed xclip || {
echo "Error during installing the dependencies!"
exit 1
}
}
function setup_bash() {
backup_if_exists ~/.bashrc
[[ -f ~/.bashrc ]] || touch ~/.bashrc
text_append ~/.bashrc '#DISABLE_FLOW_CONTROL' \
'stty -ixon #DISABLE_FLOW_CONTROL'
stty -ixon
}
function setup_local_bin() {
mkdir -p ~/.local/bin
}
function setup_micro() {
setup_local_bin
mkdir -p ~/.config/micro
[[ -f ~/.config/micro/settings.json ]] && rm ~/.config/micro/settings.json
download_file "$REPO_PREFIX/micro/settings.json" ~/.config/micro/settings.json
[[ -f ~/.config/micro/bindings.json ]] && rm ~/.config/micro/bindings.json
download_file "$REPO_PREFIX/micro/bindings.json" ~/.config/micro/bindings.json
mkdir -p ~/.config/micro/syntax
[[ -f ~/.config/micro/syntax/python.yaml ]] && rm ~/.config/micro/syntax/python.yaml
download_file "$REPO_PREFIX/micro/syntax/python.yaml" ~/.config/micro/syntax/python.yaml
mkdir -p ~/.config/micro/colorschemes
[[ -f ~/.config/micro/colorschemes/base16-tomorrow-night.micro ]] && rm ~/.config/micro/colorschemes/base16-tomorrow-night.micro
download_file "$REPO_PREFIX/micro/colorschemes/base16-tomorrow-night.micro" ~/.config/micro/colorschemes/base16-tomorrow-night.micro
TMP_DIR="/tmp/micro/"
EXTRACT_DIR="$TMP_DIR/extract"
case "$CPU_ARCHITECTURE" in
i?86) ARCH="linux32";;
x86_64) ARCH="linux64" ;;
armv?l) ARCH="linux-arm" ;;
aarch64) ARCH="linux-arm64" ;;
esac
VERSION="2.0.8"
DOWNLOAD_URL="https://github.com/zyedidia/micro/releases/download/v$VERSION/micro-$VERSION-$ARCH.tar.gz"
mkdir -p "$EXTRACT_DIR"
curl -o "$TMP_DIR/micro.tar.gz" -L $DOWNLOAD_URL
tar -xvzf "$TMP_DIR/micro.tar.gz" -C "$EXTRACT_DIR"
find "$EXTRACT_DIR" -iname "micro" -exec cp "{}" ~/.local/bin \;
~/.local/bin/micro -plugin install filemanager
}
function setup_byobu() {
backup_if_exists ~/.byobu
[[ -d ~/.byobu ]] || mkdir ~/.byobu
[[ -f ~/.byobu/tmux.conf ]] && rm ~/.byobu/.tmux.conf
install_symlink ~/.tmux.conf ~/.byobu/.tmux.conf
[[ -f ~/.byobu/keybindings.tmux ]] && rm ~/.byobu/keybindings.tmux
download_file "$REPO_PREFIX/byobu/keybindings.tmux" ~/.byobu/keybindings.tmux
byobu-disable-prompt
}
function setup_tmux() {
backup_if_exists ~/.tmux.conf
[[ -f ~/.tmux.conf ]] || touch ~/.tmux.conf
text_patch ~/.tmux.conf "$REPO_PREFIX/tmux/.tmux.conf"
}
function setup_zsh() {
backup_if_exists ~/.oh-my-zsh
backup_if_exists ~/.zshrc
backup_if_exists ~/.zshenv
install_git_repo git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
text_replace ~/.zshrc \
'ZSH_THEME="robbyrussell"' \
'ZSH_THEME="agnoster"'
for GIT_URL in ${ZSH_PLUGIN_REPOS[*]}
do
GIT_FOLDER="$(basename $GIT_URL)"
if [ ! -d ~/.zsh/$GIT_FOLDER ]; then
install_git_repo $GIT_URL ~/.zsh/$GIT_FOLDER
fi
LINE="source ~/.zsh/$GIT_FOLDER/$GIT_FOLDER.plugin.zsh"
text_append ~/.zshrc "$LINE" "$LINE"
done
text_append ~/.zshrc '#SOURCE_PROFILE' \
"[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile' #SOURCE_PROFILE"
text_append ~/.zshrc '#KEY-HOME-FIX' \
'bindkey "^[[1~" beginning-of-line #KEY-HOME-FIX'
text_append ~/.zshrc '#KEY-END-FIX' \
'bindkey "^[[4~" end-of-line #KEY-END-FIX'
text_append ~/.zshrc '#ALIAS-COLORCAT' \
'alias ccat="pygmentize -g" #ALIAS-COLORCAT'
[[ -f ~/.zshenv ]] || touch ~/.zshenv
text_append ~/.zshenv '#DISABLE_FLOW_CONTROL' \
'stty -ixon #DISABLE_FLOW_CONTROL'
}
function install_all() {
if [ ! $SKIP_PACKAGES ]; then
install_pkgs
fi
setup_bash
setup_byobu
setup_zsh
setup_tmux
setup_micro
echo "Installation done!"
}
function remove() {
restore_backup ~/.bashrc
restore_backup ~/.byobu
restore_backup ~/.zshrc
restore_backup ~/.zshenv
restore_backup ~/.tmux.conf
restore_backup ~/.oh-my-zsh
echo "Remove done!"
}
function clean() {
remove
[[ -d ~/.oh-my-zsh ]] && rm -Rf ~/.oh-my-zsh
[[ -d ~/.byobu ]] && rm -Rf ~/.byobu
[[ -d ~/.zsh ]] && rm -Rf ~/.zsh
[[ -f ~/.zshrc ]] && rm ~/.zshrc
[[ -f ~/.zshenv ]] && rm ~/.zshenv
[[ -f ~/.tmux.conf ]] && rm ~/.tmux.conf
[[ -d ~/.oh-my-zsh ]] && rm -Rf ~/.oh-my-zsh
[[ -f ~/.local/bin/micro ]] && rm ~/.local/bin/micro
echo "Clean done!"
}
SKIP_PACKAGES=""
FORCE=""
while [ "$#" -gt "0" ]; do
case $1 in
--clean)
clean
exit 0
;;
--remove)
remove
exit 0
;;
--editor)
setup_micro
exit 0
;;
--skip-packages)
SKIP_PACKAGES="1"
shift
;;
--force)
FORCE="1"
shift
;;
*)
echo "Unknown option '$1'!"
exit 1
;;
esac
done
install_all