-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdotfiles-util.sh
executable file
·286 lines (242 loc) · 7.94 KB
/
dotfiles-util.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
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
#!/bin/bash
export DOT_DIR=~/dotfiles
export DOT_BACKUP_DIR=~/dotfiles.bu
########## HELPER FUNCTIONS ##########
# Prints regular messages
function green_echo() {
echo -e "\033[0;32m[Message] ${1}\033[0m"
}
# Prints prompts requiring user attention
function yellow_echo() {
echo
echo -e "\033[0;33m[Attention] ${1}\033[0m"
echo
}
# Prints error prompts
function red_echo() {
echo
echo -e "\033[0;31m[Fatal] ${1}\033[0m"
echo
}
# Checks if the script is located in $DOT_DIR. Else, end the script
function verify_script_dir() {
script_dir=$( cd -- "$( dirname -- ${BASH_SOURCE[0]} )" &> /dev/null && pwd )
if [[ "$script_dir" != "$DOT_DIR" ]]; then
red_echo "${DOT_DIR} directory not found"
exit 1
fi
}
# Prompts user yes/no for the installation of $1
function selection_prompt() {
yellow_echo "Would you like to install $1 related files?"
read -p "y/n? > " -n1 -r REPLY # -p for prompt, -n1 for reading 1 character, -r for reading literally
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
green_echo "Deploying $1 related files..."
true
else
green_echo "Skipping $1 related files..."
false
fi
}
# Creates a symlink for $1 at $2
# If a file/dir already exists at $2, move to $DOT_BACKUP_DIR
# E.g.: backup_then_symlink ~/dotfiles/i3/config ~/.config/i3/config
function backup_then_symlink() {
if [[ -e $2 ]]; then
yellow_echo "Existing $2 will be moved to ${DOT_BACKUP_DIR}."
mkdir -p $DOT_BACKUP_DIR
mv $2 ${DOT_BACKUP_DIR}/
fi
green_echo "Creating symlink for $1 at $2..."
ln -s $1 $2
}
########## INSTALL FUNCTIONS ##########
# Installs cross-platform core utilities
function install() {
green_echo "
_____ _ ___ _ __ _ _
|_ _| |_ ___ ___ | \ ___| |_ / _(_) |___ ___
| | | ' \/ -_) _ \ | |) / _ \ _| _| | / -_|_-<
|_| |_||_\___\___/ |___/\___/\__|_| |_|_\___/__/
"
verify_script_dir
if selection_prompt 'Doom'; then
CURRENT_FILES=("init.el" "config.el" "packages.el" "org-config.el")
mkdir -p ~/.config/doom/
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/doom/${FILE} ~/.config/doom/${FILE}
done
fi
if selection_prompt 'Fish'; then
CURRENT_FILES=("config.fish" "alias.fish")
mkdir -p ~/.config/fish/
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/fish/${FILE} ~/.config/fish/${FILE}
done
# Fish functions
CURRENT_FILES=("fish_prompt.fish")
mkdir -p ~/.config/fish/functions/
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/fish/functions/${FILE} ~/.config/fish/functions/${FILE}
done
fi
if selection_prompt 'Git'; then
CURRENT_FILES=("gitignore_global" "gitconfig")
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/git/${FILE} ~/.${FILE}
done
fi
if selection_prompt 'lf'; then
CURRENT_FILES=('lfrc' 'icons')
mkdir -p ~/.config/lf/
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/lf/${FILE} ~/.config/lf/${FILE}
done
fi
if selection_prompt 'Tmux'; then
backup_then_symlink ${DOT_DIR}/tmux/tmux.conf ~/.tmux.conf
fi
if selection_prompt 'Vim'; then
backup_then_symlink ${DOT_DIR}/vim/vimrc ~/.vimrc
mkdir -p ~/.vim/
backup_then_symlink ${DOT_DIR}/vim/colors ~/.vim/colors
fi
if selection_prompt 'Wezterm'; then
mkdir -p ~/.config/wezterm/
backup_then_symlink ${DOT_DIR}/wezterm/wezterm.lua ~/.config/wezterm/wezterm.lua
fi
if selection_prompt 'Zsh'; then
backup_then_symlink ${DOT_DIR}/zsh/zshrc ~/.zshrc
fi
yellow_echo 'Ending the dotfiles installation...'
green_echo "
____ __ __U _____ u
U | __')u \ \ / /\| ___'|/
\| _ \/ \ V / | _|'
| |_) | U_|'|_u | |___
|____/ |_| |_____|
_|| \\_.-,//|(_ << >>
(__) (__)\_) (__)(__) (__)
"
}
# Installs Homebrew formulae and runs macOS specific
function macos-install() {
green_echo "Starting macOS specific installlation process..."
verify_script_dir
if [[ "$OSTYPE" != "darwin"* ]] && [[ "$OSTYPE" != "macOS" ]]; then
red_echo "You are not using macOS! OSTYPE == $OSTYPE"
exit 1
fi
green_echo 'Homebrew Core Formulae/Casks:
Existing formulae will be uninstalled.
Some casks might not be compitable with non-macOS systems.
Do you want to proceed?'
if selection_prompt 'Homebrew Core'; then
# brew remove --force $(brew list --formula)
# brew remove --cask --force $(brew list)
brew bundle --file ${DOT_DIR}/homebrew/Brewfile_core
fi
if selection_prompt 'macOS Settings'; then
. "${DOT_DIR}/macos/macos-settings.sh"
fi
if selection_prompt 'macOS Tiling WM Utilities'; then
mkdir -p ~/.config/yabai
backup_then_symlink ${DOT_DIR}/yabai/yabairc ~/.config/yabai/yabairc
mkdir -p ~/.config/skhd
backup_then_symlink ${DOT_DIR}/skhd/skhdrc ~/.config/skhd/skhdrc
CURRENT_FILES=(plugins colors.sh icons.sh sketchybarrc)
mkdir -p ~/.config/sketchybar
for FILE in ${CURRENT_FILES[@]}; do
backup_then_symlink ${DOT_DIR}/sketchybar/${FILE} ~/.config/sketchybar/${FILE}
done
fi
green_echo 'Homebrew Optional Formulae/Casks:
This might take a while, and I honestly recommend you to go through each program manually.
Do you want to proceed?'
if selection_prompt 'Homebrew Optional'; then
brew bundle --file ${DOT_DIR}/homebrew/Brewfile_optional
fi
yellow_echo 'Ending the macos specific installation...'
}
########## AUXILIARY FUNCTIONS ##########
# Deletes $DOT_BACKUP_DIR directory
function delete_backup() {
yellow_echo "Deleting ${DOT_BACKUP_DIR}..."
rm -rf $DOT_BACKUP_DIR
}
# Prompts user and adds SSH host and user to ~/.ssh/config
function add_ssh_shortcut() {
mkdir -p ~/.ssh/
[[ ! -e ~/.ssh/config ]] && touch ~/.ssh/config
echo -n 'add_ssh_shortcut) Enter host nickname (e.g. data): '
read host_nickname
echo -n 'add_ssh_shortcut) Enter host URL (e.g. data.cs.best.school.in.the.world.located.in.west.lafayette.edu): '
read host_url
echo -n 'add_ssh_shortcut) Enter username for the host (e.g. good_student_69): '
read username
echo "Host $host_nickname
Hostname $host_url
User $username" >> ~/.ssh/config
green_echo "${username}@${host_url} has been added to the SSH config! Try <ssh ${host_nickname}>."
}
# Installs font from the supplied URL to ~/.local/share/fonts
# $1: URL of the font zip file to be installed
# It is highly recommended that the URL comes from https://nerdfonts.com/font-downloads
function install_font() {
if [[ ! $1 ]]; then red_echo 'Target URL is missing!'; exit 1; fi
mkdir -p ~/.local/share/fonts
cd ~/.local/share/fonts
wget -O temp-font.zip $1
unzip temp-font.zip
rm temp-font.zip
fc-cache -vf
cd - > /dev/null 2>&1
green_echo "A font from $1 successfully installed!"
}
# Prints help message about this script
function help() {
green_echo "
Dotfiles Utility Script Usage
===================================================================
Syntax: ./dotfiles-util.sh <arg>
-------------------------------------------------------------------
args:
--install : Deploy configuration symlinks for cross-platform utilities
--macos-install : Deploy configuration symlinks for macOS and related utilities
--delete-backup : Delete $DOT_BACKUP_DIR
--add-ssh-shortcut : Add a new SSH shortcut at ~/.ssh/config
--install-font <URL> : wget a font file from URL (preferably from NERDFont website) and install it at ~/.local/share/fonts/
"
}
########### MAIN CALL HERE ##########
# Executes util functionalities based on the supplied flag ($1)
function main() {
case $1 in
"--install")
install
;;
"--macos-install")
macos-install
;;
"--delete-backup")
delete_backup
;;
"--add-ssh-shortcut")
add_ssh_shortcut
;;
"--install-font")
install_font $2
;;
"--help")
help
;;
*)
red_echo 'Invalid option'
help
;;
esac
exit 0
}
main $@
########### MAIN CALL HERE ##########