#!/lib/klibc/bin/sh
# $Id: udev.get_persistent_device_name.sh 87 2004-07-29 13:16:55Z olh $
#set -x

# this script should provide a persistent device name for a given
# entry below /sys

# example for all:
# for i in `find /sys/block -name dev` ;do DEVPATH="`echo $i | sed -e 's@^/sys\|/dev@@g'`" $0 block ; done

PATH=/lib/klibc/bin #/usr:/usr/sbin:/usr/bin:/sbin:/bin
SYSFS=/sys
export PATH
RESULT=1
CDROM=
TYPE=
OPWD="`pwd`"
full_sysfs_class_path=
full_sysfs_device_path=
persistant_list=

if [ -z "$DEVPATH" -a -z "$1" ] ; then
exit 1
fi
if [ -z "$DEVPATH" ] ; then
	case "$1" in
		$SYSFS/*)
		DEVPATH="${1#$SYSFS}"
		;;
		*)
		DEVPATH=$1
		;;
	esac
fi

if [ ! -d $SYSFS$DEVPATH ] ; then
exit 1
fi
if [ ! -f $SYSFS$DEVPATH/dev ] ; then
exit 1
fi

case "$DEVPATH" in
	/block/*)
		TYPE=block
		;;
	/class/*)
		TYPE="${DEVPATH#/class/}"
		TYPE="${TYPE%%/*}"
		;;
	*)
		exit 1
		;;
esac
		


#
##
#


if [ -x /sbin/udev.get_unique_hardware_path.sh ] ; then
	by_path="`/sbin/udev.get_unique_hardware_path.sh $DEVPATH`"
	if [ ! $? ]; then
	    by_path=
	fi
	: by_path $by_path
fi
if [ -x /sbin/udev.get_unique_drive_id.sh ] ; then
	by_id="`/sbin/udev.get_unique_drive_id.sh $DEVPATH`"
	if [ ! $? ]; then
	    by_id=
	fi
	: by_id $by_id
fi
if [ ! -z "$by_path" ] ;then
	for i in $by_path ; do
	    persistent_list="$persistent_list $i"
	done
fi
if [ ! -z "$by_id" ] ;then
	for i in $by_id ; do
	    persistent_list="$persistent_list $i"
	done
fi
if [ -z "$persistent_list" ] ; then
	: persistent_list empty
	exit 1
fi
# -n required, udev would pass the newline to mknod
echo -n $persistent_list
exit 0
