-
Notifications
You must be signed in to change notification settings - Fork 48
/
install_dotfiles
executable file
·231 lines (203 loc) · 5.19 KB
/
install_dotfiles
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
#!/bin/bash
#
# Setup up dotfiles
set -eu
os="mac"
mac="mac"
linux="linux"
fancy_echo() {
local fmt="$1"; shift
# shellcheck disable=SC2059
printf "\\n$fmt\\n" "$@"
}
make_workspace_dir() {
local workspace=~/Devel/Workspace
if [ ! -d "$workspace" ]; then
mkdir -p "$workspace"
fancy_echo "Creating workspace directory done."
fi
}
# Checks if a particular program is installed on the machine
exists() {
command -v "$1" >/dev/null 2>&1
}
display_success_message() {
fancy_echo "$1 successfully installed."
}
is_homebrew_installed() {
if ! exists brew; then
fancy_echo "Please make sure homebrew and brew-cask are installed on the machine"
fancy_echo "See: https://brew.sh/"
fancy_echo "See: https://caskroom.github.io/"
exit 1
fi
}
install_zsh_files() {
local zshrc_file=~/.zshrc
local zshenv_file=~/.zshenv
local oh_my_zsh=~/.oh-my-zsh/
if [ ! -d "$oh_my_zsh" ]; then
# install oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
display_success_message "oh_my_zsh"
fi
# Delete any installed zshrc file
if [ -f "$zshrc_file" ] || [ -L "$zshrc_file" ]; then
rm -f $zshrc_file
fi
# Delete any installed zshenv file
if [ -f "$zshenv_file" ] || [ -L "$zshenv_file" ]; then
rm -f $zshenv_file
fi
ln -s "$PWD"/zshenv "$zshenv_file"
if [[ "$os" == "$linux" ]]; then
ln -s "$PWD"/linux-zshrc "$zshrc_file"
else
ln -s "$PWD"/mac-zshrc "$zshrc_file"
fi
# Source "$zshrc_file"
display_success_message "$zshrc_file"
}
#is_haxornews_installed() {
#}
install_htop() {
local htop_rc=~/.htoprc
# Install htop if it doesn't exists
if ! exists htop && [ "$os" == "$mac" ] ; then
brew install htop
fi
# Delete any installed htop_rc file
if [ -f "$htop_rc" ] || [ -L "$htop_rc" ]; then
rm -f "$htop_rc"
fi
ln -s "$PWD"/htop/htoprc "$htop_rc"
}
install_git() {
local git_conf=~/.gitconfig
# Install git if it doesn't exists
if ! exists git; then
brew install git
fi
# Delete any installed git directory
if [ -d "$git_conf" ] || [ -L "$git_conf" ]; then
rm -rf "$git_conf"
fi
ln -s "$PWD"/git/gitconfig "$git_conf"
display_success_message "$git_conf"
}
install_tmux() {
local tmux_conf=~/.tmux.conf
local tmux_dir=~/.tmux
# Install htop if it doesn't exists
if ! exists tmux && [ "$os" == "$mac" ];then
brew install tmux
fi
# Delete any installed tmux.conf file
if [ -f "$tmux_conf" ] || [ -L "$tmux_conf" ]; then
rm -f "$tmux_conf"
fi
ln -s "$PWD"/tmux/.tmux.conf "$tmux_conf"
display_success_message "$tmux_conf"
if [ -d "$tmux_dir" ] || [ -L "$tmux_dir" ]; then
rm -rf "$tmux_dir"
fi
ln -s "$PWD"/tmux/.tmux "$tmux_dir"
display_success_message "$tmux_dir"
}
install_nvim() {
local nvim_dir=~/.config/nvim
# Install nvim if it doesn't exists
if ! exists nvim && [ "$os" == "$mac" ]; then
brew install nvim
fi
# Delete any installed nvim directory
if [ -d "$nvim_dir" ] || [ -L "$nvim_dir" ]; then
rm -rf "$nvim_dir"
fi
pushd "${PWD}/config/nvim"
./install.sh
popd
display_success_message "$nvim_dir"
}
help() {
# Display Help
fancy_echo "Facilitates the migration and backup of computer configurations, including Homebrew installations."
fancy_echo "Syntax: install.sh [-p|h|v]"
fancy_echo "options:"
fancy_echo "p The operating system to port to. Valid values are mac, linux"
fancy_echo "h Print this Help."
fancy_echo "v Print version and exit."
}
version() {
fancy_echo "v1.0.0"
}
mac_install() {
fancy_echo "Restoring a mac install"
make_workspace_dir
is_homebrew_installed
check_and_update_shell_to_zsh
install_zsh_files
install_htop
install_git
install_tmux
install_nvim
fancy_echo "Done"
}
linux_install() {
fancy_echo "Restoring a linux install"
make_workspace_dir
check_and_update_shell_to_zsh
install_zsh_files
install_htop
install_git
install_tmux
install_nvim
fancy_echo "Done"
}
update_shell() {
fancy_echo "Updating shell"
local shell_path;
shell_path="$(command -v zsh)"
fancy_echo "Changing your shell to zsh ..."
if ! grep "$shell_path" /etc/shells > /dev/null 2>&1 ; then
fancy_echo "Adding '$shell_path' to /etc/shells"
sudo sh -c "echo $shell_path >> /etc/shells"
fi
sudo chsh -s "$shell_path" "$USER"
}
check_and_update_shell_to_zsh() {
# Check if using zsh and not Homebrew-installed zsh
if [[ "$SHELL" == */zsh ]]; then
update_shell
else
# Update shell if not using native or Homebrew zsh
update_shell
fi
}
#######################
# Main function #
#######################
#######################
# Process options #
#######################
# Get the options
# Credits: https://www.redhat.com/sysadmin/arguments-options-bash-scripts
while getopts ":hp:" option; do
case $option in
h) # Display help
help
exit;;
p) # Enter operating system
os=$OPTARG;;
\?) # Invalid option
fancy_echo "Error: Invalid option"
exit;;
esac
done
if [[ "$os" == "$mac" ]]; then
mac_install
elif [[ "$os" == "$linux" ]]; then
linux_install
else
fancy_echo "$os is an unsupported operating system. Supported os are $mac, $linux"
fi