-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·239 lines (196 loc) · 5.1 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
#!/bin/bash
#
# CN_Experience Installer
#
# Description:
# Bash script installer to set up the CN_Experience for users. It assumes
# that you have zsh, tmux, and vim installed.
#
# Author:
# Clara Nguyen (@iDestyKK)
#
printf \
"%s\n%s\n\n%s\n\n" \
"CN_Experience" \
"Version 0.2.1 (Last Updated: 2019-01-31)" \
"Installer Bash Script to make your life easier."
# -----------------------------------------------------------------------------
# Configure Script {{{1
# -----------------------------------------------------------------------------
# Paths
HOME_DIR=~
TARGET="${HOME_DIR}/.cn_experience"
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
# Colours
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
NORMAL=$(tput sgr 0)
function log_print {
printf "%-56s" "$1"
if [ $# -gt 1 ]; then
printf "$2"
fi
}
function log_status {
if [ $1 -eq 0 ]; then
# Success
printf "[ ${GREEN}OK${NORMAL} ]\n"
else
# Failure
printf "[${RED}FAILED${NORMAL}]\n"
fi
}
function append_line {
if [ ! -e "$1" ]; then
touch "$1"
fi
if ! cat "$1" | grep "$2" > /dev/null 2> /dev/null; then
echo "$2" >> "$1"
fi
}
# -----------------------------------------------------------------------------
# Copy over files {{{1
# -----------------------------------------------------------------------------
log_print "Checking..." "\n"
# Check if the directory exists already
log_print " DIR: ~/.cn_experience... "
if [ ! -e "${TARGET}" ]; then
printf "[ ${YELLOW}MAKE${NORMAL} ]\n"
log_print " Creating ~/.cn_experience..."
MSG=$(mkdir "${TARGET}" 2>&1)
RES=$?
if [ $RES -eq 0 ]; then
log_status 0
else
log_status 1
log_print " Failed to create \"${TARGET}\"." "\n"
log_print " ${MSG}" "\n"
exit 1
fi
else
log_status 0
fi
# Check if .zshrc exists
log_print " DIR: ~/.zshrc... "
if [ ! -e "${HOME_DIR}/.zshrc" ]; then
printf "[ ${YELLOW}MAKE${NORMAL} ]\n"
log_print " Creating ~/.cn_experience..."
MSG=$(touch "${HOME_DIR}/.zshrc" 2>&1)
RES=$?
if [ $RES -eq 0 ]; then
log_status 0
else
log_status 1
log_print " Failed to create \"${HOME_DIR}/.zshrc\"." "\n"
log_print " ${MSG}" "\n"
exit 1
fi
else
log_status 0
fi
# Check if .vim exists
log_print " DIR: ~/.vim..."
if [ ! -e "${HOME_DIR}/.vim" ]; then
printf "[ ${YELLOW}MAKE${NORMAL} ]\n"
log_print " Creating ~/.vim..."
MSG=$(mkdir "${HOME_DIR}/.vim" 2>&1)
RES=$?
if [ $RES -eq 0 ]; then
log_status 0
else
log_status 1
log_print " Failed to create \"${HOME_DIR}/.vim\"." "\n"
log_print " ${MSG}" "\n"
exit 1
fi
else
log_status 0
fi
# Check if tar is a valid command
log_print " CMD: tar"
if ! command -v tar > /dev/null; then
log_status 1
log_print " tar is not installed." "\n"
exit 1
else
log_status 0
fi
printf "\n"
# Begin the copying
log_print "Copying files..." "\n"
shopt -s dotglob
# Configurations
log_print " Default configurations..." "\n"
for F in "${SCRIPTPATH}/default/"*; do
# log_print " $(basename ${F})" "\n"
cp -r "$F" "${TARGET}"
done
# Configuration directory
if [ -e "${TARGET}/configs" ]; then
# Copy over older configs to a new directory
log_print " Moving old configs to \"configs.old\""
MSG=$(mv "${TARGET}/configs" "${TARGET}/configs.old" 2>&1)
RES=$?
if [ $RES -eq 0 ]; then
log_status 0
else
log_status 1
log_print " Failed to move configs over." "\n"
log_print " ${MSG}" "\n"
exit 1
fi
fi
# Create new directory
mkdir "${TARGET}/configs" 2> /dev/null > /dev/null
# ZSH Configs
log_print " zsh..."
cp -r "${SCRIPTPATH}/configs/zsh" "${TARGET}/configs"
log_status 0
# VIM Configs
log_print " vim..."
cp -r "${SCRIPTPATH}/configs/vim" "${TARGET}/configs"
log_status 0
# TMUX Configs
log_print " tmux..."
cp -r "${SCRIPTPATH}/configs/tmux" "${TARGET}/configs"
log_status 0
# Youbi
log_print " Youbi..."
cp -r "${SCRIPTPATH}/youbi" "${TARGET}"
log_status 0
# Scripts
log_print " Scripts..."
cp -r "${SCRIPTPATH}/scr" "${TARGET}"
log_status 0
# Vim Plugins
log_print " VIM Plugins (vim.tar.xz -> ~/.vim)..."
cd "${HOME_DIR}/.vim"
# I'm lazy. Extract from the tar
MSG=$(tar -xJf "${SCRIPTPATH}/pkg/vim.tar.xz" 2>&1)
RES=$?
if [ $RES -ne 0 ]; then
log_status 1
log_print " Failed to extract \"${SCRIPTPATH}/pkg/vim.tar.xz\"." "\n"
log_print " ${MSG}" "\n"
exit 1
else
log_status 0
fi
# -----------------------------------------------------------------------------
# Inject data into existing files {{{1
# -----------------------------------------------------------------------------
printf "\n"
log_print "Patching..." "\n"
# Assume ~/.zshrc exists. Append to .zshrc
log_print " .zshrc..."
append_line "${HOME_DIR}/.zshrc" "source $HOME/.cn_experience/configs/zsh/.zsh.include"
log_status 0
# Append to .vimrc
log_print " .vimrc..."
append_line "${HOME_DIR}/.vimrc" "source ~/.cn_experience/configs/vim/.vimrc"
log_status 0
# Append to .tmux.conf
log_print " .tmux.conf..."
append_line "${HOME_DIR}/.tmux.conf" "source ~/.cn_experience/configs/tmux/.tmux.conf"
log_status 0