-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
190 lines (161 loc) · 3.92 KB
/
bootstrap.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
#!/usr/bin/env bash
# authors:
# Nurudin Imsirovic <imshvc>
#
# file:
# bootstrap.sh
#
# summary:
# Downloads the repository as an archive and once extracted it executes apply.sh
# which deploys files from the dotfiles directory to the home directory of the
# current user. Same applies to other users on the system, the root user would
# need to run apply.sh by hand to apply dotfiles to the root user.
#
# created:
# 2025-02-07 08:55 PM
#
# updated:
# 2025-02-26 11:06 AM
#
# repository:
# https://github.com/imshvc/dotfiles
# set: dotfiles file name and download link
dotfiles_file="dotfiles-main.tar.gz"
dotfiles_link="https://codeload.github.com/imshvc/dotfiles/tar.gz/refs/heads/main"
# set: state
has_curl=0
has_wget=0
has_tar=0
has_unzip=0
downloader=0
home_path=0
is_msys2=0
# detect: cygwin
# fail: not a supported platform
if [[ "$(uname -s)" =~ ^CYGWIN_NT.* ]]; then
echo "fail: cygwin is not a supported platform"
exit 1
fi
# detect: msys2
if [[ "$(uname -s)" =~ ^MSYS_NT.* ]]; then
is_msys2=1
# fix: msys2: user not set
if [ "$USER" = "" ]; then
USER=$USERNAME
fi
fi
# do: check for curl
if command -v curl 2>&1 >/dev/null; then
# pass: curl exists
has_curl=1
fi
# do: check for wget
if command -v wget 2>&1 >/dev/null; then
# pass: wget exists
has_wget=1
fi
# fail: no curl nor wget
if [ $has_curl = 0 ] && [ $has_wget = 0 ]; then
echo "fail: no curl and wget - cannot download archive"
exit 2
fi
# set: curl as downloader
if [ $has_curl = 1 ]; then
downloader=curl
fi
# set: wget as downloader (no curl)
if [ $downloader = 0 ] && [ $has_wget = 1 ]; then
downloader=wget
fi
# do: check for tar
if command -v tar 2>&1 >/dev/null; then
# pass: tar exists
has_tar=1
fi
# fail: no tar
if [ $has_tar = 0 ]; then
echo "fail: no tar - cannot extract archive"
exit 3
fi
# do: check for unzip
if command -v unzip 2>&1 >/dev/null; then
# pass: unzip exists
has_unzip=1
fi
# fail: no unzip
if [ $has_unzip = 0 ]; then
echo "fail: no unzip - cannot extract archive"
exit 3
fi
# set: absolute home path
if [ $EUID = 0 ]; then
home_path="/root"
else
# fix: msys2: inject windows path
if [ $is_msys2 = 1 ]; then
home_path="/c/Users/$USER"
else
home_path="/home/$USER"
fi
fi
# fail: invalid home path
if [ ! -d "$home_path" ]; then
# fail: fake root user
if [ $EUID != 0 ] && [ "${USER,,}" = "root" ]; then
echo "fail: fake root user - try with sudo"
exit 4
fi
# fail: invalid user
echo "fail: invalid user \"$USER\""
exit 5
fi
# fail: dotfiles already exists
if [ -d "$home_path/dotfiles" ]; then
echo "fail: destination \"$home_path/dotfiles\" already exists - delete and try again"
exit 6
fi
# do: create dotfiles directory
mkdir -p "$home_path/dotfiles" 2>&1 >/dev/null
# fail: dotfiles directory
if [ ! -d "$home_path/dotfiles" ]; then
echo "fail: destination \"$home_path/dotfiles\" could not be created - no further info"
exit 7
fi
pushd "$home_path/dotfiles" 2>&1 >/dev/null
# do: download archive
if [ $downloader = curl ]; then
# pass: curl
curl --silent --insecure -o "$dotfiles_file" "$dotfiles_link"
elif [ $downloader = wget ]; then
# pass: wget
wget -nc -q -O "$dotfiles_file" "$dotfiles_link"
else
# fail: unknown downloader
echo "fail: unknown downloader the script does not support"
exit 8
fi
# fail: download failed
if [ ! -f "$dotfiles_file" ]; then
echo "fail: archive download failed: $dotfiles_link"
exit 9
fi
# do: extract archive (stripping dotfiles-main)
tar --strip-components=1 -xzvf "$dotfiles_file" 2>&1 >/dev/null
# do: check for apply.sh
if [ -f "apply.sh" ]; then
# pass: found
chmod +x "apply.sh"
./apply.sh "$home_path" 2>&1 >/dev/null
else
# fail: not found
echo "fail: apply.sh not found"
fi
# do: make bootstrap.sh executable
# because by default it is not
if [ -f "bootstrap.sh" ]; then
chmod +x "bootstrap.sh"
fi
popd 2>&1 >/dev/null
# pass: successful bootstrap
echo "imshvc/dotfiles... done"
exit 0