Merge pull request #540 from slowpeek/master

Tidy the script startup code
This commit is contained in:
Jack
2021-10-14 19:08:42 +03:00
committed by GitHub
+42 -30
View File
@@ -2,7 +2,7 @@
# Push-button installer of macOS on VirtualBox # Push-button installer of macOS on VirtualBox
# (c) myspaghetti, licensed under GPL2.0 or higher # (c) myspaghetti, licensed under GPL2.0 or higher
# url: https://github.com/myspaghetti/macos-virtualbox # url: https://github.com/myspaghetti/macos-virtualbox
# version 0.99.1.3 # version 0.99.1.4
# Dependencies: bash coreutils gzip unzip wget xxd dmg2img # Dependencies: bash coreutils gzip unzip wget xxd dmg2img
# Optional features: tesseract-ocr tesseract-ocr-eng # Optional features: tesseract-ocr tesseract-ocr-eng
@@ -205,6 +205,9 @@ if [[ -n "$(gcsplit --help 2>/dev/null)" ]]; then
function seq() { function seq() {
gseq "$@" gseq "$@"
} }
function sort() {
gsort "$@"
}
fi fi
} }
@@ -1096,17 +1099,17 @@ fi
function and_all_subsequent_stages() { function and_all_subsequent_stages() {
# if exactly two arguments were specified on the command line, and the first is a stage title, # if exactly two arguments were specified on the command line, and the first is a stage title,
# then perform all stages subsequent to the specified stage, otherwise do nothing. # then perform all stages subsequent to the specified stage, otherwise do nothing.
# first_argument is already sanitized so it's safe (though incorrect) to use as a regex (for brevity) [[ ${#specified_arguments[@]} -ne 2 ]] && return
first_argument=${specified_arguments%% *} for stage in "${stages[@]}"; do
last_argument=${specified_arguments##* } [[ "${stages[0]}" = "${specified_arguments[0]}" ]] && break
[[ "${first_argument} ${last_argument}" = "${specified_arguments}" ]] && \ stages=( "${stages[@]:1}" )
[[ "${stages}" =~ "${first_argument}" ]] && \ done
for stage in ${stages##*${first_argument}}; do ${stage}; done for stage in "${stages[@]:1}"; do ${stage}; done
} }
function documentation() { function documentation() {
low_contrast_stages="" low_contrast_stages=""
for stage in ${stages}; do for stage in "${stages[@]}"; do
low_contrast_stages="${low_contrast_stages}"' '"${low_contrast_color}${stage}${default_color}"$'\n' low_contrast_stages="${low_contrast_stages}"' '"${low_contrast_color}${stage}${default_color}"$'\n'
done done
echo -ne "\n ${highlight_color}NAME${default_color} echo -ne "\n ${highlight_color}NAME${default_color}
@@ -1685,7 +1688,7 @@ function prompt_delete_y_n() {
# command-line argument processing # command-line argument processing
check_shell check_shell
stages=' stages=(
check_gnu_coreutils_prefix check_gnu_coreutils_prefix
set_variables set_variables
welcome welcome
@@ -1703,27 +1706,36 @@ stages='
create_target_virtual_disk create_target_virtual_disk
populate_macos_target_disk populate_macos_target_disk
prompt_delete_temporary_files prompt_delete_temporary_files
' )
[[ -z "${1}" ]] && for stage in ${stages}; do ${stage}; done && exit
[[ "${1}" = "documentation" ]] && documentation && exit other_commands=(
valid_arguments=(${stages//$'[\r\n]'/ } check_shell troubleshoot documentation and_all_subsequent_stages) check_shell
specified_arguments="$@" # this variable is used in the function "and_all_subsequent_stages" troubleshoot
for specified_arg in "$@"; do documentation
there_is_a_match="" and_all_subsequent_stages
# doing matching the long way to prevent delimiter confusion )
for valid_arg in "${valid_arguments[@]}"; do
[[ "${valid_arg}" = "${specified_arg}" ]] && there_is_a_match="true" && break # script called without arguments
done [[ $# -eq 0 ]] && for stage in "${stages[@]}"; do ${stage}; done && exit
if [[ -z "${there_is_a_match}" ]]; then
echo -e "\nOne or more specified arguments is not recognized." # first argument is "documentation"
echo -e "\nRecognized stages:\n${stages}" [[ "${1}" == "documentation" ]] && documentation && exit
echo -e "Other recognized arguments:\n\n check_shell\n documentation\n troubleshoot\n and_all_subsequent_stages"
echo -e "\nView documentation by entering the following command:" specified_arguments=("$@") # this variable is used in the function "and_all_subsequent_stages"
would_you_like_to_know_less sorted_unique_recognized_arguments="$( printf "%s\n" ${stages[@]} ${other_commands[@]} | sort -bu )"
exit sorted_unique_recognized_and_specified_arguments="$( printf "%s\n" ${stages[@]} ${other_commands[@]} $@ | sort -bu )"
fi # if a specified argument is different from any recognized argument
done if [[ "${sorted_unique_recognized_and_specified_arguments}" != "${sorted_unique_recognized_arguments}" ]]; then
echo -e "\nOne or more specified arguments is not recognized."
echo -e "\nRecognized stages:\n"
printf ' %s\n' "${stages[@]}"
echo -e "\nOther recognized arguments:\n"
printf ' %s\n' "${other_commands[@]}"
echo -e "\nView documentation by entering the following command:"
would_you_like_to_know_less
exit
fi
check_gnu_coreutils_prefix check_gnu_coreutils_prefix
set_variables set_variables
check_dependencies check_dependencies
for argument in "$@"; do ${argument}; done for argument; do ${argument}; done