Add debug mode and help message

This commit is contained in:
gardouille 2020-07-15 18:03:00 +02:00
parent 70bdbc8a89
commit 4da7cdad05
Signed by: gardouille
GPG Key ID: E759BAA22501AF32
1 changed files with 69 additions and 3 deletions

72
winboot
View File

@ -9,6 +9,10 @@ readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
## Debug mode to print specific messages
DEBUG=1
## Grub specific vars
readonly GRUB_DEFAULT_PATH='/etc/default/grub'
readonly GRUB_DEFAULT_DIR='/etc/default/grub.d'
readonly WIN_GRUB="2"
@ -17,8 +21,54 @@ readonly WIN_GRUB="2"
REBOOT=0
HALT=1
## Colors
readonly PURPLE='\033[1;35m'
readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly COLOR_DEBUG="${PURPLE}"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d,--debug|-h,--help|--halt,--poweroff|--reboot]
Try to set a specific grub's entry for the next boot and
reboot the host (default behaviour).
EXAMPLES:
- Set the grub entry ${WIN_GRUB} (Windows by default) and reboot
${PROGNAME}
- Set the grub entry ${WIN_GRUB} (Windows by default) and shutdown
${PROGNAME} --halt
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
--halt,--poweroff
Ask to shutdown at the end of the script.
--reboot
Ask to reboot at the end of the script (default behaviour).
EOF
}
# }}}
debug_message() { # {{{
local_debug_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_debug_message}"
}
# }}}
main() { # {{{
# First check in Grub default dir if saved mode is set
@ -60,14 +110,22 @@ if [ ! "${NBARGS}" -eq "0" ]; then
while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do
case "${1}" in
--reboot ) ## A reboot was requested (default behaviour)
REBOOT=0
HALT=1
-d|--debug ) ## debug
DEBUG=0
;;
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
--halt|--poweroff ) ## A poweroff was requested
REBOOT=1
HALT=0
;;
--reboot ) ## A reboot was requested (default behaviour)
REBOOT=0
HALT=1
;;
-- ) ## End of options list
## End the while loop
break
@ -80,12 +138,20 @@ if [ ! "${NBARGS}" -eq "0" ]; then
;;
esac
debug_message "Arguments management \
${RED}${1}${COLOR_DEBUG} option managed."
## Move to the next argument
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}