-
Notifications
You must be signed in to change notification settings - Fork 1
/
core-menu.sh
executable file
·462 lines (416 loc) · 13.9 KB
/
core-menu.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/bin/bash
set -e
# Start heading
EASYLEGACY="false"
OCONFIG=""
arg0=$(basename "$0" .sh)
blnk=$(echo "$arg0" | sed 's/./ /g')
usage_info()
{
echo "Usage: $arg0 [-l|--legacy] type"
echo " $blnk [{-c|--config} config_location] "
echo " $blnk [-d|--debug]"
echo " $blnk [-h|--help]"
}
usage()
{
exec 1>2 # Send standard output to standard error
usage_info
continue 2
}
error()
{
echo "$arg0: $*" >&2
exit 1
}
help()
{
usage_info
echo
echo " {-l|--legacy} type -- launch for legacy menu control (possible values: yad, gtkdialog, kdialog, zenity, Xdialog, dialog, none. default: auto select)"
echo " {-h|--help} -- print this help message and exit"
echo " {-d|--debug} -- prints verbose info for debugging"
echo " {-c|--config} config_location -- set custom config location (default: ./options/options.json)"
[[ $_ != $0 ]] && exit 0 2>/dev/null || return 0 2>/dev/null;
}
flags()
{
while test $# -gt 0
do
case "$1" in
(-l|--legacy)
shift
[ $# = 0 ] && OCTYPE="" || OCTYPE="$1"
EASYLEGACY="true"
shift;;
(-c|--config)
shift
[ $# = 0 ] && error "No config specified"
OCONFIG="$1"
shift;;
(-d|--debug)
echo "Debug Mode Enabled"
EZ_DEBUG_MODE="true"
shift;;
(-h|--help)
help ;;
(*) usage;;
esac
done
}
flags "$@"
# End heading
SCRIPT_PATH=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
MAIN_SCRIPT_PATH=$SCRIPT_PATH
INCLDIR=$MAIN_SCRIPT_PATH/includes
optionspath=$MAIN_SCRIPT_PATH/options
temp_path="$MAIN_SCRIPT_PATH/tmp/"
export SCRIPT_PATH="$SCRIPT_PATH"
export supertitle="Start Menu"
export supericon=""
source $INCLDIR/common.sh
function goto
{
label=$1
cmd=$(sed -n "/^:[[:blank:]][[:blank:]]*${label}/{:a;n;p;ba};" $0 |
grep -v ':$')
eval "$cmd"
exit
}
menuParse() {
# Root Menu:
# (jq -r '.options.menuEntry[]? | .name' $options_path)
# First Child Menu:
# (jq -r '.options.menuEntry[]? | select(.name == "this") | .subMenuEntry[]? | .name' $options_path)
# First Child Dependency:
# (jq -r '.options.menuEntry[]? | select(.name == "this") | .dependencies[]? | .dep' $options_path)
# We will keep the base address static
BASE_ENTRY=".options"
# Define MENUENTRY if not passed ($1)
if [ ! "$1" ]; then
MENUENTRY=".menuEntry[]?"
else
MENUENTRY="$1"
fi
# Define THIS_MENU if not passed ($2)
if [ ! "$2" ]; then
THIS_MENU=".name"
else
THIS_MENU="$2"
fi
LAST_MENUENTRY="$MENUENTRY"
LAST_MENUNAME="$THIS_MENU"
current_menuentry="$MENUENTRY"
current_menu="$THIS_MENU"
if [[ $current_menuentry != $previous_menuentry ]] || [[ $current_menu != $previous_menu ]]; then
if [[ -n "$previous_menu" ]]; then
echo "New menu: $previous_menu"
fi
echo -e ""
echo -e "Menu ${BLUE}$current_project${NC}"
echo -e ""
else
current_menuentry="$previous_menuentry"
current_menu="$previous_menu"
MENUENTRY="$previous_menuentry"
THIS_MENU="$previous_menu"
fi
# If a third and fourth variables are passed, we use those for a search definition
# $3 = variable_name
# $4 = Variable_Value
if [[ "$3" ]] && [[ "$4" ]]; then
THIS_SEARCH="select($3 == '"$4"')"
else
THIS_SEARCH=""
fi
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "MENUENTRY: $MENUENTRY"
echo "THIS_MENU: $THIS_MENU"
echo "THIS_SEARCH: $THIS_SEARCH"
fi
parseMenu(){
# Parse menu
MENUOPTIONS=()
while IFS= read -r entry; do
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Parsing Menuoptions Entry: $entry"
fi
# Check Dependency Here First
MENUITEMDEPS=()
while IFS= read -r menuitem; do
if [[ "$menuitem" != "" ]]; then
MENUITEMDEPS+=("$menuitem")
$menuitem # 2>/dev/null;
if [ $? -eq 0 ]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Dependency Passed: $menuitem"
fi
else
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Dependency Falied: $menuitem"
fi
smconfigerror="true";
fi
else
MENUITEMDEPS+=("$menuitem")
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "No dependency found: $entry"
fi
fi
# If dependency passes, add entry
if [[ ! "$smconfigerror" ]] || [[ "$menuitem" == "" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Adding Entry: $entry"
fi
MENUOPTIONS+=("$entry")
fi
smconfigerror="";
done < <(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${entry}\") | .dependencies[]?.dep" $options_path)
if [[ "$MENUITEMDEPS" == "" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "No deps stated, adding Entry: $entry"
fi
MENUOPTIONS+=("$entry")
fi
done < <(jq -r "$BASE_ENTRY$MENUENTRY$THIS_MENU" $options_path)
}
parseMenu
while :
do
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "SUBMENUENTRY: $SUBMENUENTRY"
echo "MENUENTRY: $MENUENTRY"
echo "THIS_MENU: $THIS_MENU"
echo "previous_menuentry: $previous_menuentry"
echo "previous_menu: $previous_menu"
echo "current_menuentry: $current_menuentry"
echo "current_menu: $current_menu"
echo "initial MENUOPTIONS:"
echo "${MENUOPTIONS[@]}"
fi
if [[ "$SUBMENUENTRY" == "" ]] && [[ "$previous_menuentry" != "" ]]; then
echo "NEW SUBMENU"
if [[ "$previous_menuentry" == "$current_menuentry" ]]; then
MENUENTRY=".menuEntry[]?"
THIS_MENU=".name"
else
MENUENTRY="$previous_menuentry"
THIS_MENU="$previous_menu"
fi
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "reparsed MENUENTRY: $MENUENTRY"
echo "reparsed THIS_MENU: $THIS_MENU"
fi
parseMenu
fi
menu ".." "${MENUOPTIONS[@]}"
answer=$(0< "${dir_tmp}/${file_tmp}")
for i in "${MENUOPTIONS[@]}" ; do
i="$(echo ${i} | tr -d '\n' | tr -d '\r' | tr -d \")"
if [[ "$(echo ${answer} | tr -d '\n' | tr -d '\r')" == "${i}" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Starting \"${i}\" ..."
fi
# Check for Flags here, to see if we need to form any options
# If Flags, and Command are both present, we find n flags.
# Command
# COMMAND=()
# while IFS= read -r com; do
# COMMAND+=("$com")
# done < <(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .command" $options_path)
COMMAND="$(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .command" $options_path)"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Command: $COMMAND"
fi
# Flags
FLAGS=()
while IFS= read -r flag; do
FLAGS+=("$flag")
done < <(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .flags[]?.flag" $options_path)
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "FLAGS: ${FLAGS[@]}"
fi
if [[ "${FLAGS[@]}" != "" ]]; then
FLAG_ANSWERS=""
menu ".." "${FLAGS[@]}"
answer=$(0< "${dir_tmp}/${file_tmp}")
for f in "${FLAGS[@]}" ; do
if [[ "$(echo ${answer} | tr -d '\n' | tr -d '\r')" == "${f}" ]]; then
flag_name="$f"
flag_description="$(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .flags[]? | select(.flag == \"${f}\") | .descr" $options_path)"
flag_type="$(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .flags[]? | select(.flag == \"${f}\") | .type" $options_path)"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "flag_name: ${flag_name}"
echo "flag_description: ${flag_description}"
echo "flag_type: ${flag_type}"
fi
if [[ "$flag_type" == "boolean" ]]; then
# Show popup question for bool
question "$flag_description"
flag_answer="${?}"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "flag_answer: ${flag_answer}"
fi
if [[ "${flag_answer}" = 0 ]]; then
flag_command="$flag_name"
elif [[ "${flag_answer}" == "1" ]]; then
flag_command=""
fi
elif [[ "$flag_type" == "string" ]]; then
# Show popup question for bool
input "$flag_description"
flag_answer=$(0< "${dir_tmp}/${file_tmp}" )
if {[ ${flag_answer} != "" ]}; then
flag_command="$flag_name $flag_answer"
fi
fi
FLAG_ANSWERS="${FLAG_ANSWERS} $flag_command"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "flag_command: ${flag_command}"
echo "FLAG_ANSWERS: ${FLAG_ANSWERS}"
fi
fi
if [[ "${answer}" == ".." ]]; then
SUBMENUENTRY=""
THIS_SUBMENU=""
MENUENTRY=""
THIS_MENU=""
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Going Back"
fi
previous_menuentry="$current_menuentry"
previous_menu="$current_menu"
continue 2
fi
if [[ "$(echo ${answer} | tr -d '\n' | tr -d '\r')" == "" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "exiting..."
fi
[[ $_ != $0 ]] && exit 0 2>/dev/null || return 0 2>/dev/null;
fi
done
# we parse the command
PARSED_MENU_OPTION_COMMAND="$COMMAND $FLAG_ANSWERS"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Parsed flag command: $PARSED_MENU_OPTION_COMMAND"
fi
else
# we parse the command
PARSED_MENU_OPTION_COMMAND=$COMMAND
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Parsed command: $PARSED_MENU_OPTION_COMMAND"
fi
fi
if [[ "$PARSED_MENU_OPTION_COMMAND" != "" ]]; then
$PARSED_MENU_OPTION_COMMAND
fi
# Do submenu operation
SUBMENUOPTIONS=()
while IFS= read -r option; do
SUBMENUOPTIONS+=("$option")
done < <(jq -r "$BASE_ENTRY$MENUENTRY | select(.name == \"${i}\") | .menuEntry[]? | .name" $options_path)
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "SUBMENUOPTIONS: ${SUBMENUOPTIONS[@]}"
fi
if [[ "${SUBMENUOPTIONS[@]}" != "" ]]; then
SUBMENUENTRY="$MENUENTRY | select(.name == \"${i}\") | .menuEntry[]?"
THIS_SUBMENU=".name"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "SUBMENUENTRY: $SUBMENUENTRY"
echo "THIS_SUBMENU: $THIS_SUBMENU"
fi
previous_menuentry="$current_menuentry"
previous_menu="$current_menu"
current_menuentry="$SUBMENUENTRY"
current_menu="$THIS_SUBMENU"
return 0
else
SUBMENUENTRY=""
THIS_SUBMENU=""
MENUENTRY=""
THIS_MENU=""
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "SUBMENUENTRY: $SUBMENUENTRY"
echo "THIS_SUBMENU: $THIS_SUBMENU"
fi
# goto "$menu"
return 0
fi
fi
if [[ "${answer}" == ".." ]]; then
# refactor: let's parce the places to get the parent.
previous_menuentry="$current_menuentry"
previous_menu="$current_menu"
SUBMENUENTRY=""
THIS_SUBMENU=""
MENUENTRY="$previous_menuentry"
THIS_MENU="$previous_menu"
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Clearing vars and moving back to parent menu"
fi
break && break
fi
if [[ "$(echo ${answer} | tr -d '\n' | tr -d '\r')" == "" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "exiting..."
fi
[[ $_ != $0 ]] && exit 0 2>/dev/null || return 0 2>/dev/null;
fi
done
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Exiting MENUOPTIONS for loop"
fi
done
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo "Exiting menuParse()"
fi
}
# if debug or tty, set "supermode" var to "none"
# export supermode="none" && source easybashgui
if [[ "$EASYLEGACY" == "true" ]]; then
export supermode="$OCTYPE" && source $MAIN_SCRIPT_PATH/includes/easybashgui
else
export supermode="" && source $MAIN_SCRIPT_PATH/includes/easybashgui
source $MAIN_SCRIPT_PATH/includes/easybashgui.lib
fi
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
export EZ_DEBUG_MODE="true"
else
export EZ_DEBUG_MODE="false"
fi
# If OCONFIG is not "" then set the options file to the passed location
if [[ "$OCONFIG" != "" ]]; then
options_path=$OCONFIG
else
# Otherwise, set the options file to the default location
options_path=$optionspath/options.json
fi
# Detect 32/64 bit host
HOST_ARCH=$(uname -m)
chmod +x $INCLDIR/jq-linux*
if [[ "$HOST_ARCH" == "x86_64" ]]; then
alias jq=$INCLDIR/jq-linux64
else
alias jq=$INCLDIR/jq-linux32
fi
# use jq to grab the following variables for the menu options:
# - option name (string)
# - if it is a suboption (bool)
# - command (string)
# - dependencies (array)
# Do root level menu
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo
echo "Running First Menu"
echo
fi
menuParse
if [[ "$SUBMENUENTRY" != "" ]]; then
if [[ "$EZ_DEBUG_MODE" == "true" ]]; then
echo
echo "Running second Menu"
echo
fi
menuParse "$SUBMENUENTRY" "$THIS_SUBMENU"
fi
clean_temp #(since v.6.X.X this function is no more required if easybashlib is present and then successfully loaded by easybashgui)