-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup.sh
executable file
·198 lines (153 loc) · 5.19 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
#!/bin/bash
#==================================
# Variables
#==================================
declare GITHUB_REPOSITORY="excalith/.dotfiles"
declare DOTFILES_ORIGIN="[email protected]:$GITHUB_REPOSITORY.git"
declare DOTFILES_TARBALL_URL="https://github.com/$GITHUB_REPOSITORY/tarball/main"
declare DOTFILES_UTILS_URL="https://raw.githubusercontent.com/$GITHUB_REPOSITORY/main/scripts/utils/utils.sh"
#==================================
# Settings
#==================================
declare DOTFILES_DIR="$HOME/.dotfiles"
declare MINIMUM_MACOS_VERSION="12.0"
declare MINIMUM_UBUNTU_VERSION="20.04"
#==================================
# Helper Functions
#==================================
download() {
local url="$1"
local output="$2"
if command -v "curl" &> /dev/null; then
curl \
--location \
--silent \
--show-error \
--output "$output" \
"$url" \
&> /dev/null
return $?
elif command -v "wget" &> /dev/null; then
wget \
--quiet \
--output-document="$output" \
"$url" \
&> /dev/null
return $?
fi
return 1
}
download_dotfiles() {
local tmpFile=""
print_title "Download and extract archive"
tmpFile="$(mktemp /tmp/XXXXX)"
download "$DOTFILES_TARBALL_URL" "$tmpFile"
print_result $? "Download archive" "true"
mkdir -p "$DOTFILES_DIR"
print_result $? "Create '$DOTFILES_DIR'" "true"
# Extract archive in the `dotfiles` directory.
extract "$tmpFile" "$DOTFILES_DIR"
print_result $? "Extract archive" "true"
rm -rf "$tmpFile"
print_result $? "Remove archive"
}
download_utils() {
local tmpFile=""
tmpFile="$(mktemp /tmp/XXXXX)"
download "$DOTFILES_UTILS_URL" "$tmpFile" \
&& . "$tmpFile" \
&& rm -rf "$tmpFile" \
&& return 0
return 1
}
extract() {
local archive="$1"
local outputDir="$2"
if command -v "tar" &> /dev/null; then
tar \
--extract \
--gzip \
--file "$archive" \
--strip-components 1 \
--directory "$outputDir"
return $?
fi
return 1
}
verify_os() {
local os_name="$(get_os)"
local os_version="$(get_os_version)"
# Check if the OS is `macOS` and supported
if [ "$os_name" == "macos" ]; then
if is_supported_version "$os_version" "$MINIMUM_MACOS_VERSION"; then
print_success "$os_name $os_version is supported"
return 0
else
print_error "Minimum MacOS $MINIMUM_MACOS_VERSION is required (current is $os_version)"
fi
# Check if the OS is `Ubuntu` and supported
elif [ "$os_name" == "ubuntu" ]; then
if is_supported_version "$os_version" "$MINIMUM_UBUNTU_VERSION"; then
print_success "$os_name $os_version is supported"
return 0
else
print_error "Minimum Ubuntu $MINIMUM_UBUNTU_VERSION is required (current is $os_version)"
fi
# Check if the OS is `Windows WSL` and supported
elif [ "$os_name" == "wsl_ubuntu" ]; then
print_success "Windows WSL on Ubuntu is supported"
return 0
# Check if the OS is `Arch` and supported
elif [ "$os_name" == "arch" ]; then
print_success "$os_name is supported"
return 0
# Check if the OS is `Alpine` and supported
elif [ "$os_name" == "alpine" ]; then
print_success "$os_name is supported"
return 0
# Exit if not supported OS
else
print_error "$os_name is not supported. This dotfiles are intended for MacOS, Ubuntu and Arch"
fi
return 1
}
#==================================
# Main Install Starter
#==================================
main() {
# Ensure that the following actions are made relative to this file's path.
cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
# Load utils
if [ -x "utils.sh" ]; then
. "utils.sh" || exit 1
else
download_utils || exit 1
fi
print_section "Excalith Dotfiles Setup"
# Ask user for sudo
print_title "Sudo Access"
ask_for_sudo
# Verify OS and OS version
print_title "Verifying OS"
verify_os || exit 1
# Check if this script was run directly (./<path>/setup.sh),
# and if not, it most likely means that the dotfiles were not
# yet set up, and they will need to be downloaded.
printf "%s" "${BASH_SOURCE[0]}" | grep "setup.sh" &> /dev/null \
|| download_dotfiles
# Start installation
. "$HOME/.dotfiles/system/$(get_os)/install.sh"
# Ask for git credentials
. "$HOME/.dotfiles/scripts/utils/generate_git_creds.sh"
# Ask for SSH (Disabled since I started using another method)
#. "$HOME/.dotfiles/scripts/utils/generate_ssh.sh"
# Ask for GPG (Disabled since I started using another method)
#. "$HOME/.dotfiles/scripts/utils/generate_gpg.sh"
# Link to original repository and update contents of dotfiles
if [ "$(git config --get remote.origin.url)" != "$DOTFILES_ORIGIN" ]; then
. "$HOME/.dotfiles/scripts/utils/init_dotfile_repo.sh '$DOTFILES_ORIGIN'"
fi
# Ask for restart
. "$HOME/.dotfiles/scripts/utils/restart.sh"
}
main "$@"