#!/bin/sh
progname=$(basename "$0")
while getopts ':hqd:s:a:f:u:' opt "$@"; do
  case "${opt}" in
    'h')
      printf '%s\n' "Invalid option -${OPTARG}" 1>&2
      usage
      # exit 2
      ;;
    \?)
      >&2 printf '%s\n' "\033[31merror\033[0m: invalid option: \033[1m-${OPTARG}\033[0m"
      exit 1
      ;;
    # ':')
    #   >&2 echo "\033[31merror\033[0m: missing required argument: \033[1m-${OPTARG}\033[0m"
    #   exit 1
    #   ;;
    -*)
      printf '%s: invalid option %s\n' "${progname}" "$1" >&2
      usage >&2
      exit 1
      ;;
    *)
      >&2 printf '%s\n' "\033[31minternal error\033[0m: unhandled option: \033[1m-${opt}\033[0m"
      exit 1
      ;;
  esac
done

# func_exit STATUS
# exits with a given status
# this function needs to be used, rather than 'exit',
# when a 'trap' handler is in effect that refers to $?.
func_exit() {
  (exit "$1"); exit "$1"
}

# func_fatal_error message
# outputs to stderr a fatal error message, and terminates the program
# input:
# - progname                 name of this program
progname=$(basename "$0")
func_fatal_error() {
  echo "${progname}: *** $1" 1>&2
  echo "${progname}: *** Stop." 1>&2
  func_exit 1
}

# func_warning message
# outputs to stderr a warning message
func_warning() {
  echo "$(basename "$0"): warning: $1" 1>&2
}
