#! /bin/sh
#
#   diskname.sh -- ディスクの名称を得る
#
#	0.0: Jan. 11, 2023 by Dai ISHIJIMA (for Live CD)
#

temp=/tmp/diskname.$$

trap 'rm -f $temp*; exit 1' 1 2 15

while [ $# -gt 1 ]; do
	case x"$1" in
	    # ディスクのサイズ
	    x-d)
		MODE=DEBUG
		shift
		file="$1"
		;;
	    x-b)
		FLAVOR=BSD
		;;
	    x-l)
		FLAVOR=LINUX
		;;
	esac
	shift
done

if [ $# -ne 1 ]; then
	echo "Usage: $0 [-[bl] -d file] disk" 1>&2
fi

disk=$1

case x"$FLAVOR" in
    xBSD)
	case x"$MODE" in
		xDEBUG)
			f=$file
			;;
		*)
			f=/var/log/messages
			;;
	esac
	grep "$disk.* <" $f | sed 's/.*<\([^>]*\)>.*$/\1/' | sed -n '$p'
	;;
    x|xLINUX)
	case x"$MODE" in
		xDEBUG)
			f=$file
			;;
		*)
			f=/proc/scsi/scsi
			;;
	esac
	cat $f | sed -n 's/  Vendor: //p' | sed 's/  *Model://' |
		sed 's/  */ /g' | sed 's/ *$//' > $temp.name

	(cd /dev; ls sd[a-d] hd[a-d]) > $temp.disks 2>/dev/null
	cp /dev/null $temp.devnam
	nline=1
	for d in `cat $temp.disks` ; do
		fdisk -l /dev/$d > $temp.fdisk 2>/dev/null
		if [ -s $temp.fdisk ]; then
			echo -n "$d:" >> $temp.devnam
			sed -n "${nline}"p $temp.name >> $temp.devnam
			nline=$(( $nline + 1 ))
		fi
	done
	grep $disk $temp.devnam | sed 's/^[^:]*://'
esac

rm -f $temp*

exit 0

