Skip to content

Commit

Permalink
src/winusb: Eliminate global variables
Browse files Browse the repository at this point in the history
Signed-off-by: 林博仁 <[email protected]>
  • Loading branch information
brlin-tw committed Jun 7, 2017
1 parent 4de4ba0 commit f2622e5
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/winusb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ declare -i\
only_for_gui='0'\
no_color='0'

# Commands that needs to be determined in runtime
# due to different names in distributions
declare\
command_mkdosfs\
command_grubinstall

declare -ir DD_BLOCK_SIZE="((4 * 1024 * 1024))" # 4MiB

# FIXME: No documentation for this non-trivial parameter
Expand All @@ -96,7 +90,15 @@ init(){
source_fs_mountpoint="/media/winusb_source_$(date +%s)_$$"
target_fs_mountpoint="/media/winusb_target_$(date +%s)_$$"

if ! check_runtime_dependencies; then
# Commands that needs to be determined in runtime
# due to different names in distributions
declare\
command_mkdosfs\
command_grubinstall

if ! check_runtime_dependencies\
command_mkdosfs\
command_grubinstall; then
exit 1
fi

Expand Down Expand Up @@ -136,7 +138,8 @@ init(){
if [ "${install_mode}" = 'device' ]; then
destroy_and_rebuild_entire_disk\
"${target_device}"\
"${target_partition}"
"${target_partition}"\
"${command_mkdosfs}"
fi

if [ "${install_mode}" = 'partition' ]; then
Expand Down Expand Up @@ -170,7 +173,8 @@ init(){

install_bootloader_grub\
"${target_fs_mountpoint}"\
"${target_device}"
"${target_device}"\
"${command_grubinstall}"

cleanup_mountpoints\
"${source_fs_mountpoint}"\
Expand Down Expand Up @@ -305,9 +309,10 @@ determine_target_parameters(){
}; declare -fr determine_target_parameters

install_bootloader_grub(){
util_check_function_parameters_quantity 2 "${#}"
util_check_function_parameters_quantity 3 "${#}"
local -r target_fs_mountpoint="${1}"; shift 1
local -r target_device="${1}"
local -r target_device="${1}"; shift 1
local -r command_grubinstall="${1}"

echo_with_color blue "Installing GRUB bootloader for legacy boot support..."
"${command_grubinstall}"\
Expand Down Expand Up @@ -465,9 +470,10 @@ util_check_function_parameters_quantity(){
}; declare -fr util_check_function_parameters_quantity

destroy_and_rebuild_entire_disk(){
util_check_function_parameters_quantity 2 $#
util_check_function_parameters_quantity 3 $#
local -r target_device="${1}"; shift
local -r target_partition="${1}"
local -r target_partition="${1}"; shift
local -r command_msdosfs="${1}"

echo_with_color blue "DESTROYING previous data on \"${target_device}\" and reintializing ..."
# Create new PC, a.k.a. MBR, a.k.a. msdos style partition table(and overwrite the old one, whatever it was)
Expand Down Expand Up @@ -505,7 +511,10 @@ destroy_and_rebuild_entire_disk(){
}; declare -fr destroy_and_rebuild_entire_disk

check_runtime_dependencies(){
util_check_function_parameters_quantity 0 $#
util_check_function_parameters_quantity 2 $#
local -n command_mkdosfs_ref="$1"; shift
local -n command_grubinstall_ref="$1"

local result="unknown"

for required_command in\
Expand Down Expand Up @@ -534,9 +543,9 @@ check_runtime_dependencies(){
done

if command -v 'mkdosfs' &> /dev/null; then
command_mkdosfs='mkdosfs'
command_mkdosfs_ref='mkdosfs'
elif command -v 'make.msdos' &> /dev/null; then
command_mkdosfs='mkfs.msdos'
command_mkdosfs_ref='mkfs.msdos'
else
echo_with_color red\
"${FUNCNAME[0]}: Error: mkdosfs or mkfs.msdos command not found!" >&2
Expand All @@ -546,9 +555,9 @@ check_runtime_dependencies(){
fi

if command -v 'grub-install' &> /dev/null; then
command_grubinstall='grub-install'
command_grubinstall_ref='grub-install'
elif command -v 'grub2-install' &> /dev/null; then
command_grubinstall='grub2-install'
command_grubinstall_ref='grub2-install'
else
echo_with_color red "${FUNCNAME[0]}: Error: grub-install or grub2-install command not found!" >&2
echo_with_color red "${FUNCNAME[0]}: Error: Please make sure that GNU GRUB is properly installed!" >&2
Expand Down

0 comments on commit f2622e5

Please sign in to comment.