NOTAV=/tmp/rdia_notav.$$
AVAIL=/tmp/rdia_avail.$$

choose_port()
{
	message_start looking for ports...
	
	pmadm -r -p rdamon -s rdaserv >/dev/null 2>&1
#	pmadm -l 2> /dev/null | awk '{ print $6 }' | sed #'s/\(\/dev\/term\/\)\(.*\)/\2/' > $NOTAV
	
	pmadm -l 2> /dev/null | awk '{ print $6 }' > $NOTAV
	
	for PORT in `ls /dev/term`
	do
		if [ `grep -c /dev/term/$PORT $NOTAV` = 0 ]; then

			dd if=/dev/null of=/dev/cua/$PORT 2>/dev/null

			if [ $? = 0 ]; then
				echo $PORT
			fi
		fi
	done > $AVAIL
	
	message_done
	
	if [ `wc -l < $AVAIL` = 0 ]; then
		echo NULLPORT
		return
	fi
			
	while /bin/true
	do 
		message ""
		message "The following port(s) are available for use by the"
		message "remote diagnostics service:"
		message ""

		sed 's/^/		\/dev\/term\//g' < $AVAIL | cat_message
	
		message ""
		message "Please enter a port you wish to use: /dev/term/"\\c
		read PORT_CHOICE
	
		if [ "$PORT_CHOICE" = "" ]; then
			message Sorry, that is an invalid response - try again
			message ""
			continue
		fi

		if [ "`grep -s -c /dev/term/$PORT_CHOICE $NOTAV`" -ne 0 ]; then
			message Sorry, $PORT_CHOICE is unavailable - try again
			message ""
			continue
		fi
	
		if [ "`grep -s -c $PORT_CHOICE $AVAIL`" -eq 0 ]; then
			message "Sorry, invalid port - try again"
			message ""
			continue
		fi
	
		break
	done
	
	rm $AVAIL $NOTAV
	
	echo $PORT_CHOICE
}

validate_port()
{
	if [ "$1" = NULLPORT ]; then
		message No ports are available to run the service.
		message Please run 'pmadm -l' to view active port monitors
		message and then you should remove one using 'pmadm -r'.

		terminate No ports available
	fi
}
