-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·447 lines (399 loc) · 11.1 KB
/
bootstrap.sh
File metadata and controls
executable file
·447 lines (399 loc) · 11.1 KB
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
#!/bin/bash
# some colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
# resource files used by bootstraping
INFO_FILE=$HOME/.config/.bootstrap_info
# constants
OS_UPDATE_INTERVAL_DAYS=5
OS_UPDATE_INTERVAL_SECONDS=$(( ${OS_UPDATE_INTERVAL_DAYS}*24*60*60 ))
function yellow()
{
printf "${YELLOW}$@${NC}\n"
}
function red()
{
printf "${RED}$@${NC}\n"
}
function green()
{
printf "${GREEN}$@${NC}\n"
}
function confirm()
{
read -p "${@} " answer
[[ ! "$answer" =~ ^[Yy]$ ]] && return 1 || return 0
}
function show_os_info()
{
local os_name=`grep -E '^NAME=' /etc/os-release | sed 's/^[^=]*=//; s/"//g'`
local version=`grep -E '^VERSION=' /etc/os-release | sed 's/^[^=]*=//; s/"//g'`
if [[ -n $os_name ]]; then
echo -e "Operating System: ${GREEN}${os_name} ${version}${NC}"
else
echo -e "Operating System: ${RED}"Unknown"${NC}"
fi
echo -e "Host: ${GREEN}$(hostname)${NC}"
echo -e "User: ${GREEN}${USER}${NC}"
}
function update_os()
{
if should_update_os; then
yellow "Updating packages"
local cmd=`echo sudo ${update_os_command}`
echo $cmd
sh -c "$cmd"
[[ $? -eq 0 ]] && update_last_update_timestamp
fi
}
function should_update_os()
{
if [[ ! -f ${INFO_FILE} ]]; then
# File doesn't exist. Probably the first time doing bootstraping
cat << EOF > ${INFO_FILE}
last_update:
repo_site:
email:
EOF
return 0
fi
# if the file exists, let's check if we have gone beyond the interval
# read last_update timestamp
last_update_timestamp=$(sed -n -E "s/^last_update: (.*)/\1/p" ${INFO_FILE})
current_timestamp=$(date +%s)
difference=$((current_timestamp - last_update_timestamp))
if (( difference > OS_UPDATE_INTERVAL_SECONDS )); then
# Now get the confirmation
yellow "You haven't updated the packages in $(( difference / 86400 )) days."
read -p "Continue to update packages (y/n): " input
if [[ "$input" =~ ^[Yy]$ ]]; then
return 0
else
return 1
fi
fi
return 1
}
function update_last_update_timestamp()
{
sed -i -E "s/(^last_update: )(.*)$/\1$(date +%s)/" ${INFO_FILE}
}
function export_install_command()
{
if which dnf &> /dev/null; then
HAS_DNF=1
install_command="dnf install -y"
update_os_command="dnf update -y"
elif which apt-get &> /dev/null; then
HAS_APT=1
install_command="apt-get install -y"
update_os_command="apt-get update -y"
elif which pacman &> /dev/null; then
HAS_PACMAN=1
install_command="pacman --noconfirm -Sy"
update_os_command="pacman --noconfirm -Syu"
fi
if [[ -n $install_command ]]; then
echo -e "Install Command: ${GREEN}${install_command}${NC}"
else
echo -e "Install Command: ${RED}"Unknown"${NC}"
exit 1
fi
}
function probe_os_info()
{
yellow "Probing OS information"
show_os_info
export_install_command
echo
}
function install()
{
local cmd=`echo "sudo ${install_command} ${@}"`
echo $cmd
sh -c "$cmd"
}
function check_dependencies()
{
# We rely on dialog, if this doesn't exist install it first
yellow "Checking dependencies for bootstraping"
which dialog &> /dev/null
if [[ $? -ne 0 ]]; then
install dialog
fi
}
function pip_install()
{
local cmd=`echo "pip3 install ${@}"`
echo $cmd
sh -c "$cmd"
}
function essentials()
{
local pkgs=()
pkgs+=(git)
pkgs+=(ripgrep)
pkgs+=(tree)
pkgs+=(btop)
pkgs+=(htop)
pkgs+=(bat)
pkgs+=(wget)
pkgs+=(xclip)
pkgs+=(dictd)
if [[ $HAS_DNF -eq 1 ]]; then
pkgs+=(fastfetch)
else
pkgs+=(neofetch)
fi
pkgs+=(tmux)
pkgs+=(vim)
install ${pkgs[*]}
}
function dev_tools()
{
local pkgs=()
# more selective ones
if [[ $HAS_DNF -eq 1 ]]; then
pkgs+=(@development-tools)
pkgs+=(ninja-build)
pkgs+=(python3-devel) # for building boost
pkgs+=(gmp-devel)
pkgs+=(mpfr-devel)
pkgs+=(libmpc-devel)
pkgs+=(isl-devel)
pkgs+=(libzstd-devel)
elif [[ $HAS_APT -eq 1 ]]; then
pkgs+=(build-essential)
pkgs+=(ninja-build)
pkgs+=(pkg-config)
pkgs+=(libevent-dev)
pkgs+=(bison)
pkgs+=(byacc)
pkgs+=(python3-dev)
pkgs+=(npm)
pkgs+=(nodejs)
pkgs+=(libgmp-dev)
pkgs+=(libmpfr-dev)
pkgs+=(libmpc-dev)
pkgs+=(libisl-dev)
pkgs+=(libzstd-dev)
pkgs+=(flex)
pkgs+=(help2man)
fi
pkgs+=(cmake)
pkgs+=(ccache)
pkgs+=(unzip)
pkgs+=(texinfo)
install ${pkgs[*]}
}
function python_stuff()
{
local pkgs=()
if [[ $HAS_APT -eq 1 ]]; then
# force python3
pkgs+=(python3)
pkgs+=(python3-pip)
pkgs+=(ipython3)
pkgs+=(python3-venv)
else
pkgs+=(python)
pkgs+=(python-pip)
pkgs+=(ipython)
pkgs+=(python-jedi)
fi
install ${pkgs[*]}
}
function extra_repos()
{
# more selective ones
if [[ $HAS_DNF -eq 1 ]]; then
local ver=$(rpm -E %fedora)
repos+=("https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-${ver}.noarch.rpm")
repos+=("https://mirrors.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-${ver}.noarch.rpm")
repos+=("fedora-workstation-repositories")
install ${repos[*]}
sh -c "sudo dnf config-manager --set-enabled google-chrome"
pkgs+=(google-chrome-stable)
install ${pkgs[*]}
fi
}
# install latest nvim from source code
function nvim_from_sources()
{
echo " - Installing pre-requisites..."
pre_requisites=()
if [[ $HAS_APT -eq 1 ]]; then
pre_requisites=(ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip)
install ${pre_requisites[*]}
elif [[ $HAS_DNF -eq 1 ]]; then
pre_requisites=(ninja-build libtool autoconf automake cmake gcc gcc-c++ make pkgconfig unzip patch gettext curl)
install ${pre_requisites[*]}
fi
echo " - Cloning neovim..."
# create tmp directory if not exists
if [ ! -d $HOME/tmp/neovim ]; then
git clone https://github.com/neovim/neovim.git ~/tmp/neovim
else
cd ~/tmp/neovim && git pull
fi
# switch to stable branch
echo " - Switching to stable..."
cd ~/tmp/neovim && git checkout stable && git pull
echo " - Building and installing neovim..."
cd ~/tmp/neovim && make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=${HOME}/.local install
}
function setup_github_personal_ssh()
{
ssh_key_file="${HOME}/.ssh/id_github_personal"
if [[ ! -f ${ssh_key_file} ]]; then
yellow "Setting up ssh keys ${ssh_key_file}"
green "Generatig ed25519 key with no passphrase"
cmd="ssh-keygen -N '' -t ed25519 -C \"github, personal(${USER})\" -f ${ssh_key_file}"
eval ${cmd}
eval "$(ssh-agent -s)" && green "ssh agent started" || return 2
eval ssh-add ${ssh_key_file} && \
green "ssh keys added\nCopy & paste the following key to Github\n\n" || \
return 2
eval cat ${ssh_key_file}.pub
echo
else
yellow "Not setting up ssh keys since ${ssh_key_file} already exists..."
eval "$(ssh-agent -s)" && green "ssh agent started" || return 2
eval ssh-add ${ssh_key_file}
return 1
fi
return 0
}
function check_if_auth_ok()
{
ssh -T git@github.com 2>&1 | grep -q "successfully authenticated"
return $?
}
function setup_configs()
{
url='git@github.com:amilaperera/dotfiles'
if [[ ${BYPASS_SSH} -eq 1 ]]; then
url='https://github.com/amilaperera/dotfiles'
fi
if [[ ! -d "$HOME/.dotfiles" ]]; then
green "Cloning dotfiles"
git clone ${url} ~/.dotfiles
else
yellow "$HOME/.dotfiles directory already exists"
fi
echo
green "Setting up personal dotfiles"
prev=$(pwd)
cd ~/.dotfiles && make all
cd $prev
}
function setup_configs_if_auth_ok()
{
yellow "Check if the user can be validated with the ssh keys..."
if check_if_auth_ok; then
green "Authentication successful with GitHub"
setup_configs
fi
}
function nerd_fonts()
{
echo " - Downloading Nerd fonts..."
curl -L https://github.com/ryanoasis/nerd-fonts/releases/download/v3.0.2/Hack.zip --output /tmp/Hack.zip
if [[ $? -eq 0 ]]; then
echo " - Downloading finished"
mkdir -p ~/.fonts
unzip /tmp/Hack.zip -d ~/.fonts
echo " - Updating font cache"
fc-cache -fv
else
red "Error downloading Nerd fonts"
return 1
fi
return $?
}
function ai_stuff()
{
echo " - Installing opencode..."
curl -fsSL https://opencode.ai/install | bash
if [[ $? -eq 0 ]]; then
echo " - opencode installed successfully"
else
red "Error installing OpenCode"
return 1
fi
}
# Function wrapper to install packages
function install_packages()
{
yellow "Installing ${@}..."
${@}
echo
}
########################################
# main
########################################
probe_os_info
# confirm before continuing
if ! confirm "Continue the rest of bootstrapping with ROOT privileges to update/install packages (y/n) ?"; then
echo "Exiting."
exit 1
fi
check_dependencies
update_os
cmd=(dialog --separate-output --no-tags --checklist "Select Options:" 22 76 16)
options=(
1 "Essential packages (bash, tmux, git, curl etc.)" on
2 "Development tools" off
3 "Python stuff" off
4 "Extra repositories (Fedor Only)" off
5 "Install nvim latest from sources" off
6 "Setup github SSH" off
7 "Setup personal configs(bash,tmux,vim etc.)" off
8 "Install Nerd fonts(Hack)" off
9 "AI" off
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
clear
for choice in $choices; do
case $choice in
1)
install_packages essentials
;;
2)
install_packages dev_tools
;;
3)
install_packages python_stuff
;;
4)
install_packages extra_repos
;;
5)
install_packages nvim_from_sources
;;
6)
if setup_github_personal_ssh; then
# wait until the user wishes to continue
if ! confirm "Continue with setup (y/n) ?"; then
break
fi
fi
;;
7)
setup_configs_if_auth_ok
;;
8)
install_packages nerd_fonts
;;
9)
install_packages ai_stuff
;;
esac
done
green "Bye...."
unset HAS_DNF HAS_APT HAS_PACMAN RED YELLOW GREEN NC install_command
unset INFO_FILE OS_UPDATE_INTERVAL_DAYS OS_UPDATE_INTERVAL_SECONDS
unset -f yellow red green
unset -f install