-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.dots
executable file
·654 lines (599 loc) · 17.2 KB
/
.dots
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/bin/bash
# Don't use interactive mode as a hash-bang!, see https://stackoverflow.com/questions/6431741/how-to-stop-a-zsh-script-from-being-suspended-tty-output
# Colors as per: http://www.tldp.org/LDP/abs/html/colorizing.html
echoerrcolor() {
if (($colors)); then
case $1 in
none)
str=$'\e[0;37m'
;;
green)
str=$'\e[0;32m'
;;
red)
str=$'\e[0;31m'
;;
blue)
str=$'\e[1;34m'
;;
darkcyan)
str=$'\e[0;36m'
;;
darkgreen)
str=$'\e[1;32m'
;;
darkred)
str=$'\e[1;31m'
;;
magenta)
str=$'\e[0;35m'
;;
darkmagenta)
str=$'\e[1;35m'
;;
esac
echo -ne "$str" >&2
fi
}
echoerrnocolor() {
if (($colors)); then
echo -ne $'\e[0m' >&2
fi
}
echoerr() {
if [ $# -gt 1 ]; then
color=$1
shift
echoerrcolor $color
fi
echo "$@" >&2
if [ -z "$color" ]; then
echoerrnocolor
fi
}
printferr() { printf "$@" >&2; }
author='nobitagamer'
appendshell() {
case "$1" in
start)
add="echo 'Setting up Dotbot. Please do not ^C.' >&2;"
;;
exportproxy)
add="export http_proxy https_proxy ALL_PROXY; \
grep -q 'http_proxy=' /etc/environment || echo \"http_proxy=$http_proxy\" | sudo tee -a /etc/environment; \
grep -q 'https_proxy=' /etc/environment || echo \"https_proxy=$https_proxy\" | sudo tee -a /etc/environment;"
;;
centosfixeol)
add="sudo sed -i -e '/^mirrorlist/d;/^#baseurl=/{s,^#,,;s,/mirror,/vault,;}' /etc/yum.repos.d/CentOS*.repo || true;"
;;
debianwsl)
# Install require pacakages Debian distros on WSL
add="sudo apt update && sudo TZ=Asia/Ho_Chi_Minh DEBIAN_FRONTEND=noninteractive apt install -y git python3-pip locales procps gettext gawk tzdata;"
;;
fedorawsl)
# Install require pacakages Fedora distros on WSL
add="sudo yum --nogpgcheck -y install procps glibc-langpack-en git which gettext;"
;;
streamwsl)
# !BUG: WSL1: fix `RPM: error: db5 error(-30969) from dbenv->open: BDB0091 DB_VERSION_MISMATCH: Database environment version mismatch`, see https://github.com/WhitewaterFoundry/Fedora-Remix-for-WSL/issues/47#issuecomment-531762785
# !BUG: WSL1: error on importing (any) GPG key, see https://github.com/mishamosher/CentOS-WSL/issues/14
add="sudo sed -i '/gpgcheck/ s/^#*/#/' /etc/dnf/dnf.conf; \
sudo yum --nogpgcheck -y upgrade --refresh rpm glibc; \
sudo rm /var/lib/rpm/.rpm.lock; \
sudo yum --nogpgcheck -y upgrade dnf;"
;;
stream9)
# Require by ruby-build for openssl-1.1 on CentOS Stream/Rocky Linux 9
add="sudo yum --nogpgcheck -y install perl-lib perl-FindBin perl-File-Compare;"
;;
enablepasswordless)
# shellcheck disable=SC2016
add='sudo bash -c "echo \"$USER ALL=(ALL) NOPASSWD: ALL\" > /etc/sudoers.d/$USER";'
;;
mkprefix)
add="mkdir -p $2;
cd $2;"
;;
useprefix)
add="cd $2;"
;;
gitclone)
add="git clone https://github.com/$author/dotfiles .;"
;;
gitupdate)
add='git fetch && git reset --hard origin/master;'
;;
ensureparentdirs)
add="mkdir -p $2; \
rmdir $2;"
;;
mv)
add="mv $2 $3;"
;;
echoconfig)
add='echo -e "'$2'" >> '$3';'
;;
runinstaller)
add="./install-profile $2;"
;;
wslumaskfix)
add=". ./wsl-umask-fix.sh;"
;;
osxcmdinstall)
add="cd ~/ && curl -O https://raw.githubusercontent.com/nobitagamer/dotfiles/master/osx-install-xcode-command-line.sh && . ./osx-install-xcode-command-line.sh;"
;;
gitsetname)
if (($3)); then
global=' --global '
else
global=' '
fi
add='git config'$global'user.name "'$2'";'
;;
gitsetemail)
if (($3)); then
global=' --global '
else
global=' '
fi
add='git config'$global'user.email "'$2'";'
;;
gitinit)
add='git init;'
;;
gitaddsub)
add='git submodule add https://github.com/anishathalye/dotbot;'
;;
gitinstallinstall)
add='cp dotbot/tools/git-submodule/install .;'
;;
gitinitialcommit)
add='git add -A; git commit -m "Initial commit";'
;;
esac
if [ -z "$setupshell" ]; then
setupshell=" $add"
else
setupshell="$setupshell $add"
fi
}
# See https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
# 0) op='=';;
# 1) op='>';;
# 2) op='<';;
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if ((10#${ver1[i]:=0} > 10#${ver2[i]:=0}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}
request_sudo()
{
# Ask for the administrator password upfront
echoerr blue "To easy accepting licenses and setup system software, you MUST enter sudo password to continue!"
sudo sudo -v
# Keep-alive: update existing `sudo` time stamp until `.macos` has finished
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
}
centos_update_git()
{
updatecmd='sudo yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm; sudo yum check-update; sudo yum -y --enablerepo="endpoint" install git'
echoerr darkcyan "Auto run Git package update command (see https://serverfault.com/questions/709433/install-a-newer-version-of-git-on-centos-7):"
echoerr blue " $updatecmd"
eval $updatecmd
}
testmode=0
verboseconf=0
preview=1
colors=1
installprofile=''
while [ $# -ne 0 ]; do
case "$1" in
test)
testmode=1
echoerr darkcyan "Test mode enabled."
;;
no-test)
testmode=0
echoerr darkcyan "Test mode disabled."
;;
verbose-config)
verboseconf=1
echoerr darkcyan "Verbose configuration file active."
;;
no-verbose-config)
verboseconf=0
echoerr darkcyan "Concise configuration file active."
;;
preview)
preview=1
echoerr darkcyan "Will show commands to be executed."
;;
no-preview)
preview=0
echoerr darkcyan "Will not show commands to be executed."
;;
colors)
colors=1
echoerr darkcyan "Will print with colors."
;;
no-colors)
colors=0
echoerr darkcyan "No color."
;;
*)
installprofile=$installprofile' '$2
# echoerr red "Unfamiliar configuration option"
;;
esac
shift
done
setupshell=''
installerrun=1
if [ "$(umask)" = "0000" ]; then
echoerr red 'I detected that you are running WSL and current umask value'
echoerr red 'is NOT configured to 022, this may cause permission problems!'
echoerr red 'To continue the following changes must be apply:'
echoerr red '- Append `umask 022` to your ~/.bashrc file.'
read -p "Do you want to apply? (Y/n) " answer
if [ -z "$answer" ]; then
answer="y"
fi
case "$answer" in
Y* | y*)
echoerr green "Fixes will be applied."
setupshell='umask 022;'
appendshell wslumaskfix
;;
*)
echoerr red "CANCELED, may be another time!"
exit 1
;;
esac
fi
echoerr
echoerr blue "Welcome to $author's dotfiles installer powered by Dotbot"
echoerr
echoerr darkred "Please be aware that installer may overwrite existing files in your home directory."
echoerr
echoerr blue "At any time, press ^C to quit. No changes will be made until you confirm."
echoerr
# ----------------------------------
# Try to detect platform
# ----------------------------------
platform='unknown'
osversion=$(. /etc/os-release; echo "$VERSION_ID" | cut -d '.' -f1)
unamestr=$(uname)
case $unamestr in
'Linux')
platform=$(. /etc/os-release; echo "$ID_LIKE $ID")
# Detect RedHat distros
if [ -f /etc/redhat-release ]; then
if [ -f /etc/centos-release ] || [ -f /etc/rocky-release ] || [ -f /etc/almalinux-release ]; then
platform="centos$osversion $platform"
fi
# Treat CentOS Stream 8/9+ as Rocky Linux (use `dnf`)
if [ -f /etc/almalinux-release ] || [ -f /etc/centos-release ] && [ "$osversion" -ge "8" ]; then
platform="rocky $platform"
fi
elif [ -f /etc/lsb-release ]; then
platform="ubuntu$osversion $platform"
else
# !TODO: support anything else
platform="linux $platform"
fi
if grep -q Microsoft /proc/version || grep -q WSL2 /proc/version; then
platform="$platform wsl"
fi
;;
'Darwin')
platform='macos'
;;
MINGW*)
platform='windows'
;;
CYGWIN*)
platform='windows'
;;
'FreeBSD')
# platform='freebsd'
;;
'SunOS')
# platform='solaris'
;;
'AIX') ;;
*) ;;
esac
echoerr green "Detected platform: $platform"
request_sudo
$(which git >&/dev/null)
if [ $? -ne 0 ]; then
if [[ $platform == *"centos7"* ]]; then
echoerr red "Detected $(cat /etc/centos-release)but Git (required by Homebrew) was not found!"
centos_update_git
elif [[ $platform == *"rhel"* ]]; then
installcmd='sudo yum check-update; sudo yum -y install epel-release; sudo yum install -y git'
echoerr red "Detected $(cat /etc/redhat-release) but Git (required by Homebrew) was not found!"
echoerr darkcyan "Auto run git package install command:"
echoerr blue " $installcmd"
eval $installcmd
else
# !TODO: Not support yet.
echoerr red "Git not found! Confirm it is indeed installed and reachable."
exit
fi
fi
if [[ $platform == *"centos7"* ]]; then
# Require Git's version >=2.7.0
gitver=$(git --version | sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,4\}[0-9][^.]*\)/\1/p')
vercomp $gitver 2.7.0
if [ $? -eq 2 ]; then
echoerr red "Detected $(cat /etc/centos-release)but Git version is $gitver < 2.7.0 required by Homebrew!"
centos_update_git
fi
# Require cURL's version >=7.41.0
curlver=$(curl --version | head -n 1 | awk '{ print $2 }')
vercomp $curlver 7.41.0
if [ $? -eq 2 ]; then
updatecmd='sudo yum -y install http://www.city-fan.org/ftp/contrib/yum-repo/city-fan.org-release-3-15.rhel7.noarch.rpm; sudo yum check-update; sudo yum -y install epel-release; sudo yum -y --enablerepo="epel,city-fan.org" install curl'
echoerr red "Detected $(cat /etc/centos-release)but CURL version is $curlver < 7.41.0 required by Homebrew!"
echoerr darkcyan "Auto run cURL package update command (see https://serverfault.com/questions/321321/upgrade-curl-to-latest-on-centos):"
echoerr blue " $updatecmd"
eval $updatecmd
fi
fi
appendshell start
if [ -n "$http_proxy" ] || [ -n "$https_proxy" ]; then
echoerr darkgreen "Found PROXY settings: http_proxy=$http_proxy, https_proxy=$https_proxy"
# Export proxy settings for Homebrew
if [ -n "$http_proxy" ]; then
export ALL_PROXY=$http_proxy
[ -z "$https_proxy" ] && export https_proxy=$http_proxy
fi
if [ -n "$https_proxy" ]; then
export ALL_PROXY=$https_proxy
[ -z "$http_proxy" ] && export http_proxy=$https_proxy
fi
appendshell exportproxy
fi
appendshell enablepasswordless
if [[ $platform == *"macos"* ]]; then
appendshell osxcmdinstall
fi
if [[ $platform == *"fedora"* ]]; then
# Fedora 28 = CentOS Stream 8, Fedora 34 = CentOS Stream 9, see https://superuser.com/questions/215993/which-version-of-fedora-rhel-correspond-to-which-version-of-centos
if [[ $platform == *"centos9"* ]] || [ "$osversion" -ge "33" ]; then
appendshell stream9
fi
fi
if [[ $platform == *"centos7"* ]] || [[ $platform == *"centos8"* ]]; then
appendshell centosfixeol
if [[ $platform == *"wsl"* ]] && [[ $platform != *"centos7"* ]]; then
appendshell streamwsl
fi
fi
if [[ $platform == *"wsl"* ]]; then
if [[ $platform == *"debian"* ]]; then
appendshell debianwsl
elif [[ $platform == *"fedora"* ]]; then
appendshell fedorawsl
fi
fi
prefix="~/.dotfiles"
prefixfull="${prefix/\~/${HOME}}"
if ! [ -d $prefixfull ]; then
echoerr darkcyan "${prefix} is not in use."
appendshell mkprefix $prefix
appendshell gitclone
else
echoerr darkcyan "${prefix} exists, i will try to update."
appendshell useprefix $prefix
appendshell gitupdate
fi
getgitinfo=0
gitinfoglobal=0
if (($installerrun)); then
$(git config user.name >&/dev/null && git config user.email >&/dev/null)
if [ $? -ne 0 ]; then
echoerr darkred "Please note you do not have a name or email set for git."
echoerr darkred "I shall not be able to commit unless you set the missing variables."
while [ 1 ]; do
read -p "Do you want to set them? (Y/n) " answer
if [ -z "$answer" ]; then
answer='y'
fi
case "$answer" in
Y* | y*)
getgitinfo=1
while [ 1 ]; do
read -p "Do you want these settings to be global? (Y/n) " answer
if [ -z "$answer" ]; then
answer='y'
fi
case "$answer" in
Y* | y*)
echoerr green "Adding --global to the set commands."
gitinfoglobal=1
break
;;
N* | n*)
echoerr green "Okay, I shall make them local."
gitinfoglobal=0
break
;;
*)
echoerr red "Answer not understood: ${answer}"
;;
esac
done
break
;;
N* | n*)
echoerr darkgreen "Okay, I shall not."
getgitinfo=0
installerrun=0
break
;;
*)
echoerr red "Answer not understood: ${answer}"
;;
esac
done
fi
fi
if (($getgitinfo)); then
$(git config user.name >&/dev/null)
if [ $? -ne 0 ]; then
gitname="Nguyen Khac Trieu"
else
gitname="$(git config user.name)"
fi
$(git config user.email >&/dev/null)
if [ $? -ne 0 ]; then
gitemail="[email protected]"
else
gitemail="$(git config user.email)"
fi
read -p "What do you want for your git name? [${gitname}] " answer
if [ -z "$answer" ]; then
answer="$gitname"
fi
gitname="$answer"
read -p "What do you want for your git email? [${gitemail}] " answer
if [ -z "$answer" ]; then
answer="$gitemail"
fi
gitemail="$answer"
appendshell gitsetname "$gitname" $gitinfoglobal
appendshell gitsetemail "$gitemail" $gitinfoglobal
fi
if [ -z "$installprofile" ]; then
installprofile=$platform
else
read -p "What profile do you want to run? (macos/linux/wsl.ubuntu/windows10) [${installprofile}] " answer
if [ -z "$answer" ]; then
answer="macos"
fi
installprofile="$answer"
fi
appendshell runinstaller "$installprofile"
# Set git information since it maybe ovewrite by install process.
if (($gitinfoglobal)); then
appendshell gitsetname "$gitname" $gitinfoglobal
appendshell gitsetemail "$gitemail" $gitinfoglobal
fi
if (($preview)); then
printferr "\n${setupshell//; /;\\n}\n\n" # place newline after each command for printing
warningmessage='If you do not see a problem with the above commands, press ENTER. '
else
warningmessage=''
fi
echoerrcolor darkred
read -p "${warningmessage}This is your last chance to press ^C before actions are taken that should not be interrupted. "
echoerrnocolor
result=0
if ! (($testmode)); then
request_sudo
eval $setupshell
result=$?
fi
if [ $result -eq 0 ]; then
# echoerr green 'Install completed, Use the following command to reloading your current shell:'
# shellcheck disable=SC2016
# echoerr blue ' exec sudo -u $USER -i'
echoerr green "Reloading current shell, see you soon..."
# shellcheck disable=SC2093
exec sudo -u "$USER" -i
exit 0
else
exit $result
fi
# shellcheck disable=SC2317
function runDots() {
git clone --recursive https://github.com/nobitagamer/dotfiles .dotfiles
cd ~/.dotfiles && . ./install-profile $@
# Run sections based on command line arguments
for ARG in "$@"; do
if [ $ARG == "bootstrap" ] || [ $ARG == "all" ]; then
echo ""
echo "------------------------------"
echo "Syncing the dev-setup repo to your local machine."
echo "------------------------------"
echo ""
cd ~ && curl -L https://github.com/nobitagamer/dotfiles/tarball/master | tar -xzv --strip-components 1 --exclude={README.md,LICENSE}
fi
if [ $ARG == "osxprep" ] || [ $ARG == "all" ]; then
# Run the osxprep.sh Script
echo ""
echo "------------------------------"
echo "Updating OSX and installing Xcode command line tools"
echo "------------------------------"
echo ""
./.osxprep.sh
fi
if [ $ARG == "brew" ] || [ $ARG == "all" ]; then
# Run the brew.sh Script
# For a full listing of installed formulae and apps, refer to
# the commented brew.sh source file directly and tweak it to
# suit your needs.
echo ""
echo "------------------------------"
echo "Installing Homebrew along with some common formulae and apps."
echo "This might awhile to complete, as some formulae need to be installed from source."
echo "------------------------------"
echo ""
./.brew.sh
fi
if [ $ARG == "osx" ] || [ $ARG == "all" ]; then
# Run the osx.sh Script
# I strongly suggest you read through the commented osx.sh
# source file and tweak any settings based on your personal
# preferences. The script defaults are intended for you to
# customize. For example, if you are not running an SSD you
# might want to change some of the settings listed in the
# SSD section.
echo ""
echo "------------------------------"
echo "Setting sensible OSX defaults."
echo "------------------------------"
echo ""
./.osx.sh
fi
if [ $ARG == "php" ] || [ $ARG == "all" ]; then
# Run the php.sh Script
echo "------------------------------"
echo "Setting up php development environment."
echo "------------------------------"
echo ""
./.php.sh
fi
done
echo "------------------------------"
echo "Completed running .dots, restart your computer to ensure all updates take effect"
echo "------------------------------"
}
# read -p "This script may overwrite existing files in your home directory. Are you sure? (y/n) " -n 1;
# echo "";
# if [[ $REPLY =~ ^[Yy]$ ]]; then
# runDots $@
# fi;
# unset runDots;