#!/bin/bash
# vim: softtabstop=2 shiftwidth=2 expandtab

# shellcheck disable=SC1091

sources=(
  /lib/profiling-lib.sh
  /etc/zfsbootmenu.conf
  /lib/kmsg-log-lib.sh
)

for src in "${sources[@]}"; do
  # shellcheck disable=SC1090
  source "${src}" >/dev/null 2>&1 || exit 1
done

hook_stage="${1}"

if [ -z "${hook_stage}" ]; then
  zerror "required hook stage undefined"
  exit 1
fi

if [ ! -d "/libexec/hooks/${hook_stage}" ]; then
  zdebug "no hook directory for ${hook_stage}"
  exit 1
fi

_ran_hook=
for _hook in "/libexec/hooks/${hook_stage}"/*; do
  [ -x "${_hook}" ] || continue
  zinfo "processing ${_hook}"
  "${_hook}"
  _ran_hook="yes"
done

# Return success if at least one hook ran
[ -n "${_ran_hook}" ]
