-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinstall.sh
executable file
·332 lines (262 loc) · 7.14 KB
/
install.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env bash
set -euo pipefail
readonly version=0.1.4
readonly base_dir=$(pwd)
readonly fex_target_dir="$HOME/.fex"
readonly fex_path="$fex_target_dir/bin"
untar_dir=""
readonly red="\x1b[31m"
readonly yellow="\x1b[33m"
readonly green="\x1b[32m"
readonly faint="\x1b[2m"
readonly reset="\x1b[m"
readonly logo="\x1b[46m \x1b[m\x1b[43m \x1b[m"
readonly check="${green}✔${reset}"
function install() {
echo -e "Installing fex $logo"
if [[ $(can_install) == "n" ]]; then
error "Installation cancelled"
fi
local tar_name=$(get_tar_name)
echo -n "Downloading $tar_name from GitHub "
local folder_name=$(download $tar_name)
echo -e "$check"
untar_dir=$base_dir/$folder_name
echo -n "Placing binary at $fex_target_dir "
place_binary
echo -e "$check"
if [[ $(ask "Setup fex default command?") == "y" ]]; then
setup_defaults
fi
if [[ $(which_shell) == "zsh" && $(ask "Setup zsh configuration?") == "y" ]]; then
setup_zsh
fi
if [[ $(which_shell) == "fish" && $(ask "Setup fish configuration?") == "y" ]]; then
setup_fish
fi
cleanup
echo -e "Installation complete $check"
echo "Restart your shell to use fex"
}
function get_tar_name() {
local target_stub=""
case $(uname -sm) in
"Darwin arm64") target_stub="aarch64-macos" ;;
"Darwin x86_64") target_stub="x86_64-macos" ;;
"Linux x86_64") target_stub="x86_64-linux" ;;
*) error "Binary unavailable for $(uname -sm)" ;;
esac
echo "fex-$version-$target_stub.tar.gz"
}
function can_install() {
if ! command -v "fex" &> /dev/null; then
echo "y"
return
fi
local bin_path=$(command -v "fex")
local installed_version=$($bin_path --version)
ask "Found version $installed_version installed, overwrite with $version?"
}
function download() {
local tar_name=$1
if ! command -v "curl" &> /dev/null; then
error "File cannot be downloaded, curl not found"
fi
local file_name="fex.tar.gz"
if [[ -f $file_name ]]; then
rm $file_name
fi
local url="https://github.com/18alantom/fex/releases/download/v$version/$tar_name"
curl -fsSL $url --output $file_name
tar --no-same-owner -xzf $file_name
rm $file_name
local folder_name=${tar_name%.tar.gz}
if [[ ! -d $folder_name ]]; then
error "$folder_name not found after downloading"
fi
echo $folder_name
}
function place_binary() {
cd $untar_dir
if [[ ! -f "fex" ]]; then
error "Binary (fex) not found at $(pwd)"
fi
chmod +x fex
local output=$(./fex --version 2>&1)
if [[ $? -ne 0 ]]; then
error "Invalid binary, error: $output"
fi
if [[ "$output" != "$version" ]]; then
error "Invalid version: $output"
fi
if [[ -f "$fex_path/fex" ]]; then
rm "$fex_path/fex"
fi
mkdir -p $fex_path
mv ./fex "$fex_path/fex"
update_path
}
function update_path() {
local path_update_line="[[ ! \$PATH == *$fex_path* ]] && export PATH=\"$fex_path:\$PATH\""
local rc_path=$(which_rc)
if grep -q "export PATH=\"$fex_path:\$PATH\"" $rc_path; then
return
fi
echo "$path_update_line" >> $rc_path
}
function setup_defaults() {
local default_command="export FEX_DEFAULT_COMMAND=\"fex"
# Show icons
if [[ $(ask "Display icons?") == "n" ]]; then
default_command+=" --no-icons"
fi
# Show item size
if [[ $(ask "Display size?") == "n" ]]; then
default_command+=" --no-size"
fi
# Show perm info
if [[ $(ask "Display permissions?") == "n" ]]; then
default_command+=" --no-perm"
fi
# Show time
local display_time=$(ask "Display time?")
if [[ $display_time == "n" ]]; then
default_command+=" --no-time"
fi
# Which time to set, defaults to changed.
if [[ $display_time == "y" ]]; then
local time_type=$(mcq "Select which time to display" "modified accessed changed")
default_command+=" --time-type $time_type"
fi
default_command+="\""
set_default_command "$default_command"
}
function set_default_command() {
local new_command=$1
local rc_path=$(which_rc)
if old_command=$(grep "export FEX_DEFAULT_COMMAND=" $rc_path); then
sed -i".bac" "s/$old_command/$new_command/" $rc_path
rm $rc_path.bac
else
echo $new_command >> $rc_path
fi
}
function setup_zsh() {
if [[ ! -f ./.fex.zsh ]]; then
error "Zsh file (.fex.zsh) not found at $(pwd)"
fi
local widget_path="$fex_target_dir/.fex.zsh"
# Remove old fex-widget if present
if [[ -f $widget_path ]]; then
rm $widget_path
fi
mv ./.fex.zsh $widget_path
local rc_path=$(which_rc)
local load_widget="[ -f $widget_path ] && source $widget_path"
if ! grep -q "$load_widget" $rc_path; then
echo $load_widget >> $rc_path
fi
# Key binding already exists
if grep -q "^bindkey.*fex-widget" $rc_path; then
return
fi
local bind_ctrlf="bindkey '^f' fex-widget"
if [[ $(ask "Bind CTRL-F to invoke fex?") == "y" ]]; then
echo $bind_ctrlf >> $rc_path
else
echo "Check https://github.com/18alantom/fex?tab=readme-ov-file#zsh-setup for info on custom keybinds"
fi
}
function setup_fish() {
if [[ ! -f ./.fex.fish ]]; then
error "Zsh file (.fex.fish) not found at $(pwd)"
fi
local widget_path="$fex_target_dir/.fex.fish"
# Remove old fex-widget if present
if [[ -f $widget_path ]]; then
rm $widget_path
fi
mv ./.fex.fish $widget_path
local rc_path=$(which_rc)
local load_widget="[ -f $widget_path ] && source $widget_path"
if ! grep -q "$load_widget" $rc_path; then
echo $load_widget >> $rc_path
fi
# Key binding already exists
if grep -q "^bind.*fex-widget" $rc_path; then
return
fi
local bind_ctrlf="bind \cf fex-widget"
if [[ $(ask "Bind CTRL-F to invoke fex?") == "y" ]]; then
echo $bind_ctrlf >> $rc_path
else
echo "Check https://github.com/18alantom/fex?tab=readme-ov-file#fish-setup for info on custom keybinds"
fi
}
function which_rc() {
case $(which_shell) in
"zsh") echo "$HOME/.zshrc" ;;
"bash") echo "$HOME/.bashrc" ;;
"fish") echo "$HOME/.config/fish/config.fish" ;;
*) echo "$HOME/.bashrc" ;;
esac
}
function which_shell() {
local shells="zsh bash fish"
for s in $shells; do
if echo "$SHELL" | grep -q "$s"; then
echo "$s"
break
fi
done
}
function error() {
echo -e "\n${red}Error${reset}: $1"
cleanup
exit 1
}
function ask() {
read -p "$(echo -e "$yellow>$reset $1 $faint([y]/n)$reset: ")" -r
if [[ $REPLY =~ ^[Nn]$ ]]; then
echo "n"
return
fi
echo "y"
}
function mcq() {
local items
read -r -a items <<< "$2"
local len=${#items[@]}
local prompt="$1\n"
prompt+="$yellow>$reset "
for (( i=0; i<$len; i++ )); do
prompt+=${items[$i]}
if [[ $i -eq 0 ]]; then
prompt+=" $faint[$i]$reset, "
elif [[ $i -eq $((len - 1)) ]]; then
prompt+=" $faint($i)$reset"
else
prompt+=" $faint($i)$reset, "
fi
done
prompt+=":"
read -p "$(echo -e $prompt) " -r
local idx=$REPLY
if ! [[ $idx =~ ^[0-9]$ ]]; then
idx=0
fi
if [[ $idx -ge $len ]]; then
idx=0
fi
echo "${items[$idx]}"
}
function cleanup() {
cd $base_dir
if [[ -d $untar_dir ]]; then
rm -rf $untar_dir
fi
if [[ -f "$base_dir/install.sh" ]]; then
rm "$base_dir/install.sh"
fi
}
install || cleanup