Repository navigation
Expand file tree
/
Copy pathbootstrap.sh
More file actions
executable file
·178 lines (146 loc) · 4.85 KB
/
Copy pathbootstrap.sh
File metadata and controls
executable file
·178 lines (146 loc) · 4.85 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
#!/bin/bash
usage(){
echo "\
$(basename "$0") [OPTION...]
-n, --git_name; Set GIT Name with argument
-e, --git_email; Set GIT Email with argument
--chsh; Set chsh to true
" | column -t -s ";"
}
# Error message
error(){
echo "$(basename "$0"): invalid option -- '$1'";
echo "Try '$(basename "$0") -h' for more information.";
exit 1;
}
while [ -n "$1" ]; do
case "$1" in
-n|--git_name) GIT_NAME="$2"; shift 2;;
-e|--git_email) GIT_EMAIL="$2"; shift 2;;
--chsh) CHSH=1; shift;;
-h|--help) usage; exit 0;;
*) error "$1";;
esac
done
function init_git() {
if [[ -f "$HOME/.gitconfig" ]]; then
echo "- Git config already installed, leaving \$HOME/.gitconfig as-is"
return 0
fi
echo "- Installing git config"
if [[ -z "$GIT_NAME" ]]
then
if [[ ! -t 0 ]]; then
echo "$(basename "$0"): no TTY to prompt for a name -- pass --git_name" >&2
exit 1
fi
echo ""
echo "What's your full name (for git purposes)?"
read GIT_NAME
fi
if [[ -z "$GIT_EMAIL" ]]
then
if [[ ! -t 0 ]]; then
echo "$(basename "$0"): no TTY to prompt for an email -- pass --git_email" >&2
exit 1
fi
echo ""
echo "What's your email address?"
read GIT_EMAIL
fi
for file in .gitignore .gitconfig; do
cp -p $DIR/$file $HOME/$file;
done;
# replace the placeholders in .gitconfig with user input
sed -i -e "s/GIT_NAME/$GIT_NAME/g" $HOME/.gitconfig
sed -i -e "s/GIT_EMAIL/$GIT_EMAIL/g" $HOME/.gitconfig
}
function init_vim() {
echo "- Initializing vim"
for file in .vimrc ; do
ln -sfn $DIR/$file $HOME/$file;
done;
mkdir -p $HOME/.vim/autoload
ln -sfn $DIR/modules/vim-plug/plug.vim $HOME/.vim/autoload/plug.vim
}
function init_zsh() {
echo "- Initializing zsh"
for file in .zshrc ; do
ln -sfn $DIR/$file $HOME/$file;
done;
ln -sfn $DIR/modules/oh-my-zsh $HOME/.oh-my-zsh
mkdir -p $HOME/.oh-my-zsh/custom/plugins
ln -sfn $DIR/modules/zsh-autosuggestions $HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions
ln -sfn $DIR/modules/zsh-syntax-highlighting $HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
mkdir -p $HOME/.config
ln -sfn $DIR/starship.toml $HOME/.config/starship.toml
}
function change_shell() {
[[ "$SHELL" != */zsh ]] || return 0
chsh -s "$(grep -E '/zsh$' /etc/shells | tail -1)"
}
function install_starship_fallback() {
# Not packaged for apt on every distro (e.g. Ubuntu 24.04/noble) -- fall
# back to the official installer, non-interactively.
curl -sS https://starship.rs/install.sh | "$@" sh -s -- -y
}
function ensure_packages() {
local missing=()
for pkg_cmd in git zsh vim curl; do
command -v "$pkg_cmd" >/dev/null 2>&1 || missing+=("$pkg_cmd")
done
local need_starship=0
command -v starship >/dev/null 2>&1 || need_starship=1
if [ ${#missing[@]} -eq 0 ] && [ "$need_starship" -eq 0 ]; then
return 0
fi
local as_root=()
if [[ "$(id -u)" -ne 0 ]]; then
as_root=(sudo)
fi
if ! command -v apt-get >/dev/null 2>&1; then
[ ${#missing[@]} -gt 0 ] && echo "- Missing packages: ${missing[*]} (no apt-get found, install them manually)"
[ "$need_starship" -eq 1 ] && install_starship_fallback "${as_root[@]}"
return 0
fi
DEBIAN_FRONTEND=noninteractive "${as_root[@]}" apt-get update -y
if [ ${#missing[@]} -gt 0 ]; then
echo "- Installing missing packages: ${missing[*]}"
DEBIAN_FRONTEND=noninteractive "${as_root[@]}" apt-get install -y "${missing[@]}"
fi
if [ "$need_starship" -eq 1 ]; then
echo "- Installing starship"
if ! DEBIAN_FRONTEND=noninteractive "${as_root[@]}" apt-get install -y starship 2>/dev/null; then
echo " starship isn't packaged for apt here, falling back to the official installer"
install_starship_fallback "${as_root[@]}"
fi
fi
}
echo "Bootstrapping Environment"
# '1' if running under Windows Subsystem for Linux, '0' otherwise.
readonly WSL=$(grep -q Microsoft /proc/version && echo 1 || echo 0)
# Find it's own location
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ensure_packages
echo "- Installing fonts"
mkdir -p ~/.local/share/fonts/
FONTS_LINK="$HOME/.local/share/fonts/NerdFonts"
if [ -L "$FONTS_LINK" ]; then
ln -sfn "$DIR/.local/share/fonts/NerdFonts" "$FONTS_LINK"
elif [ -e "$FONTS_LINK" ]; then
echo " $FONTS_LINK already exists and isn't a symlink -- leaving it alone (remove it yourself first if you want this repo's fonts linked in)"
else
ln -s "$DIR/.local/share/fonts/NerdFonts" "$FONTS_LINK"
fi
if [[ $WSL == 1 ]]; then
echo "- Installing fonts in windows"
echo " Don't forget to install them manually, you need to fix this!"
fi
echo "- Creating initial directories"
mkdir -p $HOME/src $HOME/bin
init_git
init_zsh
init_vim
if [[ -z "$CHSH" ]]; then
change_shell
fi