-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteward.sh
executable file
·226 lines (206 loc) · 5.91 KB
/
steward.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
#!/usr/bin/env bash
#
# Steward is a script designed to manage dotfile configurations. It allows users
# to sync dotfiles to/from remote, as well as executing helper scripts that are
# stored within the dotfiles.
set -e
PROGRAM_DIR=$(realpath "$0" | xargs dirname)
SCRIPTS_DIR="${PROGRAM_DIR}/scripts"
CONFIRM_BEFORE_EXECUTE="${STEWARD_CONFIRM_EXECUTE:-false}"
VERSION=0.0.1
# Files/Folders that reside in "$HOME/.config/".
CONFIGS=(
"hypr"
"mako"
"wlogout"
"swaylock"
"kitty"
"waybar"
"doom"
"nvim"
"i3"
)
# Files/Folders that reside in "$HOME".
DOTFILES=(
".alacritty.toml"
".zshrc"
".zlogin"
".zlogout"
".zpreztorc"
".zprofile"
".zshenv"
".p10k.zsh"
)
source "${PROGRAM_DIR}/helpers.sh"
# Prints out usage information
usage() {
cat <<EOF
Usage: ${0} COMMAND [OPTIONS]
Commands:
install Installs the script to "$HOME/.local/bin".
sync [local|remote] Synchronizes state [Default: local]:
- "local": Copies the local machine state to the repository.
- "remote": Copies the repository files to the local machine.
setup [OPTIONS] Setup local machine. Supported options:
- "base": Install my must-have packages (Yay, Emacs, Chrome, Rust, etc.).
- "rog": Install Asus ROG ArchLinux packages.
- "hypr": Install Hyprland environment.
- "nvidia": Install Nvidia Graphics card packages.
- "amdcpu": Install AMD CPU microcode.
- "intelcpu": Install Intel CPU microcode.
- "amdgpu": Install AMD GPU packages.
execute SCRIPT Execute one of the scripts in the repository.
Options:
-h Display this help message.
-V Show the program version.
EOF
}
####################################################################
# Description: Installs this script to "$HOME/.local/bin".
# Arguments:
# None
###################################################################
install() {
echo "=> $(ColorBlue 'Installing script')..."
dest="$HOME/.local/bin"
dest="$dest/steward"
src=$(realpath "$0")
if [[ -L $dest ]]; then
read -n1 -rp "File already exists at ${dest}. $(ColorRed 'Override') ? [Y|N] " override
echo
if [[ $override == "Y" ]] || [[ $override == "y" ]]; then
ln -sf "$src" "$dest"
else
echo "$(ColorGreen 'Done')."
return 0
fi
else
ln -s "$src" "$dest"
fi
echo "=> $(ColorGreen 'Done'). Installed at $dest"
return 0
}
####################################################################
# Description: Execute script; Fuzzy finds closest script matching
# the query.
# Arguments:
# Script to search for and execute
####################################################################
execute() {
script=$1
if [[ -z $script ]]; then
echo "$(ColorRed 'Script search string missing')."
fi
readarray -t script < <(fd -t f -e sh -E setup.sh . "${PROGRAM_DIR}/scripts/" | fzf -q -1 --print0 -f "$script" | xargs -0 -n1)
if [[ -n "${script[0]}" ]]; then
if [[ "${CONFIRM_BEFORE_EXECUTE}" = "true" ]]; then
echo "$(ColorYellow "Executing $(basename "${script[0]}")")"
read -n1 -rp "Continue [Y|N]? " con
echo
if [[ $con == 'Y' ]] || [[ $con == 'y' ]]; then
"${script[0]}" "${@:2}"
fi
else
"${script[0]}" "${@:2}"
fi
else
echo "$(ColorRed "Script not found")."
fi
}
####################################################################
# Description: Synchronize local or remote state.
# Arguments:
# "local" or "remote"
####################################################################
sync() {
local remote
local localpath
local remotepath
case $1 in
"local")
echo "=> $(ColorBlue 'Synchronizing'): Local -> Remote"
remote=0
;;
remote)
echo "=> $(ColorBlue 'Synchronizing'): Remote -> Local"
remote=1
;;
*)
echo "$(ColorYellow 'Direction not specified')."
echo "=> $(ColorBlue 'Synchronizing'): Local -> Remote"
remote=0
;;
esac
localpath="$HOME"
remotepath="$PROGRAM_DIR"
for f in "${CONFIGS[@]}"; do
if [[ $remote -eq 0 ]]; then
to="${remotepath}/"
from="${localpath}/.config/${f}"
else
to="${localpath}/.config/"
from="${remotepath}/${f}"
fi
UpdateCopy "$from" "$to"
done
for f in "${DOTFILES[@]}"; do
if [[ $remote -eq 0 ]]; then
to="${remotepath}/"
from="${localpath}/${f}"
else
to="${localpath}/"
from="${remotepath}/${f}"
fi
UpdateCopy "$from" "$to"
done
echo "=> $(ColorGreen 'Done')."
return 0
}
# Main entrypoint of the script
main() {
while getopts "hv" opt; do
case $opt in
h)
usage
exit 0
;;
v)
echo "version: $VERSION"
exit 0
;;
*)
usage
exit 1
;;
esac
done
command=$1
if [[ -z ${command} ]]; then
usage
exit 1
fi
case ${command} in
install)
install "${@:2}"
exit $?
;;
sync)
sync "${@:2}"
exit $?
;;
setup)
bash "${SCRIPTS_DIR}/setup.sh" "${@:2}"
exit $?
;;
execute)
execute "${@:2}"
exit $?
;;
*)
echo "unknown command: ${command}"
usage
exit 1
;;
esac
}
main "$@"