#!/bin/sh
# Block device events of mdevd: runs the hooks of /usr/lib/mdevd/block.d and
# /etc/mdevd/block.d in the order of their names; a hook in /etc replaces the
# one of the same name in /usr/lib, a link to /dev/null disables it. A hook
# runs with the variables of the event (ACTION, MDEV, DEVPATH, DEVTYPE …),
# DEVNODE=/dev/$MDEV and, except on remove, the ID_* variables of
# blkid -p -o udev (ID_FS_TYPE, ID_FS_UUID …).
PATH=/usr/sbin:/usr/bin:/sbin:/bin
DEVNODE=/dev/$MDEV
export PATH DEVNODE
if [ "$ACTION" != remove ]; then
	ids=$(blkid -p -o udev "$DEVNODE" 2>/dev/null)
	while IFS='=' read -r k v; do
		case $k in ID_[A-Z0-9_]*) export "$k=$v" ;; esac
	done <<IDS
$ids
IDS
fi
for d in /usr/lib/mdevd/block.d /etc/mdevd/block.d; do
	for f in "$d"/*; do [ -e "$f" ] && printf '%s\n' "${f##*/}"; done
done | sort -u | while read -r n; do
	f=/etc/mdevd/block.d/$n
	[ -e "$f" ] || f=/usr/lib/mdevd/block.d/$n
	[ -f "$f" ] && [ -x "$f" ] || continue
	"$f" || echo "mdevd block: $n exited $? on $ACTION $MDEV" >&2
done
exit 0
