-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup
executable file
·313 lines (275 loc) · 10.3 KB
/
setup
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
#!/usr/bin/env sh
# init
# Author: Lucas Larson
#
# Description: bootstrap a new machine
# Assumptions: installation target is one of Arch Linux or Alpine Linux
# Alpine Linux
# wget -O- https://lucaslarson.net/setup | sh
# Arch Linux
# curl https://lucaslarson.net/setup | sh
if command -v -- apk >/dev/null 2>&1; then
alias install='command apk add --verbose'
elif command -v -- pacman >/dev/null 2>&1; then
alias install='command pacman --sync --verbose --noconfirm'
elif command -v -- apt-get >/dev/null 2>&1; then
alias install='command apt-get install --show-progress --assume-yes'
fi
command -p -- printf -- '\n\n .\137\137 .\137\137 \137\137\n' >&2
command -p -- printf -- ' \174\137\137\174 \137\137\137\137 \174\137\137\174\057 \174\137\n' >&2
command -p -- printf -- ' \174 \174\057 \134\174 \134 \137\137\134\n' >&2
command -p -- printf -- ' \174 \174 \174 \134 \174\174 \174\n' >&2
command -p -- printf -- ' \174\137\137\174\137\137\137\174' >&2
command -p -- printf -- ' \057\137\137\174\174\137\137\174\n' >&2
command -p -- printf -- ' \134\057\n\n' >&2
command -p -- printf -- ' Linux setup\n' >&2
command -p -- sleep 1
command -p -- printf -- ' a Lucas Larson production\n\n' >&2
command -p -- sleep 1
# save `date` for backup files
now="$(command -p -- date -- '+%Y%m%d%H%M%S')"
# unset `$PS4`
# if this quaternary prompt string is already unset, then
# set it to the POSIX default: `+ `
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
ps4_temporary="${PS4:-+ }"
unset -v -- PS4 2>/dev/null || PS4=''
set -o xtrace
# pacman
# https://askubuntu.com/a/459425
command -v -- pacman >/dev/null 2>&1 && {
install --refresh --refresh ||
# https://wiki.archlinux.org/?oldid=667441#Installing_packages
install --refresh --sysupgrade 2>/dev/null
}
# apk
command -v -- apk >/dev/null 2>&1 || {
# trust apk only if it matches a known checksum
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'verifying apk tools integrity...\n' >&2
set -o xtrace
# https://web.archive.org/web/20201127045648id_/github.com/ish-app/ish/wiki/Installing-apk-on-the-App-Store-Version#wiki-body
command wget --output-document=- -- 'https://dl-cdn.alpinelinux.org/alpine/v3.16/main/'"$(command -p -- uname -m)"'/apk-tools-static-2.12.9-r3.apk' |
command tar --extract --gzip --strip-components=1 --verbose -- sbin/apk.static &&
./apk.static add apk-tools &&
command -p -- rm -- ./apk.static
# configure only main and community repositories at first
command -p -- mkdir -p -- '/etc/apk' && {
command -p -- printf -- 'https://dl-cdn.alpinelinux.org/alpine/latest-stable/main\n'
command -p -- printf -- 'https://dl-cdn.alpinelinux.org/alpine/latest-stable/community\n'
} >'/etc/apk/repositories'
# update
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'updating Alpine Linux repositories...\n' >&2
set -o xtrace
command apk update --verbose --progress
command apk upgrade --verbose --progress
}
# https://wiki.alpinelinux.org/w/index.php?oldid=17773&title=How_to_get_regular_stuff_working#Man_pages
# man-pages adds `man0`, `man2`, man4`, `man6` to `/usr/share/man/`
{ command -p -- test -d '/usr/share/man/man0' &&
command -p -- test -d '/usr/share/man/man2' &&
command -p -- test -d '/usr/share/man/man4' &&
command -p -- test -d '/usr/share/man/man6'; } || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing man pages...\n' >&2
set -o xtrace
install man-pages
}
command -v -- mandoc >/dev/null 2>&1 || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing mandoc for man pages...\n' >&2
set -o xtrace
install mandoc
}
command -v -- less >/dev/null 2>&1 || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing less to read man pages...\n' >&2
set -o xtrace
install less
}
# https://wiki.alpinelinux.org/w/index.php?oldid=17773&title=How_to_get_regular_stuff_working#Shell_.40_commandline
# https://web.archive.org/web/20210218201739id_/web.archive.org/screenshot/docs.google.com/document/d/10-8wjANQGbG43XZ0wN57M1RYOLUwu9RZATNe9vJQYKw/mobilebasic
# https://wiki.alpinelinux.org/w/index.php?oldid=18038&title=Alpine_newbie_apk_packages#coreutils_libc_and_utmps_in_alpine
install coreutils
{ command -p -- test -x '/usr/bin/coreutils' &&
command -p -- find --version >/dev/null 2>&1 | command -p -- grep -e 'findutils' >/dev/null 2>&1; } || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing Linux utilities...\n' >&2
set -o xtrace
}
install util-linux pciutils usbutils coreutils binutils findutils grep wget curl openssl sudo sed attr dialog readline
# add additional Alpine Linux repositories if applicable
command -v -- apk >/dev/null 2>&1 &&
command -p -- test -s '/etc/apk/repositories' && {
{
command -p -- printf -- 'https://dl-cdn.alpinelinux.org/alpine/edge/main\n'
command -p -- printf -- 'https://dl-cdn.alpinelinux.org/alpine/edge/community\n'
command -p -- printf -- 'https://dl-cdn.alpinelinux.org/alpine/edge/testing\n'
} >>'/etc/apk/repositories' &&
command apk update
}
# ssh
# https://wiki.alpinelinux.org/w/index.php?oldid=13842&title=Setting_up_a_ssh-server#OpenSSH
command -p -- test -d '/etc/ssh' || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing OpenSSH...\n' >&2
set -o xtrace
install openssh
}
# gpg
# https://wiki.alpinelinux.org/w/index.php?oldid=17295&title=Setting_up_a_laptop#Creating_GPG_keys
command -p -- test -x '/usr/bin/gpg2' || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing GPG...\n' >&2
set -o xtrace
install gnupg
}
# git
command -v -- git >/dev/null 2>&1 || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing Git...\n' >&2
set -o xtrace
install git
}
# git user
command git config --global --get user.name >/dev/null 2>&1 || {
command git config --global user.name 'Lucas Larson'
}
# git default branch
command git config --global --get init.defaultBranch >/dev/null 2>&1 || {
command git config --global init.defaultBranch 'main'
}
# time zone
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'updating time zone information...\n' >&2
set -o xtrace
install --no-cache tzdata
# python
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'checking Python installation...\n' >&2
set -o xtrace
command -v -- python >/dev/null 2>&1 || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing Python...\n' >&2
set -o xtrace
install python3
}
# zsh
command -v -- zsh >/dev/null 2>&1 || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing Zsh...\n' >&2
set -o xtrace
install zsh
}
# chsh
# part of shadow on Alpine Linux
command -v -- chsh >/dev/null 2>&1 || {
install shadow
}
# Oh My Zsh
command -v -- omz >/dev/null 2>&1 ||
command -p -- test -d "${ZSH:=${HOME%/}/.oh-my-zsh}" || {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installing Oh My Zsh...\n' >&2
if command -v -- wget >/dev/null 2>&1; then
set -o xtrace
command -p -- sh -c "$(command wget 'https://github.com/ohmyzsh/ohmyzsh/raw/HEAD/tools/install.sh' --output-document=-)" --unattended --keep-zshrc
elif command -v -- curl >/dev/null 2>&1; then
set -o xtrace
command -p -- sh -c "$(command curl --location --url 'https://github.com/ohmyzsh/ohmyzsh/raw/HEAD/tools/install.sh')" --unattended --keep-zshrc
fi
{ set +o xtrace; } 2>/dev/null
}
# update, repair everything again before close
command -v -- apk >/dev/null 2>&1 && {
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'updating...\n' >&2
set -o xtrace
command apk update --verbose --verbose --progress
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'upgrading...\n' >&2
set -o xtrace
command apk upgrade --verbose --verbose --progress
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'repairing and resolving dependencies...\n' >&2
set -o xtrace
command apk fix --verbose --verbose --depends --progress
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'verifying installations...\n' >&2
set -o xtrace
command apk verify --verbose --verbose --progress
}
# cleanup
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'cleaning up temporary installation files and performing housekeeping...\n' >&2
set -o xtrace
# message of the day
command -p -- test -w '/etc/motd' &&
command -p -- cp -- '/etc/motd' '/etc/motd-'"${now-}" &&
command -p -- printf -- '' >'/etc/motd'
# delete thumbnail cache files
command -p -- find -- . \
-type f \
'(' \
-name '.DS_Store' -o \
-name 'Desktop.ini' -o \
-name 'Thumbs.db' -o \
-name 'desktop.ini' -o \
-name 'thumbs.db' \
')' \
-delete 2>/dev/null
# delete empty files
# except those within `.git/` directories
# and those with specific names
command -p -- find -- . \
-type f \
-size 0 \
-path '*/.git' -prune -o \
-path '*example*' -prune -o \
-path '*sample*' -prune -o \
-path '*template*' -prune -o \
-path '*test*' -prune -o \
! -name "$(command -p -- printf -- 'Icon\015\012')" \
! -name '*LOCK' \
! -name '*empty*' \
! -name '*hushlogin' \
! -name '*ignore' \
! -name '*journal' \
! -name '*lock' \
! -name '*lockfile' \
! -name '.dirstamp' \
! -name '.gitkeep' \
! -name '.gitmodules' \
! -name '.keep' \
! -name '.sudo_as_admin_successful' \
! -name '.watchmanconfig' \
! -name '__init__.py' \
! -name 'favicon.*' \
-delete 2>/dev/null
# delete empty directories recursively
# but skip Git-specific and `/.well-known/` directories
command -p -- find -- . -type d -links 2 \
-path '*/.git' -prune -o \
-path '*/.well-known' -prune -o \
-delete 2>/dev/null
# if the shell can be changed and
# if zsh is available, then replace bash, ash, and sh with zsh in `/etc/passwd`
command -p -- test -w '/etc/passwd' &&
command -v -- zsh >/dev/null 2>&1 &&
command -p -- grep -E -e '/bin/b?a?sh' '/etc/passwd' >/dev/null 2>&1 &&
command -p -- cp -- '/etc/passwd' '/etc/passwd-'"${now-}" &&
command -p -- sed -e 's|/bin/b\{0,1\}a\{0,1\}sh$|'"$(command -v -- zsh)"'|' '/etc/passwd-'"${now-}" >'/etc/passwd'
# done
{ set +o xtrace; } 2>/dev/null
command -p -- printf -- 'installation complete\n' >&2
command -p -- sleep 1
command -p -- printf -- 'exiting to apply updates...\n' >&2
# restore `$PS4`
PS4="${ps4_temporary:-+ }"
unset -v -- ps4_temporary 2>/dev/null || ps4_temporary=''
{
command -p -- printf -- '\n'
command -p -- printf -- 'done!\n'
} >&2
exit