|
| 1 | +#!/usr/bin/env bash |
| 2 | +# _script_description_ |
| 3 | +# |
| 4 | +# Copyright _copyright_effective_year_ _copyright_holder_name_ <_copyright_holder_contact_> |
| 5 | +# SPDX-License-Identifier: CC-BY-SA-4.0 |
| 6 | + |
| 7 | +init(){ |
| 8 | + printf \ |
| 9 | + 'Info: Operation completed without errors.\n' |
| 10 | +} |
| 11 | + |
| 12 | +printf \ |
| 13 | + 'Info: Configuring the defensive interpreter behaviors...\n' |
| 14 | +set_opts=( |
| 15 | + # Terminate script execution when an unhandled error occurs |
| 16 | + -o errexit |
| 17 | + -o errtrace |
| 18 | + |
| 19 | + # Terminate script execution when an unset parameter variable is |
| 20 | + # referenced |
| 21 | + -o nounset |
| 22 | +) |
| 23 | +if ! set "${set_opts[@]}"; then |
| 24 | + printf \ |
| 25 | + 'Error: Unable to configure the defensive interpreter behaviors.\n' \ |
| 26 | + 1>&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +printf \ |
| 31 | + 'Info: Checking the existence of the required commands...\n' |
| 32 | +required_commands=( |
| 33 | + realpath |
| 34 | +) |
| 35 | +flag_required_command_check_failed=false |
| 36 | +for command in "${required_commands[@]}"; do |
| 37 | + if ! command -v "${command}" >/dev/null; then |
| 38 | + flag_required_command_check_failed=true |
| 39 | + printf \ |
| 40 | + 'Error: This program requires the "%s" command to be available in your command search PATHs.\n' \ |
| 41 | + "${command}" \ |
| 42 | + 1>&2 |
| 43 | + fi |
| 44 | +done |
| 45 | +if test "${flag_required_command_check_failed}" == true; then |
| 46 | + printf \ |
| 47 | + 'Error: Required command check failed, please check your installation.\n' \ |
| 48 | + 1>&2 |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +if test -v BASH_SOURCE; then |
| 53 | + printf \ |
| 54 | + 'Info: Configuring the convenience variables...\n' |
| 55 | + # Convenience variables |
| 56 | + # shellcheck disable=SC2034 |
| 57 | + { |
| 58 | + printf \ |
| 59 | + 'Info: Determining the absolute path of the program...\n' |
| 60 | + if ! script="$( |
| 61 | + realpath \ |
| 62 | + --strip \ |
| 63 | + "${BASH_SOURCE[0]}" |
| 64 | + )"; then |
| 65 | + printf \ |
| 66 | + 'Error: Unable to determine the absolute path of the program.\n' \ |
| 67 | + 1>&2 |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + script_dir="${script%/*}" |
| 71 | + script_filename="${script##*/}" |
| 72 | + script_name="${script_filename%%.*}" |
| 73 | + } |
| 74 | +fi |
| 75 | + |
| 76 | +trap_err(){ |
| 77 | + printf \ |
| 78 | + 'Error: The program has encountered an unhandled error and is prematurely aborted.\n' \ |
| 79 | + 1>&2 |
| 80 | +} |
| 81 | + |
| 82 | +printf \ |
| 83 | + 'Info: Setting the ERR trap...\n' |
| 84 | +if ! trap trap_err ERR; then |
| 85 | + printf \ |
| 86 | + 'Error: Unable to set the ERR trap.\n' \ |
| 87 | + 1>&2 |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | +init |
0 commit comments