#!/bin/dash
#
# sysfs coldplug queue markers (VDEV_OS_QUEUE_TRACKING_FLAG from uevent QUEUE_TRACKING_FLAG):
#   Absolute value 1|2 — touch stamp resolved from vdevd.globals.json for the active
#                        profile (VDEV_CONFIG_FILE matches profiles.*.config).
#                        In coldplug_only mode (VDEV_COLDPLUG_ONLY=1), also SIGTERM parent
#                        so poll/signalfd loop exits promptly after writing the stamp.
#   Absolute value 3 — SIGTERM to parent vdevd (reload / coldplug-only exit)
#
# Stamp basename and directory come from JSON via jq (.globals.stamp_dir).
# VDEV_CONFIG_FILE may be absolute or basename; both are matched against JSON config fields.
#
# Invoked from zzzz-os-queue-tracking.act (daemonlet=false).

_json="${VDEV_GLOBALS_JSON:-/etc/vdev/vdevd.globals.json}"

if [ -r "$_json" ] && command -v jq >/dev/null 2>&1; then
    # Clean up comments // and also /* */
	VDEV_JSON_DATA="$(sed -E 's|//.*||g' "$_json" |
        sed 's|/\*.*\*/||g' |
        grep -v '^$' |
        jq -r .)"
    export VDEV_JSON_DATA
else
    exit 1
fi

_conf_full="${VDEV_CONFIG_FILE:-}"
_conf_bn="${_conf_full##*/}"

resolve_stamp() {
   printf '%s' "$VDEV_JSON_DATA" | jq -r --arg conf "${_conf_full}" --arg b "${_conf_bn}" '
      def root: (.profiles // .states // {});
      [
        (root | to_entries[]? | .value
          | select(type == "object")
          | select(.config == $conf or .config == $b) | .stamp),
        (root | to_entries[]? | .value
          | select(type == "object")
          | select(.reload.config == $conf or .reload.config == $b) | .reload.stamp)
      ] | map(select(. != null and . != "")) | first // empty'
}

# Non-empty when VDEV_CONFIG_FILE is a profiles.*.config that defines .reload.
resolve_state_reload_config() {
   printf '%s' "$VDEV_JSON_DATA" | jq -r --arg conf "${_conf_full}" --arg b "${_conf_bn}" '
      def root: (.profiles // .states // {});
      [root | to_entries[]? | .value
        | select(type == "object")
        | select(.config == $conf or .config == $b)
        | .reload.config // empty
      ] | map(select(. != "")) | first // empty'
}

resolve_stamp_dir() {
   printf '%s' "$VDEV_JSON_DATA" | jq -r --arg conf "${_conf_full}" --arg b "${_conf_bn}" '
      def hit(o):
        (o | type) == "object" and (
          (.config == $conf or .config == $b) or
          ((.reload.config // "") == $conf or (.reload.config // "") == $b)
        );
      def root: (.profiles // .states // {});
      def any_hit:
        [root | to_entries[]? | .value | select(hit(.))] | length > 0;
      if any_hit then
        (.globals["states-stamp-dir"] // .globals.stamp_dir // empty)
      else
        empty
      end'
}

touch_stamp() {
   local _stamp _stamp_dir

   [ -n "$_conf_bn" ] || return 0

   _stamp="$(resolve_stamp)"
   [ -z "$_stamp" ] && return 0

   _stamp_dir="$(resolve_stamp_dir)"
   [ -z "$_stamp_dir" ] && _stamp_dir="${VDEV_STAMP_DIR:-/run/vdev}"

   /bin/mkdir -p "$_stamp_dir"
   /bin/touch "${_stamp_dir}/${_stamp}"
}

signal_parent_exit() {
   /bin/kill -s SIGTERM "$PPID" 2>/dev/null || true
}

main() {

   local _flag_abs
   
   [ -z "$VDEV_OS_QUEUE_TRACKING_FLAG" ] && return 0

   [ "$VDEV_OS_QUEUE_TRACKING_FLAG" -eq 0 ] && return 0
	
   _flag_abs="${VDEV_OS_QUEUE_TRACKING_FLAG#-}"

   case "${_flag_abs}" in
   1|2)
      touch_stamp
      signal_parent_exit
      ;;
   3)
      signal_parent_exit
      ;;
   *)
      ;;
   esac   
   
   if [ "${VDEV_COLDPLUG_ONLY:-0}" -eq 1 ]; then
      signal_parent_exit
   fi        
   
   return 0
}

if [ "${VDEV_DAEMONLET:-0}" -eq 0 ]; then
   main
   exit $?
fi
