-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.sh
executable file
·251 lines (193 loc) · 5.11 KB
/
setup.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
#!/bin/bash
############################
# This symlinks all the dotfiles to ~/
############################
########## Support Functions
answer_is_yes() {
[[ "$REPLY" =~ ^[Yy]$ ]] \
&& return 0 \
|| return 1
}
ask() {
print_question "$1"
read
}
ask_for_confirmation() {
print_question "$1 (y/n) "
read -n 1
printf "\n"
}
ask_for_sudo() {
# Ask for the administrator password upfront
sudo -v
# Update existing `sudo` time stamp until this script has finished
# https://gist.github.com/cowboy/3118588
while true; do
sudo -n true
sleep 60
kill -0 "$$" || exit
done &> /dev/null &
}
cmd_exists() {
[ -x "$(command -v "$1")" ] \
&& printf 0 \
|| printf 1
}
execute() {
$1 &> /dev/null
print_result $? "${2:-$1}"
}
get_answer() {
printf "$REPLY"
}
get_os() {
declare -r OS_NAME="$(uname -s)"
local os=""
if [ "$OS_NAME" == "Darwin" ]; then
os="osx"
elif [ "$OS_NAME" == "Linux" ] && [ -e "/etc/lsb-release" ]; then
os="ubuntu"
fi
printf "%s" "$os"
}
mkd() {
if [ -n "$1" ]; then
if [ -e "$1" ]; then
if [ ! -d "$1" ]; then
print_error "$1 - a file with the same name already exists!"
else
print_success "$1"
fi
else
execute "mkdir -p $1" "$1"
fi
fi
}
print_error() {
# Print output in red
printf "\e[0;31m [✖] $1 $2\e[0m\n"
}
print_info() {
# Print output in purple
printf "\n\e[0;35m $1\e[0m\n\n"
}
print_question() {
# Print output in yellow
printf "\e[0;33m [?] $1\e[0m"
}
print_result() {
[ $1 -eq 0 ] \
&& print_success "$2" \
|| print_error "$2"
[ "$3" == "true" ] && [ $1 -ne 0 ] \
&& exit
}
print_success() {
# Print output in green
printf "\e[0;32m [✔] $1\e[0m\n"
}
# Warn user this script will overwrite current dotfiles
while true; do
read -p "Warning: this will overwrite your current dotfiles. Continue? [y/n] " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
# Get the dotfiles directory's absolute path
SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd -P)"
DOTFILES_DIR="$(dirname "$SCRIPT_DIR")"
dir=~/dotfiles # dotfiles directory
# Get current dir (so run this script from anywhere)
export DOTFILES_DIR
DOTFILES_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Change to the dotfiles directory
echo -n "Changing to the $dir directory..."
cd $dir
echo "done"
#
# Actual symlink stuff
#
declare -a FILES_TO_SYMLINK=(
'shell/tmux.conf'
'git/gitconfig'
'git/gitignore'
'rc/ripgreprc'
'rc/iex.exs'
'shell/tmux'
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main() {
local i=''
local sourceFile=''
local targetFile=''
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
for i in ${FILES_TO_SYMLINK[@]}; do
sourceFile="$(pwd)/$i"
targetFile="$HOME/.$(printf "%s" "$i" | sed "s/.*\/\(.*\)/\1/g")"
if [ ! -e "$targetFile" ]; then
execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
elif [ "$(readlink "$targetFile")" == "$sourceFile" ]; then
print_success "$targetFile → $sourceFile"
else
ask_for_confirmation "'$targetFile' already exists, do you want to overwrite it?"
if answer_is_yes; then
rm -rf "$targetFile"
execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
else
print_error "$targetFile → $sourceFile"
fi
fi
done
unset FILES_TO_SYMLINK
echo -e "\\n\\ninstalling to ~/.config"
echo "=============================="
if [ ! -d "$HOME/.config" ]; then
echo "Creating ~/.config"
mkdir -p "$HOME/.config"
fi
config_files=$(ls -d ~/dotfiles/config/*)
for config in $config_files; do
target="$HOME/.config/$( basename "$config" )"
echo $target
if [ -e "$target" ]; then
echo "~${target#$HOME} already exists... Skipping."
else
echo "Creating symlink for $config"
ln -s "$config" "$target"
fi
done
}
setup_linux () {
###############################################################################
# Linux #
###############################################################################
#ln -s ~/dotfiles/linux/mimeapps.list $HOME/.local/share/applications
if [ ! -d ~/.local/share/fonts ]; then
mkdir $HOME/.local/share/fonts
fi
if [ ! -d ~/.local/share/fonts/FiraCode ]; then
echo "Installing Fonts"
cd /tmp
wget -q --show-progress https://github.com/ryanoasis/nerd-fonts/releases/latest/download/FiraCode.zip
unzip FiraCode.zip -d ~/.local/share/fonts/FiraCode
fi
###############################################################################
# Caps to Esc #
###############################################################################
setxkbmap -option caps:escape
}
setup_mac () {
echo done
}
setup () {
platform=$(uname);
if [[ $platform == 'Linux' ]]; then
setup_linux
elif [[ $platform == 'Darwin' ]]; then
setup_mac
fi
}
main
setup