forked from 0xShady/42_wizzard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path42-wizzard.sh
executable file
·516 lines (488 loc) · 15.7 KB
/
42-wizzard.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/bin/zsh
BOLD=$(tput bold)
UNDERLINE=$(tput smul)
ITALIC=$(tput sitm)
RESET="\033[0m"
RED="\e[1;31m"
GREEN="\e[1;32m"
YELLOW="\e[1;33m"
BLUE="\e[1;34m"
MAGENTA="\e[1;35m"
CYAN="\e[1;36m"
WIZZARD_VERSION="2.5.0"
ANIMATION_RATE=0.3
declare -a ACTIVE_LOADING
# loadig animation arrays
classic=('-' "\\" '|' '/')
cloning=( 'cloning ' 'cloning. ' 'cloning.. ' 'cloning...')
loading=('loading ' 'loading. ' 'loading.. ' 'loading...')
waiting=('waiting ' 'waiting. ' 'waiting.. ' 'waiting...')
installing=('installing ' 'installing. ' 'installing.. ' 'installing...')
building=('building ' 'building. ' 'building.. ' 'building...')
configuring=('configuring ' 'configuring. ' 'configuring.. ' 'configuring...')
cleaning=('cleaning ' 'cleaning. ' 'cleaning.. ' 'cleaning...')
metro=( '[ ]'
'[= ]'
'[== ]'
'[=== ]'
'[==== ]'
'[===== ]'
'[====== ]'
'[======= ]'
'[======== ]'
'[========= ]'
'[========== ]'
'[=========== ]'
'[============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============ ]'
'[ ============]'
'[ ===========]'
'[ ==========]'
'[ =========]'
'[ ========]'
'[ =======]'
'[ ======]'
'[ =====]'
'[ ====]'
'[ ===]'
'[ ==]'
'[ =]' )
function play_loading_animation_loop() {
while true ; do
for FRAME in "${ACTIVE_LOADING[@]}" ; do
printf "\r%s" "${FRAME}"
sleep $ANIMATION_RATE
done
done
}
function start_loading_animation() {
ACTIVE_LOADING=( "${@}" )
# Hide the terminal cursor
tput civis
# disable terminal monitoring
set +m
# run loading loop on sub-shell
play_loading_animation_loop &
# get loading loop pid
LOADING_LOOP_PID="${!}"
}
function stop_loading_animation() {
# kill background process
kill "${LOADING_LOOP_PID}" 2> /dev/null
printf "\n"
# Restore the terminal cursor
tput cnorm
# enable terminal monitoring back
set -m
}
function 42-wizzard-clean() {
# displaying available storage before cleaning
STORAGE_AVAILABLE=$(df -h | grep "$USER" | awk '{print($4)}' | tr 'i' 'B')
printf "• Free storage before cleaning:$GREEN $STORAGE_AVAILABLE $RESET \n"
# start loading animation
start_loading_animation "${cleaning[@]}" 2> /dev/null
# cleaning
/bin/rm -rf $HOME/.Trash/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/*.42* > /dev/null 2>&1
/bin/rm -rf $HOME/*.42* > /dev/null 2>&1
/bin/chmod -R 777 $HOME/Library/Caches/Homebrew > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Caches/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/Caches/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/Slack/Service\ Worker/CacheStorage/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/Code/User/workspaceStorage/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/discord/Cache/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/discord/Code\ Cache/js* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/Google/Chrome/Default/Service\ Worker/CacheStorage/* > /dev/null 2>&1
/bin/rm -rf $HOME/Library/Application\ Support/Google/Chrome/Default/Application\ Cache/* > /dev/null 2>&1
sleep 2
# stop loading animation
stop_loading_animation
# displaying available storage after cleaning
STORAGE_AVAILABLE=$(df -h | grep "$USER" | awk '{print($4)}' | tr 'i' 'B')
printf "• Free storage after cleaning:$GREEN $STORAGE_AVAILABLE $RESET \n"
}
function 42-wizzard-storage() {
# displaying total storage
echo "• Total storage: $BLUE $(df -h | grep "$USER" | awk '{print($2)}' | tr 'i' 'B') $RESET"
# displaying used storage
echo "• Used storage: $RED $(df -h | grep "$USER" | awk '{print($3)}' | tr 'i' 'B') $RESET"
# displaying available storage
echo "• Available storage: $GREEN $(df -h | grep "$USER" | awk '{print($4)}' | tr 'i' 'B') $RESET"
# displaying heavy files
cd
echo "$YELLOW Heavy files: $RESET"
find . -size +50M -exec ls -lh {} \; 2> /dev/null | sort -k 5 -nr | awk '{print $9 $10 " " "\033[32m" $5 "\033[0m" }' | column -t
cd - > /dev/null 2>&1
}
function 42-wizzard-brew() {
# check if brew is installed
if which brew > /dev/null 2>&1
then
BREW_VERSION=$(brew --version | head -n 1 | awk '{print($2)}' | cut -d'-' -f1)
echo "Brew is $GREEN $BREW_VERSION $RESET already installed"
echo "Do you want to reinstall it? (yes/no)"
read -r ANSWER
if [ "$ANSWER" != "yes" ]
then
exit 0
fi
fi
echo "installing brew"
# start loading animation
start_loading_animation "${metro[@]}" 2> /dev/null
cd ~/goinfre
# remove old brew
rm -rf $HOME/.brew > /dev/null 2>&1
rm -rf $HOME/goinfre/homebrew > /dev/null 2>&1
# clean .zshrc
sed -n '/brew/!p' ~/.zshrc > .tmp && mv .tmp ~/.zshrc
# curl brew
mkdir homebrew && curl -sL https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew > /dev/null 2>&1
# add brew to .zshrc
echo "export PATH=~/goinfre/homebrew/bin:$PATH" >> $HOME/.zshrc
# source .zshrc
source $HOME/.zshrc > /dev/null 2>&1
# update brew
brew update > /dev/null 2>&1
# stop loading animation
stop_loading_animation
# brew version
BREW_VERSION=$(brew --version | head -n 1 | awk '{print($2)}' | cut -d'-' -f1)
printf "Homebrew $GREEN v$BREW_VERSION $RESET installed! \n"
cd - > /dev/null 2>&1
}
function 42-wizzard-docker() {
echo "Chose a destination folder to install docker $GREEN hit enter to use goinfre(recommended)$RESET \n"
# read docker destination folder
read -e docker_destination
# if user didn't choose a folder, use goinfre
if [ -z "$docker_destination" ]
then
docker_destination="/goinfre/$USER/docker"
fi
# uninstalling docker if it exists
brew uninstall -f docker docker-compose docker-machine > /dev/null 2>&1
# check if docker is installed
if [ ! -d "/Applications/Docker.app" ] && [ ! -d "~/Applications/Docker.app" ]; then
printf "$YELLOW Docker is not installed $RESET \n"
printf "Please install docker trough $BLUE Managed Software Center $RESET then hit enter to continue \n"
open -a "Managed Software Center"
start_loading_animation "${waiting[@]}" 2> /dev/null
read -n 1
stop_loading_animation
fi
# killing all docker processes
pkill Docker 2> /dev/null
# unlinking docker from /Applications
unlink ~/Library/Containers/com.docker.docker > /dev/null 2>&1
unlink ~/Library/Containers/com.docker.helper > /dev/null 2>&1
unlink ~/.docker > /dev/null 2>&1
unlink ~/Library/Containers/com.docker.docker > /dev/null 2>&1
unlink ~/Library/Containers/com.docker.helper > /dev/null 2>&1
unlink ~/.docker > /dev/null 2>&1
# removing docker old folders
/bin/rm -rf ~/Library/Containers/com.docker.{docker,helper} ~/.docker > /dev/null 2>&1
# creating docker destination folder
mkdir -p "$docker_destination"/{com.docker.{docker,helper},.docker} > /dev/null 2>&1
# linking docker to destination folder
ln -sf "$docker_destination"/com.docker.docker ~/Library/Containers/com.docker.docker > /dev/null 2>&1
ln -sf "$docker_destination"/com.docker.helper ~/Library/Containers/com.docker.helper > /dev/null 2>&1
ln -sf "$docker_destination"/.docker ~/.docker > /dev/null 2>&1
printf "Docker installed in $GREEN $docker_destination $RESET \n"
# opening docker app
open -g -a Docker
}
function 42-wizzard-code() {
# appending an alias to zshrc
echo 'code () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}' >> $HOME/.zshrc
# source zshrc
source $HOME/.zshrc
printf $GREEN "You can use the code command now!" $RESET
}
function 42-wizzard-ssh() {
# starting loading animation
start_loading_animation "${loading[@]}"
# removing old keys if it exists
/bin/rm -rf $HOME/.ssh
# creating new keys
ssh-keygen -C "" -f ~/.ssh/id_rsa -N "" > /dev/null 2>&1
# copying public key to clipboard
cat ~/.ssh/id_rsa.pub | awk '{print($2)}' | pbcopy
sleep 2
# stopping loading animation
stop_loading_animation
echo "$GREEN SSH key copied to clipboard $RESET \n"
echo "$BLUE https://profile.intra.42.fr/gitlab_users $RESET"
sleep 1
# opening gitlab profile to add ssh key
open https://profile.intra.42.fr/gitlab_users
}
function 42-wizzard-nvm() {
# starting loading animation
start_loading_animation "${installing[@]}" 2> /dev/null
# installing nvm
curl -fsSL https://raw.githubusercontent.com/creationix/nvm/v0.33.6/install.sh | zsh > /dev/null 2>&1
# source nvm
source $HOME/.nvm/nvm.sh > /dev/null 2>&1
# stopping loading animation
stop_loading_animation
# print nvm version
NVM_VERSION=$(nvm --version)
echo "nvm $GREEN v$NVM_VERSION $RESET installed! \n"
}
function 42-wizzard-node() {
# checking if nvm is installed
if which nvm > /dev/null
then
# starting loading animation
start_loading_animation "${installing[@]}" 2> /dev/null
# installing node
nvm install node
# stopping loading animation
stop_loading_animation
else
echo "$RED nvm is not installed $RESET \n"
echo "Installing nvm first"
# installing nvm
42-wizzard-nvm
# starting loading animation
start_loading_animation "${installing[@]}" 2> /dev/null
# installing node
nvm install node > /dev/null 2>&1
# stopping loading animation
stop_loading_animation
# print node + npm versions
NODE_VERSION=$(node --version)
NPM_VERSION=$(npm --version)
echo "node $GREEN v$NODE_VERSION $RESET installed! \n"
echo "npm $GREEN v$NPM_VERSION $RESET installed! \n"
fi
}
function 42-wizzard-oh-my-zsh() {
# checking if oh-my-zsh is installed
if [ ! -d "$HOME/.oh-my-zsh" ]
then
# starting loading animation
start_loading_animation "${installing[@]}" 2> /dev/null
# installing oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" > /dev/null 2>&1
# source zshrc
source $HOME/.zshrc > /dev/null 2>&1
# stopping loading animation
stop_loading_animation
# print oh-my-zsh version
printf "$GREEN Oh My Zsh $RESET installed! \n"
fi
}
function 42-wizzard-reset() {
# check for user input
printf "$RED Are you sure you want to reset your session? $RESET (yes/no)\n"
read -r ANSWER
# if user input is yes
if [ "$ANSWER" = "yes" ]
then
# resetting session
touch $HOME/.reset
touch $HOME/.reset_Library
# logging out
osascript -e 'tell application "loginwindow" to «event aevtrlgo»'
else
# aborting reset
printf "$YELLOW Aborting \n"
fi
}
function 42-wizzard-ds-store () {
# check for user input
printf "$YELLOW Are you sure you want to remove .DS_Store files? $RESET (yes/no) \n"
read -r ANSWER
# if user input is yes
if [ "$ANSWER" = "yes" ]
then
cd $HOME
# starting loading animation
start_loading_animation "${cleaning[@]}" 2> /dev/null
# removing .DS_Store files
find . -name .DS_Store -delete > /dev/null 2>&1
# stopping loading animation
stop_loading_animation
cd - > /dev/null 2>&1
else
# aborting delete
printf "$YELLOW Aborting \n"
fi
# check for user input
printf "$YELLOW Are you sure you want to prevent your os from creating .DS_Store files? $RESET (yes/no) \n"
read -r ANSWER
# if user input is yes
if [ "$ANSWER" = "yes" ]
then
# preventing os from creating .DS_Store files
defaults write com.apple.desktopservices DSDontWriteNetworkStores true
else
# aborting prevent
printf "$YELLOW Aborting \n"
fi
}
function 42-wizzard-pip() {
#starting loading animation
start_loading_animation "${installing[@]}" 2> /dev/null
# curling pip
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py > /dev/null 2>&1
# bulding pip
python3 get-pip.py > /dev/null 2>&1
rm get-pip.py > /dev/null 2>&1
# make alias for pip
echo "alias pip='pip3'" >> $HOME/.zshrc
# source zshrc
source $HOME/.zshrc > /dev/null 2>&1
# stopping loading animation
stop_loading_animation
# print pip version
if which pip > /dev/null
then
printf "$GREEN Pip $RESET installed! \n"
else
printf "$RED Pip $RESET not installed! \n"
fi
}
function 42-wizzard-init() {
# check if .42-env is present
if [ ! -f "$HOME/.42-env" ]
then
echo "this is the first time you are using this command"
echo "might take a while, but it's a one time configuration"
echo "do you wish to continue? (yes/no)"
read -r ANSWER
if [ "$ANSWER" != "yes" ]
then
exit 0
fi
# dark theme
echo "Do you prefer to use the dark theme? (yes/no)"
read -r ANSWER
if [ "$ANSWER" = "yes" ]
then
echo "THEME=dark" > $HOME/.42-env
else
echo "THEME=light" > $HOME/.42-env
fi
# bluetooth device
echo "Please enter the name of your bluetooth device(Leave empty to skip)"
read -r ANSWER
if [ "$ANSWER" != "" ]
then
echo "BLUETOOTH=\"$ANSWER\"" >> $HOME/.42-env
fi
echo "Your configuration is done!"
fi
# check if blueutil is installed
if which blueutil > /dev/null
then
sleep 1
else
if which brew > /dev/null
then
brew install blueutil > /dev/null 2>&1
else
echo "please install brew first"
echo "42 -brew"
exit 0
fi
fi
# source .42-env
source $HOME/.42-env > /dev/null 2>&1
# set theme to dark if dark is set in .42-env
if [ $THEME = "dark" ]
then
echo "setting theme to dark"
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to true'
fi
# activate bluetooth
echo "activating bluetooth..."
blueutil -p 1 > /dev/null 2>&1
sleep 3
bt=$(blueutil --is-connected "$BLUETOOTH")
if [ $bt = 0 ]
then
blueutil --connect "$BLUETOOTH" > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "$GREEN $BLUETOOTH connected!"
else
echo "$RED cannot connecte to $BLUETOOTH"
fi
else
echo "$GREEN $BLUETOOTH is already connected! \n"
fi
}
function 42-wizzard-help() {
echo "42-wizzard$GREEN v$WIZZARD_VERSION $RESET"
# big thanks to Oummixa
if [ "$USER" = "oel-yous" ];
then
echo "hey Oummixa!!"
fi
# print help
echo "$GREEN -clean -c $RESET Clean your session."
echo "$GREEN -storage -s $RESET Show your storage and heavy files."
echo "$GREEN -init -i $RESET Insialize your settings and connect your device"
echo "$GREEN -code $RESET Add code command to your zsh."
echo "$GREEN -ssh $RESET Generate a new ssh key and copying it to your clipboard."
echo "$GREEN -brew $RESET Install brew."
echo "$GREEN -docker $RESET Install docker."
echo "$GREEN -nvm $RESET Install nvm."
echo "$GREEN -node $RESET Install node."
echo "$GREEN -pip $RESET Install pip."
echo "$GREEN -oh-my-zsh -omz $RESET Install oh-my-zsh."
echo "$GREEN -ds-store -ds $RESET Remove .DS_Store files, prevent os from creating them."
echo "$GREEN -reset -r $RESET Reset your session."
echo "$GREEN -update -u $RESET Update your the wizzard."
echo "$GREEN -help -h $RESET Show this help."
}
function 42() {
trap stop_loading_animation SIGINT
case $1 in
-clean|-c) 42-wizzard-clean 2> /dev/null
;;
-storage|-s) 42-wizzard-storage
;;
-reset|-r) 42-wizzard-reset
;;
-update|-u) sh ~/.42-wizzard-updater.sh > /dev/null 2>&1
;;
-init|-i) 42-wizzard-init
;;
-brew) 42-wizzard-brew
;;
-docker) 42-wizzard-docker 2> /dev/null
;;
-code) 42-wizzard-code
;;
-ssh) 42-wizzard-ssh
;;
-nvm) 42-wizzard-nvm 2> /dev/null
;;
-pip) 42-wizzard-pip 2> /dev/null
;;
-node) 42-wizzard-node 2> /dev/null
;;
-oh-my-zsh|-omz) 42-wizzard-oh-my-zsh 2> /dev/null
;;
-ds-store|-ds) 42-wizzard-ds-store
;;
-help|-h) 42-wizzard-help
;;
*) echo 42: "Unknown flag: $1" ; echo "Usage: 42 -flag"; 42-wizzard-help
;;
esac
}