#! /bin/sh
#
#   seldisk.sh - ディスクの選択 (floppy版)
#
#	0.0: Jun. 19, 2004 by Dai ISHIJIMA
#	0.1: May  17, 2020 (for v20, FreeBSD 11.3R)
#	0.2: Dec. 25, 2022 (for ubuntu)
#	0.3: Jan. 11, 2023 (disksize, diskname)
#

#dmesg="${DMESG:-/var/run/dmesg.boot}"
#devices="${DEVICES:-ada|da}"

#resultfil=/tmp/result
#sed -n -E "/^(${devices})[0-9]:.*B [(<]/p" $dmesg |
#	sed 's/^\([a-z][a-z]*[0-9]\).*/\1/' > $resultfil
#disks=`cat $resultfil`

(cd /dev; ls -f ada? da? ad? sd[a-d] hd[a-d]) > /tmp/disks 2>/dev/null
disks=`cat /tmp/disks`

echo ''; echo ''; echo ''
echo '*** select a disk to erase data'
echo ''
n=0
m=0
for d in $disks ; do
	#dname=`sed -n -E "/^${d}:.*<.*>/p" $dmesg | sed 's/.*\(<.*>\).*/\1/'`
	if sh disksize.sh -b $d > /dev/null 2>&1 ; then
		bytes=`sh disksize.sh -b $d`
		cbytes=`sh disksize.sh -c $d`
		cap=`sh disksize.sh -s $d`
		nam=`sh diskname.sh $d`
		#echo "    $n: $d $dname $bytes bytes ($cap)"
		echo "    $n: $d <$nam> $cbytes bytes ($cap)"
		m=$n
		n=$(( $n + 1 ))
	fi
done
echo ''
echo -n "which drive to erase data? [0-$m]: "
read ans

case x"$ans" in
	x[0-$m])
		n=0;
		for d in $disks ; do
			if [ "$n" = "$ans" ]; then
				break
			fi
			n=$(( $n + 1 ))
		done
		;;
	*)
		d=$disk
		;;
esac

disk=$d

# EOF
