#! /bin/sh

# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.

#ident	"$Header: selectdevice 1.5 89/10/11 $@(#)sadmin:shell/selectdevice	2.9.1.1"
#	Select one of the simple administration names for a block or character
#	device.

#!	chmod +x ${file}

question='Select which drive to use:'

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}SA
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'`
devs=`ls ${patterns} 2>/dev/null`

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 -i20 -t -n' ' -4`
select=`checklist -q q -k "${pid}" -fep "${question}
${list}
Enter a number, a name, the initial part of a name, or
? for HELP,  q to QUIT:" ${list}`
case ${select} in
'' )
	exit 0
	;;
[1-9]  |  [1-9][0-9] )
	DEV=${DIR}/`echo "${devs}" |  sed -n "${select}p"`
	;;
* )
	DEV=${DIR}/${select}
esac
if [ ! ${type} ${DEV} ]
then
	admerr $0 ${DEV} is not of type ${typename}
	kill ${pid}
	exit 1
fi
echo ${DEV}
