-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras.sh
executable file
·225 lines (169 loc) · 4.83 KB
/
extras.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
#!/usr/bin/env bash
# Uncomment for dry-run
# set -n
# Uncomment for verbose stack trace
# set -x
SHELL_SCRIPT_FILE_NAME="extras.sh"
# --------------------------------------
# DETECT OS AND DISTRO
function detect_os () {
case $OSTYPE in
darwin*)
DOTFILES_OS_FAMILY="macos"
detect_pkg_manager
;;
linux*)
DOTFILES_OS_FAMILY="linux"
detect_pkg_manager
;;
freebsd*)
DOTFILES_OS_FAMILY="freebsd"
detect_pkg_manager
;;
*)
echo "OS could not be automatically detected." >&2
DOTFILES_OS_FAMILY="unknown"
manual_set_os
detect_pkg_manager
if [[ $DOTFILES_PKG_MANAGER == "unknown" ]]; then
echo "Package manager could not be automatically detected." >&2
manual_set_pkg_manager
fi
esac
}
function detect_pkg_manager () {
# debian
if [ -x "$(command -v apt-get)" ]; then
DOTFILES_PKG_MANAGER="apt-get"
apt-get update -y > /dev/null 2> /dev/null
# rhel
elif [ -x "$(command -v dnf)" ]; then
DOTFILES_PKG_MANAGER="dnf"
source /etc/os-release
if [[ $ID == "rocky" ]]; then
DOTFILES_OS_RHEL=1
DOTFILES_OS_RHEL_VERSION="$(echo $PLATFORM_ID | awk -F: '{print $2}' | awk -Fl '{print $2}')"
else
DOTFILES_OS_RHEL=0
fi
# suse
elif [ -x "$(command -v zypper)" ]; then
DOTFILES_PKG_MANAGER="zypper"
# freebsd
elif [ -x "$(command -v pkg)" ]; then
DOTFILES_PKG_MANAGER="pkg"
# macos
elif [ -x "$(command -v brew)" ]; then
DOTFILES_PKG_MANAGER="brew"
# unknown
else
echo "Package manager not found." >&2
DOTFILES_PKG_MANAGER="unknown"
manual_set_pkg_manager
fi
# [[ $DOTFILES_PKG_MANAGER != "unknown" ]] && echo "Detected package manager: $DOTFILES_PKG_MANAGER" >&2
}
function manual_set_os () {
os_options=(linux freebsd macos unknown)
echo "Select the OS family:"
select os in "${os_options[@]}"; do
# Test if selected option is even within range
if [[ "$REPLY" -gt 0 ]] && [[ "$REPLY" -le "${#os_options[@]}" ]]; then
DOTFILES_OS_FAMILY=$os
break
else
echo "Unknown option $REPLY. Please choose an option between 1 and ${#os_options[@]}."
fi
done
}
function manual_set_pkg_manager () {
pkg_mgr_options=(apt-get dnf zypper pkg brew unknown)
echo ""; echo "Select the package manager:"
select pkg_mgr in ${pkg_mgr_options[@]}; do
# Test if selected option is even within range
if [[ $REPLY -gt 0 ]] && [[ $REPLY -le ${#pkg_mgr_options[@]} ]]; then
# Test if selected package manager is installed. Display error and re-prompt if not.
if [ -x "$(command -v $pkg_mgr)" ]; then
DOTFILES_PKG_MANAGER=$pkg_mgr
break
else
echo "Selected package manager '$pkg_mgr' is not installed."
echo ""; echo "Select the package manager:"
fi
else
echo "Unknown option $REPLY. Please choose an option between 1 and ${#pkg_mgr_options[@]}."
fi
done
}
# --------------------------------------
# SCRIPT INITIALIZATION
# SUBCOMMAND PARSING
# GLOBAL HELP
function init () {
#./outputs.sh step "Detecting OS and package manager"
detect_os
if [[ $DOTFILES_OS_FAMILY == "unknown" ]] || [[ $DOTFILES_PKG_MANAGER == "unknown" ]]; then
echo "OS family and package manager could not be determined. This script will now exit."
exit 1
fi
export DOTFILES_OS_FAMILY
export DOTFILES_PKG_MANAGER
export DOTFILES_OS_RHEL
export DOTFILES_OS_RHEL_VERSION
# required for help text to work properly in the mod scripts
export DOTFILES_MODS_RUNNER=$SHELL_SCRIPT_FILE_NAME
}
function parse_subcommand () {
case $1 in
zsh) ./mods/zsh.sh $2 $3 $4 ;;
oh-my-zsh | oh_my_zsh | omz) ./mods/omz.sh $2 $3 $4 ;;
neovim | nvim) ./mods/neovim.sh $2 $3 $4 ;;
nvchad) ./mods/nvchad.sh $2 $3 $4 ;;
all)
./mods/zsh.sh $2 $3 $4
sleep .1
./mods/omz.sh $2 $3 $4
sleep .1
./mods/neovim.sh $2 $3 $4
sleep .1
./mods/nvchad.sh $2 $3 $4
;;
*)
echo "error: Missing required parameter" >&2
do_help
exit 1
esac
}
function do_help () {
cat << EOF
$SHELL_SCRIPT_FILE_NAME [-h] <module> <command>
Modules:
\`zsh\`: Zsh
\`omz\`: Oh My Zsh
\`all\`: apply command to all
Global Options:
\`-h\`: Display this help text
Required environment variables:
DOTFILES_DIR - absolute path to the dotfiles repository
CAN be set via \`export\` or in-line. Separate declaration and command with a space.
EOF
}
# --------------------------------------
# MOD-SPECIFIC STUFF
# All logic for Mods (Zsh, OMZ, Neovim, etc.) are now in their own files in
# the 'mods' folder.
# -------------------------------------
# MAIN LOOP
# check if DOTFILES_DIR is set; if not, error out
[ -z $DOTFILES_DIR ] && echo "Required ENV variable \`DOTFILES_DIR\` is not set! Please set via inline declaration or via \`export\`." && exit 5
init
# check global options
case $1 in
-h | help)
# display help
do_help
exit 0
;;
# if nothing here matches, just initialize os-specific variables and send it all to parse_subcommand
*) parse_subcommand $1 $2 $3 ;;
esac