#!/bin/sh
#v10.0.0
: ${distdir="/usr/local/cbsd"}
# MAIN
if [ -z "${workdir}" ]; then
	[ -z "${cbsd_workdir}" ] && . /etc/rc.conf
	[ -z "${cbsd_workdir}" ] && exit 0
	workdir="${cbsd_workdir}"
fi

[ ! -f "${workdir}/cmd.subr" ] && exit 1

. ${workdir}/cmd.subr

ARGS=$1
FLAGS_MD=""
FLAGS_CD=""
FLAGS_VERBOSE=""
MYDESC="Return of visible storage"

while [ -n "$1" ]; do
	case "$1" in
		-m)
			FLAGS_MD=1 ;;
		-v)
			FLAGS_VERBOSE=1 ;;
		-c)
			FLAGS_CD=1 ;;
	esac
	shift
done

# Create our device listing
SYSDISK=$(sysctl -n kern.disks)
if [ -n "${FLAGS_MD}" ]; then
	MDS=$( /sbin/mdconfig -l )
	[ -n "${MDS}" ] && SYSDISK="${SYSDISK} ${MDS}"
fi

# Now loop through these devices, and list the disk drives
for i in ${SYSDISK}; do
	# Get the current device
	DEV="${i}"

	# Make sure we don't find any cd devices
	if [ -z "${FLAGS_CD}" ]; then
		case "${DEV}" in
			acd[0-9]*|cd[0-9]*|scd[0-9]*) continue ;;
		esac
	fi

	# Try and find some identification information with camcontrol or atacontrol
	NEWLINE=$( /sbin/camcontrol identify $DEV 2>/dev/null| ${SED_CMD} -ne 's/^device model *//p' )
	[ -z "$NEWLINE" ] && NEWLINE=" <Can't identify model>"

	SIZE=$( /usr/sbin/diskinfo ${DEV} 2>/dev/null| ${AWK_CMD} '{printf $3}' )
	[ -z "${SIZE}" ] && SIZE="0"

	[ -n "${FLAGS_MD}" ] && echo "${DEV}" | grep -E '^md[0-9]+' >/dev/null 2>/dev/null && NEWLINE=" <Memory Disk>"

	# Save the disk list
	[ ! -z "$DLIST" ] && DLIST="\n${DLIST}"

	DLIST="${DEV}:${NEWLINE}:${SIZE}${DLIST}"
done

echo -e "$DLIST" | /usr/bin/sort -k 1
