#	START NEW ARIX SCCS HEADER
#
#	@(#) seldevice: version 25.1 created on 12/2/91 at 17:04:29
#
#	Copyright (c) 1990 by Arix Corporation
#	All Rights Reserved
#
#	@(#)seldevice	25.1	12/2/91 Copyright (c) 1990 by Arix Corporation
#
#	END NEW ARIX SCCS HEADER
#

#	ATT:#ident	"sadmin:shell/selectdevice	2.7"

#ident	"@(#)sadmin/shell:seldevice	25.1"

#	Select one of the simple administration names for a block or character
#	device.

#!	chmod +x ${file}

question='Select which drive to use:'
if [ $# -lt 2 ]
then
	echo >&2 "Usage:  $0 -[b|c] [ -q question ] pid [ dir ] [ names... ]"
	exit 1
fi

while [ $# -gt 1 ]
do
	case "$1" in
	-b )	#	block device
		type=-b
		typename=block
		r=
		;;
	-c )	#	character (aka raw) device
		type=-c
		typename=character
		r=r
		;;
	-q )
		question=$2
		shift
		;;
	-* )
		echo >&2 "Usage:  $0 -[b|c] [ -q question ] pid [ dir ] [ names... ]"
		exit 1
		;;
	* )
		break
	esac
	shift
done

pid=${1:?}
shift

if [ $# -lt 1 ]
then
	DIR=/dev/${r}dsk
else
	DIR=$1
	shift
fi

if [ ! -d $DIR ]
then
	admerr $0 $DIR nonexistent directory
	kill  ${pid}
	exit 1
fi

cd $DIR

patterns=`echo $*  |  sed 's/[^ ]\{1,\}/& &[!a-zA-Z]*/g'`
cont=0
drives=0
drivet=4
try=0
echo $try > /tmp/t1

while true 
do

if [ ${try} -lt 2 ] 
then
devices='c'$cont'd['$drives'-'$drivet']s*'
else
devices='c'$cont'd1['$drives'-'$drivet']s*'
fi

devs=`/bin/ls ${devices} 2>/dev/null`
a=$?

if [ ${a} -ne 0 ]
then
cont=0
drives=0
drivet=4
try=0

else

if [ -z "${devs}" ]
then
	admerr $0 Problems in $DIR, no valid ${typename} devices.
	kill  ${pid}
	exit 1
fi

if [ `echo "${devs}"  |  wc -l` = 1 ]
then	
	if [ ! ${type} ${devs} ]
	then
		admerr $0 ${devs} is not of type ${typename}.
		kill  ${pid}
		exit 1
	fi

	echo $DIR/${devs}
	exit 0
fi

#list=`echo "${devs}"  |  pr -t -n' ' -5`
list=`echo "${devs}"  |  pr -t -n' ' -5`
select=`checklist -q q -k "${pid}" -fep "${question}
${list}
Enter a number, a name, the initial part of a name, or
g to display rest of list,  ? for HELP, DEL to QUIT:" ${list}`

case ${select} in
[1-9]  |  [1-9][0-9] )
	DEV=${DIR}/`echo "${devs}" |  sed -n "${select}p"`
	break
	;;
c[0-9]d[0-9]s[0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9]d[0-9]s[0-9][0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9]d[0-9][0-9]s[0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9][0-9]d[0-9]s[0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9]d[0-9][0-9]s[0-9][0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9][0-9]d[0-9]s[0-9][0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9][0-9]d[0-9][0-9]s[0-9])
	DEV=${DIR}/${select}
	break
	;;
c[0-9][0-9]d[0-9][0-9]s[0-9][0-9])
	DEV=${DIR}/${select}
	break
	;;

* )
	try=`cat /tmp/t1`
	if [ ${try} -eq 0 ] 
	then
		drives=5
		drivet=9
		try=1	
		echo $try > /tmp/t1
	else
	if [ ${try} -eq 1 ] 
	then
		drives=0	
		drivet=5
		try=2
		echo $try > /tmp/t1
	else
	if [ ${try} -eq 2 ] 
	then
		cont=`expr ${cont} + 1`
		drives=0
		drivet=4
		try=0
		echo $try > /tmp/t1
	fi
	fi
	fi
	continue
	;;
esac
fi
done

rm /tmp/t1

if [ ! ${type} ${DEV} ]
then
	admerr $0 ${DEV} is not of type ${typename}
	kill  ${pid}
	exit 1
fi
echo ${DEV}
