#!/bin/bash

set -e
#set -x

# Script param (sent as env var):
# ID_BUS (example: scsi, ata)
# DEVPATH (example: /devices/pci0000:80/0000:80:08.2/0000:81:00.0/ata8/host7/target7:0:0/7:0:0:0/block/sdc)
#
# In fact, we see the variables as per:
# udevadm test -a -p  $(udevadm info -q path -n /dev/sdc)

if [ $(id -u) -ne 0 ]; then
	echo "$0 needs root: please use sudo."
	exit 1
fi

# Source Auxilium (args parser)
. /usr/share/auxilium/auxilium.sh

auxilium_init $(basename $0) -m "Provides a device name categorized based on its physical location"
auxilium_add by_devname -s d -t str -m 'Resolve by device name (\/dev\/sdX, by default, get DEVPATH by env var)'
auxilium_add to_fo_name -s F -t opt -m 'Provides a name under form Box0-Disk0 (by default, its sdX)'

if [ -f "/usr/share/ferreus-orbit/common.sh" ]; then
	. "/usr/share/ferreus-orbit/common.sh"
else
	. "../share/common.sh"
fi

auxilium_parse "$@"

fo_lock

[ -z "$MAXILIUM_ARG_by_devname" ] || eval $(udevadm info -q property --export ${MAXILIUM_ARG_by_devname})

logger -t "udev" "===> DEVPATH=${DEVPATH}"

echo $(fo_get_sorted_name)
