#!/bin/dash
# vim: set sts=4 expandtab:

# DRM: /dev/dri/by-path/* symlinks (same role as eudev 60-drm.rules path_id +
# SYMLINK for card/controlD/renderD nodes), then permissions from dri.act.

. "$VDEV_HELPERS/subr.sh"

main() {

	local STAT_RC DRI_DATA SUFFIX

	if [ "$VDEV_ACTION" = "remove" ]; then

		vdev_cleanup "$VDEV_METADATA"
		return 0
	fi

	if [ "$VDEV_PATH" != "UNKNOWN" ] && [ "$VDEV_ACTION" = "add" ]; then

		case "$VDEV_PATH" in
		dri/card* | dri/controlD* | dri/renderD*)

			DRI_DATA="$("$VDEV_HELPERS/stat_path" "$VDEV_MOUNTPOINT/$VDEV_PATH")"
			STAT_RC=$?

			if [ $STAT_RC -eq 0 ]; then

				eval "$DRI_DATA"

				if [ -n "$VDEV_PERSISTENT_PATH" ]; then

					SUFFIX=
					case "$VDEV_PATH" in
					dri/card*) SUFFIX=card ;;
					dri/controlD*) SUFFIX=control ;;
					dri/renderD*) SUFFIX=render ;;
					esac

					vdev_symlink "../../$VDEV_PATH" \
						"$VDEV_MOUNTPOINT/dri/by-path/$VDEV_PERSISTENT_PATH-$SUFFIX" \
						"$VDEV_METADATA"
				fi
			else

				vdev_error "$VDEV_HELPERS/stat_path \"$VDEV_MOUNTPOINT/$VDEV_PATH\" rc = $STAT_RC"
			fi
			;;
		esac
	fi

	"$VDEV_HELPERS/permissions.sh"
	return $?
}

if [ $VDEV_DAEMONLET -eq 0 ]; then

	main
	exit $?
fi
